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                    else {
052                            if (value == null) {
053                                    return super.remove(key);
054                            }
055                            else {
056                                    value = _encode((String)value);
057    
058                                    return super.put(key, value);
059                            }
060                    }
061            }
062    
063            @Override
064            public synchronized Object remove(Object key) {
065                    if (key == null) {
066                            return null;
067                    }
068                    else {
069                            return super.remove(key);
070                    }
071            }
072    
073            private static String _decode(String value) {
074                    return StringUtil.replace(
075                            value, _SAFE_NEWLINE_CHARACTER, StringPool.NEW_LINE);
076            }
077    
078            private static String _encode(String value) {
079                    return StringUtil.replace(
080                            value,
081                            new String[] {
082                                    StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE,
083                                    StringPool.RETURN
084                            },
085                            new String[] {
086                                    _SAFE_NEWLINE_CHARACTER, _SAFE_NEWLINE_CHARACTER,
087                                    _SAFE_NEWLINE_CHARACTER
088                            });
089            }
090    
091            private static final String _SAFE_NEWLINE_CHARACTER =
092                    "_SAFE_NEWLINE_CHARACTER_";
093    
094    }