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 com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
018    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    
022    import java.io.IOException;
023    
024    import java.util.HashMap;
025    import java.util.Map;
026    
027    /**
028     * <p>
029     * This is a rewrite of java.util.Properties that is not synchronized and
030     * natively supports non-ASCII encodings. It can also be configured to be
031     * "safe", allowing the values to have new line characters. When stored to a
032     * given BufferedWriter, "safe" properties will replace all new line characters
033     * with a _SAFE_NEWLINE_CHARACTER_.
034     * </p>
035     *
036     * <p>
037     * In its current form, this is not intended to replace java.util.Properties for
038     * reading properties flat files. This class is not thread-safe.
039     * </p>
040     *
041     * @author Alexander Chow
042     */
043    public class UnicodeProperties extends HashMap<String, String> {
044    
045            public UnicodeProperties() {
046                    super();
047            }
048    
049            public UnicodeProperties(boolean safe) {
050                    super();
051    
052                    _safe = safe;
053            }
054    
055            public void fastLoad(String props) {
056                    if (Validator.isNull(props)) {
057                            return;
058                    }
059    
060                    int x = props.indexOf(CharPool.NEW_LINE);
061                    int y = 0;
062    
063                    while (x != -1) {
064                            put(props.substring(y, x));
065    
066                            y = x;
067    
068                            x = props.indexOf(CharPool.NEW_LINE, y + 1);
069                    }
070    
071                    put(props.substring(y));
072            }
073    
074            public String getProperty(String key) {
075                    return get(key);
076            }
077    
078            public String getProperty(String key, String defaultValue) {
079                    if (containsKey(key)) {
080                            return getProperty(key);
081                    }
082                    else {
083                            return defaultValue;
084                    }
085            }
086    
087            public boolean isSafe() {
088                    return _safe;
089            }
090    
091            public void load(String props) throws IOException {
092                    if (Validator.isNull(props)) {
093                            return;
094                    }
095    
096                    UnsyncBufferedReader unsyncBufferedReader = null;
097    
098                    try {
099                            unsyncBufferedReader = new UnsyncBufferedReader(
100                                    new UnsyncStringReader(props));
101    
102                            String line = unsyncBufferedReader.readLine();
103    
104                            while (line != null) {
105                                    put(line);
106                                    line = unsyncBufferedReader.readLine();
107                            }
108                    }
109                    finally {
110                            if (unsyncBufferedReader != null) {
111                                    try {
112                                            unsyncBufferedReader.close();
113                                    }
114                                    catch (Exception e) {
115                                    }
116                            }
117                    }
118            }
119    
120            private void put(String line) {
121                    line = line.trim();
122    
123                    if (!_isComment(line)) {
124                            int pos = line.indexOf(StringPool.EQUAL);
125    
126                            if (pos != -1) {
127                                    String key = line.substring(0, pos).trim();
128                                    String value = line.substring(pos + 1).trim();
129    
130                                    if (_safe) {
131                                            value = _decode(value);
132                                    }
133    
134                                    setProperty(key, value);
135                            }
136                            else {
137                                    _log.error("Invalid property on line " + line);
138                            }
139                    }
140            }
141    
142            public String put(String key, String value) {
143                    if (key == null) {
144                            return null;
145                    }
146                    else {
147                            if (value == null) {
148                                    return remove(key);
149                            }
150                            else {
151                                    _length += key.length() + value.length() + 2;
152    
153                                    return super.put(key, value);
154                            }
155                    }
156            }
157    
158            public String remove(Object key) {
159                    if ((key == null) || !containsKey(key)) {
160                            return null;
161                    }
162                    else {
163                            String keyString = (String)key;
164    
165                            String value = super.remove(key);
166    
167                            _length -= keyString.length() + value.length() + 2;
168    
169                            return value;
170                    }
171            }
172    
173            public String setProperty(String key, String value) {
174                    return put(key, value);
175            }
176    
177            public String toString() {
178                    StringBuilder sb = new StringBuilder(_length);
179    
180                    for (Map.Entry<String, String> entry : entrySet()) {
181                            String value = entry.getValue();
182    
183                            if (Validator.isNotNull(value)) {
184                                    if (_safe) {
185                                            value = _encode(value);
186                                    }
187    
188                                    sb.append(entry.getKey());
189                                    sb.append(StringPool.EQUAL);
190                                    sb.append(value);
191                                    sb.append(StringPool.NEW_LINE);
192                            }
193                    }
194    
195                    return sb.toString();
196            }
197    
198            protected int getToStringLength() {
199                    return _length;
200            }
201    
202            private static String _decode(String value) {
203                    return StringUtil.replace(
204                            value, _SAFE_NEWLINE_CHARACTER, StringPool.NEW_LINE);
205            }
206    
207            private static String _encode(String value) {
208                    return StringUtil.replace(
209                            value,
210                            new String[] {
211                                    StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE,
212                                    StringPool.RETURN
213                            },
214                            new String[] {
215                                    _SAFE_NEWLINE_CHARACTER, _SAFE_NEWLINE_CHARACTER,
216                                    _SAFE_NEWLINE_CHARACTER
217                            });
218            }
219    
220            private boolean _isComment(String line) {
221                    return line.length() == 0 || line.startsWith(StringPool.POUND);
222            }
223    
224            private static final String _SAFE_NEWLINE_CHARACTER =
225                    "_SAFE_NEWLINE_CHARACTER_";
226    
227            private static Log _log = LogFactoryUtil.getLog(UnicodeProperties.class);
228    
229            private boolean _safe = false;
230            private int _length;
231    
232    }