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.UnsyncByteArrayInputStream;
018    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
019    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
020    import com.liferay.portal.kernel.nio.charset.CharsetDecoderUtil;
021    import com.liferay.portal.kernel.nio.charset.CharsetEncoderUtil;
022    
023    import java.io.IOException;
024    import java.io.InputStream;
025    import java.io.InputStreamReader;
026    import java.io.PrintStream;
027    import java.io.PrintWriter;
028    import java.io.Reader;
029    
030    import java.lang.reflect.Method;
031    
032    import java.nio.ByteBuffer;
033    import java.nio.CharBuffer;
034    import java.nio.charset.CharsetDecoder;
035    import java.nio.charset.CharsetEncoder;
036    
037    import java.util.Collections;
038    import java.util.Enumeration;
039    import java.util.List;
040    import java.util.Map;
041    import java.util.Properties;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     * @author Shuyang Zhou
046     */
047    public class PropertiesUtil {
048    
049            public static void copyProperties(
050                    Properties sourceProperties, Properties targetProperties) {
051    
052                    for (Map.Entry<Object, Object> entry : sourceProperties.entrySet()) {
053                            String key = (String)entry.getKey();
054                            String value = (String)entry.getValue();
055    
056                            targetProperties.setProperty(key, value);
057                    }
058            }
059    
060            public static Properties fromMap(Map<String, String> map) {
061                    Properties properties = new Properties();
062    
063                    for (Map.Entry<String, String> entry : map.entrySet()) {
064                            String key = entry.getKey();
065                            String value = entry.getValue();
066    
067                            if (value != null) {
068                                    properties.setProperty(key, value);
069                            }
070                    }
071    
072                    return properties;
073            }
074    
075            public static Properties fromMap(Properties properties) {
076                    return properties;
077            }
078    
079            public static void fromProperties(
080                    Properties properties, Map<String, String> map) {
081    
082                    map.clear();
083    
084                    for (Map.Entry<Object, Object> entry : properties.entrySet()) {
085                            map.put((String)entry.getKey(), (String)entry.getValue());
086                    }
087            }
088    
089            public static Properties getProperties(
090                    Properties properties, String prefix, boolean removePrefix) {
091    
092                    Properties newProperties = new Properties();
093    
094                    Enumeration<String> enu =
095                            (Enumeration<String>)properties.propertyNames();
096    
097                    while (enu.hasMoreElements()) {
098                            String key = enu.nextElement();
099    
100                            if (key.startsWith(prefix)) {
101                                    String value = properties.getProperty(key);
102    
103                                    if (removePrefix) {
104                                            key = key.substring(prefix.length());
105                                    }
106    
107                                    newProperties.setProperty(key, value);
108                            }
109                    }
110    
111                    return newProperties;
112            }
113    
114            public static String list(Map<String, String> map) {
115                    Properties properties = fromMap(map);
116    
117                    return list(properties);
118            }
119    
120            public static void list(Map<String, String> map, PrintStream printWriter) {
121                    Properties properties = fromMap(map);
122    
123                    properties.list(printWriter);
124            }
125    
126            public static void list(Map<String, String> map, PrintWriter printWriter) {
127                    Properties properties = fromMap(map);
128    
129                    properties.list(printWriter);
130            }
131    
132            public static String list(Properties properties) {
133                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
134                            new UnsyncByteArrayOutputStream();
135    
136                    PrintStream printStream = new PrintStream(unsyncByteArrayOutputStream);
137    
138                    properties.list(printStream);
139    
140                    return unsyncByteArrayOutputStream.toString();
141            }
142    
143            public static Properties load(InputStream is, String charsetName)
144                    throws IOException {
145    
146                    if (JavaDetector.isJDK6()) {
147                            return loadJDK6(new InputStreamReader(is, charsetName));
148                    }
149                    else {
150                            return loadJDK5(is, charsetName);
151                    }
152            }
153    
154            public static void load(Properties properties, String s)
155                    throws IOException {
156    
157                    if (Validator.isNull(s)) {
158                            return;
159                    }
160    
161                    s = UnicodeFormatter.toString(s);
162    
163                    s = StringUtil.replace(s, "\\u003d", "=");
164                    s = StringUtil.replace(s, "\\u000a", "\n");
165                    s = StringUtil.replace(s, "\\u0021", "!");
166                    s = StringUtil.replace(s, "\\u0023", "#");
167                    s = StringUtil.replace(s, "\\u0020", " ");
168                    s = StringUtil.replace(s, "\\u005c", "\\");
169    
170                    properties.load(new UnsyncByteArrayInputStream(s.getBytes()));
171    
172                    List<String> propertyNames = Collections.list(
173                            (Enumeration<String>)properties.propertyNames());
174    
175                    for (int i = 0; i < propertyNames.size(); i++) {
176                            String key = propertyNames.get(i);
177    
178                            String value = properties.getProperty(key);
179    
180                            // Trim values because it may leave a trailing \r in certain Windows
181                            // environments. This is a known case for loading SQL scripts in SQL
182                            // Server.
183    
184                            if (value != null) {
185                                    value = value.trim();
186    
187                                    properties.setProperty(key, value);
188                            }
189                    }
190            }
191    
192            public static Properties load(String s) throws IOException {
193                    return load(s, StringPool.UTF8);
194            }
195    
196            public static Properties load(String s, String charsetName)
197                    throws IOException {
198    
199                    if (JavaDetector.isJDK6()) {
200                            return loadJDK6(new UnsyncStringReader(s));
201                    }
202    
203                    ByteBuffer byteBuffer = CharsetEncoderUtil.encode(charsetName, s);
204    
205                    InputStream is = new UnsyncByteArrayInputStream(
206                            byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.limit());
207    
208                    return loadJDK5(is, charsetName);
209            }
210    
211            public static Properties loadJDK5(InputStream is, String charsetName)
212                    throws IOException {
213    
214                    Properties iso8859_1Properties = new Properties();
215    
216                    iso8859_1Properties.load(is);
217    
218                    Properties properties = new Properties();
219    
220                    CharsetEncoder charsetEncoder = CharsetEncoderUtil.getCharsetEncoder(
221                            StringPool.ISO_8859_1);
222    
223                    CharsetDecoder charsetDecoder = CharsetDecoderUtil.getCharsetDecoder(
224                            charsetName);
225    
226                    for (Map.Entry<Object, Object> entry : iso8859_1Properties.entrySet()) {
227                            String key = (String)entry.getKey();
228                            String value = (String)entry.getValue();
229    
230                            CharBuffer keyCharBuffer = charsetDecoder.decode(
231                                    charsetEncoder.encode(CharBuffer.wrap(key)));
232                            CharBuffer valueCharBuffer = charsetDecoder.decode(
233                                    charsetEncoder.encode(CharBuffer.wrap(value)));
234    
235                            properties.put(
236                                    keyCharBuffer.toString(), valueCharBuffer.toString());
237                    }
238    
239                    return properties;
240            }
241    
242            public static Properties loadJDK6(Reader reader) throws IOException {
243                    try {
244                            Properties properties = new Properties();
245    
246                            if (_jdk6LoadMethod == null) {
247                                    _jdk6LoadMethod = ReflectionUtil.getDeclaredMethod(
248                                            Properties.class, "load", Reader.class);
249                            }
250    
251                            _jdk6LoadMethod.invoke(properties, reader);
252    
253                            return properties;
254                    }
255                    catch (Exception e) {
256                            Throwable cause = e.getCause();
257    
258                            if (cause instanceof IOException) {
259                                    throw (IOException)cause;
260                            }
261    
262                            throw new IllegalStateException(
263                                    "Failed to invoke java.util.Properties.load(Reader reader)", e);
264                    }
265            }
266    
267            public static void merge(Properties properties1, Properties properties2) {
268                    Enumeration<String> enu =
269                            (Enumeration<String>)properties2.propertyNames();
270    
271                    while (enu.hasMoreElements()) {
272                            String key = enu.nextElement();
273                            String value = properties2.getProperty(key);
274    
275                            properties1.setProperty(key, value);
276                    }
277            }
278    
279            public static String toString(Properties properties) {
280                    SafeProperties safeProperties = null;
281    
282                    if (properties instanceof SafeProperties) {
283                            safeProperties = (SafeProperties)properties;
284                    }
285    
286                    StringBundler sb = null;
287    
288                    if (properties.isEmpty()) {
289                            sb = new StringBundler();
290                    }
291                    else {
292                            sb = new StringBundler(properties.size() * 4);
293                    }
294    
295                    Enumeration<String> enu =
296                            (Enumeration<String>)properties.propertyNames();
297    
298                    while (enu.hasMoreElements()) {
299                            String key = enu.nextElement();
300    
301                            sb.append(key);
302                            sb.append(StringPool.EQUAL);
303    
304                            if (safeProperties != null) {
305                                    sb.append(safeProperties.getEncodedProperty(key));
306                            }
307                            else {
308                                    sb.append(properties.getProperty(key));
309                            }
310    
311                            sb.append(StringPool.NEW_LINE);
312                    }
313    
314                    return sb.toString();
315            }
316    
317            public static void trimKeys(Properties properties) {
318                    Enumeration<String> enu =
319                            (Enumeration<String>)properties.propertyNames();
320    
321                    while (enu.hasMoreElements()) {
322                            String key = enu.nextElement();
323                            String value = properties.getProperty(key);
324    
325                            String trimmedKey = key.trim();
326    
327                            if (!key.equals(trimmedKey)) {
328                                    properties.remove(key);
329                                    properties.setProperty(trimmedKey, value);
330                            }
331                    }
332            }
333    
334            private static Method _jdk6LoadMethod;
335    
336    }