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