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