1
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
36 public class PropertiesUtil {
37
38 public static void copyProperties(Properties from, Properties to) {
39 Iterator<Map.Entry<Object, Object>> itr = from.entrySet().iterator();
40
41 while (itr.hasNext()) {
42 Map.Entry<Object, Object> entry = itr.next();
43
44 to.setProperty((String)entry.getKey(), (String)entry.getValue());
45 }
46 }
47
48 public static Properties fromMap(Map<String, String> map) {
49 Properties properties = new Properties();
50
51 Iterator<Map.Entry<String, String>> itr = map.entrySet().iterator();
52
53 while (itr.hasNext()) {
54 Map.Entry<String, String> entry = itr.next();
55
56 String key = entry.getKey();
57 String value = entry.getValue();
58
59 if (value != null) {
60 properties.setProperty(key, value);
61 }
62 }
63
64 return properties;
65 }
66
67 public static Properties fromMap(Properties properties) {
68 return properties;
69 }
70
71 public static void fromProperties(
72 Properties properties, Map<String, String> map) {
73
74 map.clear();
75
76 Iterator<Map.Entry<Object, Object>> itr =
77 properties.entrySet().iterator();
78
79 while (itr.hasNext()) {
80 Map.Entry<Object, Object> entry = itr.next();
81
82 map.put((String)entry.getKey(), (String)entry.getValue());
83 }
84 }
85
86 public static Properties getProperties(
87 Properties properties, String prefix, boolean removePrefix) {
88
89 Properties subProperties = new Properties();
90
91 Enumeration<String> enu =
92 (Enumeration<String>)properties.propertyNames();
93
94 while (enu.hasMoreElements()) {
95 String key = enu.nextElement();
96
97 if (key.startsWith(prefix)) {
98 String value = properties.getProperty(key);
99
100 if (removePrefix) {
101 key = key.substring(prefix.length());
102 }
103
104 subProperties.setProperty(key, value);
105 }
106 }
107
108 return subProperties;
109 }
110
111 public static String list(Map<String, String> map) {
112 Properties properties = fromMap(map);
113
114 return list(properties);
115 }
116
117 public static void list(Map<String, String> map, PrintStream out) {
118 Properties properties = fromMap(map);
119
120 properties.list(out);
121 }
122
123 public static void list(Map<String, String> map, PrintWriter out) {
124 Properties properties = fromMap(map);
125
126 properties.list(out);
127 }
128
129 public static String list(Properties properties) {
130 UnsyncByteArrayOutputStream ubaos = new UnsyncByteArrayOutputStream();
131 PrintStream ps = new PrintStream(ubaos);
132
133 properties.list(ps);
134
135 return ubaos.toString();
136 }
137
138 public static void load(Properties p, String s) throws IOException {
139 if (Validator.isNotNull(s)) {
140 s = UnicodeFormatter.toString(s);
141
142 s = StringUtil.replace(s, "\\u003d", "=");
143 s = StringUtil.replace(s, "\\u000a", "\n");
144 s = StringUtil.replace(s, "\\u0021", "!");
145 s = StringUtil.replace(s, "\\u0023", "#");
146 s = StringUtil.replace(s, "\\u0020", " ");
147 s = StringUtil.replace(s, "\\u005c", "\\");
148
149 p.load(new UnsyncByteArrayInputStream(s.getBytes()));
150
151 List<String> propertyNames = Collections.list(
152 (Enumeration<String>)p.propertyNames());
153
154 for (int i = 0; i < propertyNames.size(); i++) {
155 String key = propertyNames.get(i);
156
157 String value = p.getProperty(key);
158
159
163 if (value != null) {
164 value = value.trim();
165
166 p.setProperty(key, value);
167 }
168 }
169 }
170 }
171
172 public static Properties load(String s) throws IOException {
173 Properties p = new Properties();
174
175 load(p, s);
176
177 return p;
178 }
179
180 public static void merge(Properties p1, Properties p2) {
181 Enumeration<String> enu = (Enumeration<String>)p2.propertyNames();
182
183 while (enu.hasMoreElements()) {
184 String key = enu.nextElement();
185 String value = p2.getProperty(key);
186
187 p1.setProperty(key, value);
188 }
189 }
190
191 public static String toString(Properties p) {
192 SafeProperties safeProperties = null;
193
194 if (p instanceof SafeProperties) {
195 safeProperties = (SafeProperties)p;
196 }
197
198 StringBundler sb = null;
199
200 if (p.isEmpty()) {
201 sb = new StringBundler();
202 }
203 else {
204 sb = new StringBundler(p.size() * 4);
205 }
206
207 Enumeration<String> enu = (Enumeration<String>)p.propertyNames();
208
209 while (enu.hasMoreElements()) {
210 String key = enu.nextElement();
211
212 sb.append(key);
213 sb.append(StringPool.EQUAL);
214
215 if (safeProperties != null) {
216 sb.append(safeProperties.getEncodedProperty(key));
217 }
218 else {
219 sb.append(p.getProperty(key));
220 }
221
222 sb.append(StringPool.NEW_LINE);
223 }
224
225 return sb.toString();
226 }
227
228 public static void trimKeys(Properties p) {
229 Enumeration<String> enu = (Enumeration<String>)p.propertyNames();
230
231 while (enu.hasMoreElements()) {
232 String key = enu.nextElement();
233 String value = p.getProperty(key);
234
235 String trimmedKey = key.trim();
236
237 if (!key.equals(trimmedKey)) {
238 p.remove(key);
239 p.setProperty(trimmedKey, value);
240 }
241 }
242 }
243
244 }