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