1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
18  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
19  
20  import java.io.IOException;
21  import java.io.PrintStream;
22  import java.io.PrintWriter;
23  
24  import java.util.Collections;
25  import java.util.Enumeration;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Properties;
30  
31  /**
32   * <a href="PropertiesUtil.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class PropertiesUtil {
37  
38      public static void copyProperties(
39          Properties sourceProperties, Properties targetProperties) {
40  
41          for (Map.Entry<Object, Object> entry : sourceProperties.entrySet()) {
42              String key = (String)entry.getKey();
43              String value = (String)entry.getValue();
44  
45              targetProperties.setProperty(key, value);
46          }
47      }
48  
49      public static Properties fromMap(Map<String, String> map) {
50          Properties properties = new Properties();
51  
52          Iterator<Map.Entry<String, String>> itr = map.entrySet().iterator();
53  
54          while (itr.hasNext()) {
55              Map.Entry<String, String> entry = itr.next();
56  
57              String key = entry.getKey();
58              String value = entry.getValue();
59  
60              if (value != null) {
61                  properties.setProperty(key, value);
62              }
63          }
64  
65          return properties;
66      }
67  
68      public static Properties fromMap(Properties properties) {
69          return properties;
70      }
71  
72      public static void fromProperties(
73          Properties properties, Map<String, String> map) {
74  
75          map.clear();
76  
77          Iterator<Map.Entry<Object, Object>> itr =
78              properties.entrySet().iterator();
79  
80          while (itr.hasNext()) {
81              Map.Entry<Object, Object> entry = itr.next();
82  
83              map.put((String)entry.getKey(), (String)entry.getValue());
84          }
85      }
86  
87      public static Properties getProperties(
88          Properties properties, String prefix, boolean removePrefix) {
89  
90          Properties subProperties = new Properties();
91  
92          Enumeration<String> enu =
93              (Enumeration<String>)properties.propertyNames();
94  
95          while (enu.hasMoreElements()) {
96              String key = enu.nextElement();
97  
98              if (key.startsWith(prefix)) {
99                  String value = properties.getProperty(key);
100 
101                 if (removePrefix) {
102                     key = key.substring(prefix.length());
103                 }
104 
105                 subProperties.setProperty(key, value);
106             }
107         }
108 
109         return subProperties;
110     }
111 
112     public static String list(Map<String, String> map) {
113         Properties properties = fromMap(map);
114 
115         return list(properties);
116     }
117 
118     public static void list(Map<String, String> map, PrintStream printWriter) {
119         Properties properties = fromMap(map);
120 
121         properties.list(printWriter);
122     }
123 
124     public static void list(Map<String, String> map, PrintWriter printWriter) {
125         Properties properties = fromMap(map);
126 
127         properties.list(printWriter);
128     }
129 
130     public static String list(Properties properties) {
131         UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
132             new UnsyncByteArrayOutputStream();
133 
134         PrintStream printStream = new PrintStream(unsyncByteArrayOutputStream);
135 
136         properties.list(printStream);
137 
138         return unsyncByteArrayOutputStream.toString();
139     }
140 
141     public static void load(Properties properties, String s)
142         throws IOException {
143 
144         if (Validator.isNotNull(s)) {
145             s = UnicodeFormatter.toString(s);
146 
147             s = StringUtil.replace(s, "\\u003d", "=");
148             s = StringUtil.replace(s, "\\u000a", "\n");
149             s = StringUtil.replace(s, "\\u0021", "!");
150             s = StringUtil.replace(s, "\\u0023", "#");
151             s = StringUtil.replace(s, "\\u0020", " ");
152             s = StringUtil.replace(s, "\\u005c", "\\");
153 
154             properties.load(new UnsyncByteArrayInputStream(s.getBytes()));
155 
156             List<String> propertyNames = Collections.list(
157                 (Enumeration<String>)properties.propertyNames());
158 
159             for (int i = 0; i < propertyNames.size(); i++) {
160                 String key = propertyNames.get(i);
161 
162                 String value = properties.getProperty(key);
163 
164                 // Trim values because it may leave a trailing \r in certain
165                 // Windows environments. This is a known case for loading SQL
166                 // scripts in SQL Server.
167 
168                 if (value != null) {
169                     value = value.trim();
170 
171                     properties.setProperty(key, value);
172                 }
173             }
174         }
175     }
176 
177     public static Properties load(String s) throws IOException {
178         Properties p = new Properties();
179 
180         load(p, s);
181 
182         return p;
183     }
184 
185     public static void merge(Properties properties1, Properties properties2) {
186         Enumeration<String> enu =
187             (Enumeration<String>)properties2.propertyNames();
188 
189         while (enu.hasMoreElements()) {
190             String key = enu.nextElement();
191             String value = properties2.getProperty(key);
192 
193             properties1.setProperty(key, value);
194         }
195     }
196 
197     public static String toString(Properties properties) {
198         SafeProperties safeProperties = null;
199 
200         if (properties instanceof SafeProperties) {
201             safeProperties = (SafeProperties)properties;
202         }
203 
204         StringBundler sb = null;
205 
206         if (properties.isEmpty()) {
207             sb = new StringBundler();
208         }
209         else {
210             sb = new StringBundler(properties.size() * 4);
211         }
212 
213         Enumeration<String> enu =
214             (Enumeration<String>)properties.propertyNames();
215 
216         while (enu.hasMoreElements()) {
217             String key = enu.nextElement();
218 
219             sb.append(key);
220             sb.append(StringPool.EQUAL);
221 
222             if (safeProperties != null) {
223                 sb.append(safeProperties.getEncodedProperty(key));
224             }
225             else {
226                 sb.append(properties.getProperty(key));
227             }
228 
229             sb.append(StringPool.NEW_LINE);
230         }
231 
232         return sb.toString();
233     }
234 
235     public static void trimKeys(Properties properties) {
236         Enumeration<String> enu =
237             (Enumeration<String>)properties.propertyNames();
238 
239         while (enu.hasMoreElements()) {
240             String key = enu.nextElement();
241             String value = properties.getProperty(key);
242 
243             String trimmedKey = key.trim();
244 
245             if (!key.equals(trimmedKey)) {
246                 properties.remove(key);
247                 properties.setProperty(trimmedKey, value);
248             }
249         }
250     }
251 
252 }