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
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 load(String s) throws IOException {
91 Properties p = new Properties();
92
93 load(p, s);
94
95 return p;
96 }
97
98 public static void load(Properties p, String s) throws IOException {
99 if (Validator.isNotNull(s)) {
100 s = UnicodeFormatter.toString(s);
101
102 s = StringUtil.replace(s, "\\u003d", "=");
103 s = StringUtil.replace(s, "\\u000a", "\n");
104 s = StringUtil.replace(s, "\\u0021", "!");
105 s = StringUtil.replace(s, "\\u0023", "#");
106 s = StringUtil.replace(s, "\\u0020", " ");
107 s = StringUtil.replace(s, "\\u005c", "\\");
108
109 p.load(new ByteArrayInputStream(s.getBytes()));
110
111 List propertyNames = Collections.list(p.propertyNames());
112
113 for (int i = 0; i < propertyNames.size(); i++) {
114 String key = (String)propertyNames.get(i);
115
116 String value = p.getProperty(key);
117
118
122 if (value != null) {
123 value = value.trim();
124
125 p.setProperty(key, value);
126 }
127 }
128 }
129 }
130
131 public static void merge(Properties p1, Properties p2) {
132 Enumeration enu = p2.propertyNames();
133
134 while (enu.hasMoreElements()) {
135 String key = (String)enu.nextElement();
136 String value = p2.getProperty(key);
137
138 p1.setProperty(key, value);
139 }
140 }
141
142 public static String list(Map map) {
143 Properties props = fromMap(map);
144
145 ByteArrayMaker bam = new ByteArrayMaker();
146 PrintStream ps = new PrintStream(bam);
147
148 props.list(ps);
149
150 return bam.toString();
151 }
152
153 public static void list(Map map, PrintStream out) {
154 Properties props = fromMap(map);
155
156 props.list(out);
157 }
158
159 public static void list(Map map, PrintWriter out) {
160 Properties props = fromMap(map);
161
162 props.list(out);
163 }
164
165 public static String toString(Properties p) {
166 SafeProperties safeProperties = null;
167
168 if (p instanceof SafeProperties) {
169 safeProperties = (SafeProperties)p;
170 }
171
172 StringBuilder sb = new StringBuilder();
173
174 Enumeration enu = p.propertyNames();
175
176 while (enu.hasMoreElements()) {
177 String key = (String)enu.nextElement();
178
179 sb.append(key);
180 sb.append(StringPool.EQUAL);
181
182 if (safeProperties != null) {
183 sb.append(safeProperties.getEncodedProperty(key));
184 }
185 else {
186 sb.append(p.getProperty(key));
187 }
188
189 sb.append(StringPool.NEW_LINE);
190 }
191
192 return sb.toString();
193 }
194
195 public static void trimKeys(Properties p) {
196 Enumeration enu = p.propertyNames();
197
198 while (enu.hasMoreElements()) {
199 String key = (String)enu.nextElement();
200 String value = p.getProperty(key);
201
202 String trimmedKey = key.trim();
203
204 if (!key.equals(trimmedKey)) {
205 p.remove(key);
206 p.setProperty(trimmedKey, value);
207 }
208 }
209 }
210
211 }