001    /**
002     * Copyright (c) 2000-2012 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.isNotNull(s)) {
158                            s = UnicodeFormatter.toString(s);
159    
160                            s = StringUtil.replace(s, "\\u003d", "=");
161                            s = StringUtil.replace(s, "\\u000a", "\n");
162                            s = StringUtil.replace(s, "\\u0021", "!");
163                            s = StringUtil.replace(s, "\\u0023", "#");
164                            s = StringUtil.replace(s, "\\u0020", " ");
165                            s = StringUtil.replace(s, "\\u005c", "\\");
166    
167                            properties.load(new UnsyncByteArrayInputStream(s.getBytes()));
168    
169                            List<String> propertyNames = Collections.list(
170                                    (Enumeration<String>)properties.propertyNames());
171    
172                            for (int i = 0; i < propertyNames.size(); i++) {
173                                    String key = propertyNames.get(i);
174    
175                                    String value = properties.getProperty(key);
176    
177                                    // Trim values because it may leave a trailing \r in certain
178                                    // Windows environments. This is a known case for loading SQL
179                                    // scripts in SQL Server.
180    
181                                    if (value != null) {
182                                            value = value.trim();
183    
184                                            properties.setProperty(key, value);
185                                    }
186                            }
187                    }
188            }
189    
190            public static Properties load(String s) throws IOException {
191                    return load(s, StringPool.UTF8);
192            }
193    
194            public static Properties load(String s, String charsetName)
195                    throws IOException {
196    
197                    if (JavaDetector.isJDK6()) {
198                            return loadJDK6(new UnsyncStringReader(s));
199                    }
200                    else {
201                            ByteBuffer byteBuffer = CharsetEncoderUtil.encode(charsetName, s);
202    
203                            InputStream is = new UnsyncByteArrayInputStream(
204                                    byteBuffer.array(), byteBuffer.arrayOffset(),
205                                    byteBuffer.limit());
206    
207                            return loadJDK5(is, charsetName);
208                    }
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                            key = charsetDecoder.decode(
231                                    charsetEncoder.encode(CharBuffer.wrap(key))).toString();
232                            value = charsetDecoder.decode(
233                                    charsetEncoder.encode(CharBuffer.wrap(value))).toString();
234    
235                            properties.put(key, value);
236                    }
237    
238                    return properties;
239            }
240    
241            public static Properties loadJDK6(Reader reader) throws IOException {
242                    try {
243                            Properties properties = new Properties();
244    
245                            if (_jdk6LoadMethod == null) {
246                                    _jdk6LoadMethod = ReflectionUtil.getDeclaredMethod(
247                                            Properties.class, "load", Reader.class);
248                            }
249    
250                            _jdk6LoadMethod.invoke(properties, reader);
251    
252                            return properties;
253                    }
254                    catch (Exception e) {
255                            Throwable cause = e.getCause();
256    
257                            if (cause instanceof IOException) {
258                                    throw (IOException)cause;
259                            }
260    
261                            throw new IllegalStateException(
262                                    "Failed to invoke java.util.Properties.load(Reader reader)", e);
263                    }
264            }
265    
266            public static void merge(Properties properties1, Properties properties2) {
267                    Enumeration<String> enu =
268                            (Enumeration<String>)properties2.propertyNames();
269    
270                    while (enu.hasMoreElements()) {
271                            String key = enu.nextElement();
272                            String value = properties2.getProperty(key);
273    
274                            properties1.setProperty(key, value);
275                    }
276            }
277    
278            public static String toString(Properties properties) {
279                    SafeProperties safeProperties = null;
280    
281                    if (properties instanceof SafeProperties) {
282                            safeProperties = (SafeProperties)properties;
283                    }
284    
285                    StringBundler sb = null;
286    
287                    if (properties.isEmpty()) {
288                            sb = new StringBundler();
289                    }
290                    else {
291                            sb = new StringBundler(properties.size() * 4);
292                    }
293    
294                    Enumeration<String> enu =
295                            (Enumeration<String>)properties.propertyNames();
296    
297                    while (enu.hasMoreElements()) {
298                            String key = enu.nextElement();
299    
300                            sb.append(key);
301                            sb.append(StringPool.EQUAL);
302    
303                            if (safeProperties != null) {
304                                    sb.append(safeProperties.getEncodedProperty(key));
305                            }
306                            else {
307                                    sb.append(properties.getProperty(key));
308                            }
309    
310                            sb.append(StringPool.NEW_LINE);
311                    }
312    
313                    return sb.toString();
314            }
315    
316            public static void trimKeys(Properties properties) {
317                    Enumeration<String> enu =
318                            (Enumeration<String>)properties.propertyNames();
319    
320                    while (enu.hasMoreElements()) {
321                            String key = enu.nextElement();
322                            String value = properties.getProperty(key);
323    
324                            String trimmedKey = key.trim();
325    
326                            if (!key.equals(trimmedKey)) {
327                                    properties.remove(key);
328                                    properties.setProperty(trimmedKey, value);
329                            }
330                    }
331            }
332    
333            private static Method _jdk6LoadMethod;
334    
335    }