001    /**
002     * Copyright (c) 2000-present 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    
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.io.InputStreamReader;
024    import java.io.PrintStream;
025    import java.io.PrintWriter;
026    import java.io.Reader;
027    
028    import java.util.Collections;
029    import java.util.Enumeration;
030    import java.util.HashMap;
031    import java.util.List;
032    import java.util.Map;
033    import java.util.Properties;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @author Shuyang Zhou
038     */
039    public class PropertiesUtil {
040    
041            public static void copyProperties(
042                    Properties sourceProperties, Properties targetProperties) {
043    
044                    for (Map.Entry<Object, Object> entry : sourceProperties.entrySet()) {
045                            String key = (String)entry.getKey();
046                            String value = (String)entry.getValue();
047    
048                            targetProperties.setProperty(key, value);
049                    }
050            }
051    
052            public static Properties fromMap(Map<String, ?> map) {
053                    Properties properties = new Properties();
054    
055                    for (Map.Entry<String, ?> entry : map.entrySet()) {
056                            String key = entry.getKey();
057                            Object value = entry.getValue();
058    
059                            if ((value != null) && (value instanceof String)) {
060                                    properties.setProperty(key, (String)value);
061                            }
062                    }
063    
064                    return properties;
065            }
066    
067            public static void fromProperties(
068                    Properties properties, Map<String, String> map) {
069    
070                    map.clear();
071    
072                    for (Map.Entry<Object, Object> entry : properties.entrySet()) {
073                            map.put((String)entry.getKey(), (String)entry.getValue());
074                    }
075            }
076    
077            public static Properties getProperties(
078                    Properties properties, String prefix, boolean removePrefix) {
079    
080                    Properties newProperties = new Properties();
081    
082                    Enumeration<String> enu =
083                            (Enumeration<String>)properties.propertyNames();
084    
085                    while (enu.hasMoreElements()) {
086                            String key = enu.nextElement();
087    
088                            if (key.startsWith(prefix)) {
089                                    String value = properties.getProperty(key);
090    
091                                    if (removePrefix) {
092                                            key = key.substring(prefix.length());
093                                    }
094    
095                                    newProperties.setProperty(key, value);
096                            }
097                    }
098    
099                    return newProperties;
100            }
101    
102            public static String list(Map<String, String> map) {
103                    Properties properties = fromMap(map);
104    
105                    return list(properties);
106            }
107    
108            public static void list(Map<String, String> map, PrintStream printWriter) {
109                    Properties properties = fromMap(map);
110    
111                    properties.list(printWriter);
112            }
113    
114            public static void list(Map<String, String> map, PrintWriter printWriter) {
115                    Properties properties = fromMap(map);
116    
117                    properties.list(printWriter);
118            }
119    
120            public static String list(Properties properties) {
121                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
122                            new UnsyncByteArrayOutputStream();
123    
124                    PrintStream printStream = new PrintStream(unsyncByteArrayOutputStream);
125    
126                    properties.list(printStream);
127    
128                    return unsyncByteArrayOutputStream.toString();
129            }
130    
131            public static Properties load(InputStream is, String charsetName)
132                    throws IOException {
133    
134                    return load(new InputStreamReader(is, charsetName));
135            }
136    
137            public static void load(Properties properties, String s)
138                    throws IOException {
139    
140                    if (Validator.isNull(s)) {
141                            return;
142                    }
143    
144                    s = UnicodeFormatter.toString(s);
145    
146                    s = StringUtil.replace(s, "\\u003d", "=");
147                    s = StringUtil.replace(s, "\\u000a", "\n");
148                    s = StringUtil.replace(s, "\\u0021", "!");
149                    s = StringUtil.replace(s, "\\u0023", "#");
150                    s = StringUtil.replace(s, "\\u0020", " ");
151                    s = StringUtil.replace(s, "\\u005c", "\\");
152    
153                    properties.load(new UnsyncByteArrayInputStream(s.getBytes()));
154    
155                    List<String> propertyNames = Collections.list(
156                            (Enumeration<String>)properties.propertyNames());
157    
158                    for (int i = 0; i < propertyNames.size(); i++) {
159                            String key = propertyNames.get(i);
160    
161                            String value = properties.getProperty(key);
162    
163                            // Trim values because it may leave a trailing \r in certain Windows
164                            // environments. This is a known case for loading SQL scripts in SQL
165                            // Server.
166    
167                            if (value != null) {
168                                    value = value.trim();
169    
170                                    properties.setProperty(key, value);
171                            }
172                    }
173            }
174    
175            public static Properties load(Reader reader) throws IOException {
176                    Properties properties = new Properties();
177    
178                    properties.load(reader);
179    
180                    return properties;
181            }
182    
183            public static Properties load(String s) throws IOException {
184                    return load(new UnsyncStringReader(s));
185            }
186    
187            public static void merge(Properties properties1, Properties properties2) {
188                    Enumeration<String> enu =
189                            (Enumeration<String>)properties2.propertyNames();
190    
191                    while (enu.hasMoreElements()) {
192                            String key = enu.nextElement();
193                            String value = properties2.getProperty(key);
194    
195                            properties1.setProperty(key, value);
196                    }
197            }
198    
199            @SuppressWarnings("rawtypes")
200            public static Map toMap(Properties properties) {
201                    Map<String, String> propertiesMap = new HashMap<>();
202    
203                    Enumeration<?> enumeration = properties.propertyNames();
204    
205                    while (enumeration.hasMoreElements()) {
206                            String key = (String)enumeration.nextElement();
207                            String value = properties.getProperty(key);
208    
209                            propertiesMap.put(key, value);
210                    }
211    
212                    return propertiesMap;
213            }
214    
215            public static String toString(Properties properties) {
216                    SafeProperties safeProperties = null;
217    
218                    if (properties instanceof SafeProperties) {
219                            safeProperties = (SafeProperties)properties;
220                    }
221    
222                    StringBundler sb = null;
223    
224                    if (properties.isEmpty()) {
225                            sb = new StringBundler();
226                    }
227                    else {
228                            sb = new StringBundler(properties.size() * 4);
229                    }
230    
231                    Enumeration<String> enu =
232                            (Enumeration<String>)properties.propertyNames();
233    
234                    while (enu.hasMoreElements()) {
235                            String key = enu.nextElement();
236    
237                            sb.append(key);
238                            sb.append(StringPool.EQUAL);
239    
240                            if (safeProperties != null) {
241                                    sb.append(safeProperties.getEncodedProperty(key));
242                            }
243                            else {
244                                    sb.append(properties.getProperty(key));
245                            }
246    
247                            sb.append(StringPool.NEW_LINE);
248                    }
249    
250                    return sb.toString();
251            }
252    
253            public static void trimKeys(Properties properties) {
254                    Enumeration<String> enu =
255                            (Enumeration<String>)properties.propertyNames();
256    
257                    while (enu.hasMoreElements()) {
258                            String key = enu.nextElement();
259                            String value = properties.getProperty(key);
260    
261                            String trimmedKey = key.trim();
262    
263                            if (!key.equals(trimmedKey)) {
264                                    properties.remove(key);
265                                    properties.setProperty(trimmedKey, value);
266                            }
267                    }
268            }
269    
270    }