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