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