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