001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.Properties;
018    
019    /**
020     * @author Brian Wing Shun Chan
021     */
022    public class SafeProperties extends Properties {
023    
024            public SafeProperties() {
025                    super();
026            }
027    
028            @Override
029            public synchronized Object get(Object key) {
030                    Object value = super.get(key);
031    
032                    value = _decode((String)value);
033    
034                    return value;
035            }
036    
037            public String getEncodedProperty(String key) {
038                    return super.getProperty(key);
039            }
040    
041            @Override
042            public String getProperty(String key) {
043                    return (String)get(key);
044            }
045    
046            @Override
047            public synchronized Object put(Object key, Object value) {
048                    if (key == null) {
049                            return null;
050                    }
051    
052                    if (value == null) {
053                            return super.remove(key);
054                    }
055    
056                    value = _encode((String)value);
057    
058                    return super.put(key, value);
059            }
060    
061            @Override
062            public synchronized Object remove(Object key) {
063                    if (key == null) {
064                            return null;
065                    }
066                    else {
067                            return super.remove(key);
068                    }
069            }
070    
071            private static String _decode(String value) {
072                    return StringUtil.replace(
073                            value, _SAFE_NEWLINE_CHARACTER, StringPool.NEW_LINE);
074            }
075    
076            private static String _encode(String value) {
077                    return StringUtil.replace(
078                            value,
079                            new String[] {
080                                    StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE,
081                                    StringPool.RETURN
082                            },
083                            new String[] {
084                                    _SAFE_NEWLINE_CHARACTER, _SAFE_NEWLINE_CHARACTER,
085                                    _SAFE_NEWLINE_CHARACTER
086                            });
087            }
088    
089            private static final String _SAFE_NEWLINE_CHARACTER =
090                    "_SAFE_NEWLINE_CHARACTER_";
091    
092    }