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