001    /**
002     * Copyright (c) 2000-2010 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            public synchronized Object get(Object key) {
029                    Object value = super.get(key);
030    
031                    value = _decode((String)value);
032    
033                    return value;
034            }
035    
036            public String getEncodedProperty(String key) {
037                    return super.getProperty(key);
038            }
039    
040            public String getProperty(String key) {
041                    return (String)get(key);
042            }
043    
044            public synchronized Object put(Object key, Object value) {
045                    if (key == null) {
046                            return null;
047                    }
048                    else {
049                            if (value == null) {
050                                    return super.remove(key);
051                            }
052                            else {
053                                    value = _encode((String)value);
054    
055                                    return super.put(key, value);
056                            }
057                    }
058            }
059    
060            public synchronized Object remove(Object key) {
061                    if (key == null) {
062                            return null;
063                    }
064                    else {
065                            return super.remove(key);
066                    }
067            }
068    
069            private static String _decode(String value) {
070                    return StringUtil.replace(
071                            value, _SAFE_NEWLINE_CHARACTER, StringPool.NEW_LINE);
072            }
073    
074            private static String _encode(String value) {
075                    return StringUtil.replace(
076                            value,
077                            new String[] {
078                                    StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE,
079                                    StringPool.RETURN
080                            },
081                            new String[] {
082                                    _SAFE_NEWLINE_CHARACTER, _SAFE_NEWLINE_CHARACTER,
083                                    _SAFE_NEWLINE_CHARACTER
084                            });
085            }
086    
087            private static final String _SAFE_NEWLINE_CHARACTER =
088                    "_SAFE_NEWLINE_CHARACTER_";
089    
090    }