001
014
015 package com.liferay.portal.kernel.util;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
018 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
019 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
020 import com.liferay.portal.kernel.nio.charset.CharsetDecoderUtil;
021 import com.liferay.portal.kernel.nio.charset.CharsetEncoderUtil;
022
023 import java.io.IOException;
024 import java.io.InputStream;
025 import java.io.InputStreamReader;
026 import java.io.PrintStream;
027 import java.io.PrintWriter;
028 import java.io.Reader;
029
030 import java.lang.reflect.Method;
031
032 import java.nio.ByteBuffer;
033 import java.nio.CharBuffer;
034 import java.nio.charset.CharsetDecoder;
035 import java.nio.charset.CharsetEncoder;
036
037 import java.util.Collections;
038 import java.util.Enumeration;
039 import java.util.List;
040 import java.util.Map;
041 import java.util.Properties;
042
043
047 public class PropertiesUtil {
048
049 public static void copyProperties(
050 Properties sourceProperties, Properties targetProperties) {
051
052 for (Map.Entry<Object, Object> entry : sourceProperties.entrySet()) {
053 String key = (String)entry.getKey();
054 String value = (String)entry.getValue();
055
056 targetProperties.setProperty(key, value);
057 }
058 }
059
060 public static Properties fromMap(Map<String, String> map) {
061 Properties properties = new Properties();
062
063 for (Map.Entry<String, String> entry : map.entrySet()) {
064 String key = entry.getKey();
065 String value = entry.getValue();
066
067 if (value != null) {
068 properties.setProperty(key, value);
069 }
070 }
071
072 return properties;
073 }
074
075 public static Properties fromMap(Properties properties) {
076 return properties;
077 }
078
079 public static void fromProperties(
080 Properties properties, Map<String, String> map) {
081
082 map.clear();
083
084 for (Map.Entry<Object, Object> entry : properties.entrySet()) {
085 map.put((String)entry.getKey(), (String)entry.getValue());
086 }
087 }
088
089 public static Properties getProperties(
090 Properties properties, String prefix, boolean removePrefix) {
091
092 Properties newProperties = new Properties();
093
094 Enumeration<String> enu =
095 (Enumeration<String>)properties.propertyNames();
096
097 while (enu.hasMoreElements()) {
098 String key = enu.nextElement();
099
100 if (key.startsWith(prefix)) {
101 String value = properties.getProperty(key);
102
103 if (removePrefix) {
104 key = key.substring(prefix.length());
105 }
106
107 newProperties.setProperty(key, value);
108 }
109 }
110
111 return newProperties;
112 }
113
114 public static String list(Map<String, String> map) {
115 Properties properties = fromMap(map);
116
117 return list(properties);
118 }
119
120 public static void list(Map<String, String> map, PrintStream printWriter) {
121 Properties properties = fromMap(map);
122
123 properties.list(printWriter);
124 }
125
126 public static void list(Map<String, String> map, PrintWriter printWriter) {
127 Properties properties = fromMap(map);
128
129 properties.list(printWriter);
130 }
131
132 public static String list(Properties properties) {
133 UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
134 new UnsyncByteArrayOutputStream();
135
136 PrintStream printStream = new PrintStream(unsyncByteArrayOutputStream);
137
138 properties.list(printStream);
139
140 return unsyncByteArrayOutputStream.toString();
141 }
142
143 public static Properties load(InputStream is, String charsetName)
144 throws IOException {
145
146 if (JavaDetector.isJDK6()) {
147 return loadJDK6(new InputStreamReader(is, charsetName));
148 }
149 else {
150 return loadJDK5(is, charsetName);
151 }
152 }
153
154 public static void load(Properties properties, String s)
155 throws IOException {
156
157 if (Validator.isNull(s)) {
158 return;
159 }
160
161 s = UnicodeFormatter.toString(s);
162
163 s = StringUtil.replace(s, "\\u003d", "=");
164 s = StringUtil.replace(s, "\\u000a", "\n");
165 s = StringUtil.replace(s, "\\u0021", "!");
166 s = StringUtil.replace(s, "\\u0023", "#");
167 s = StringUtil.replace(s, "\\u0020", " ");
168 s = StringUtil.replace(s, "\\u005c", "\\");
169
170 properties.load(new UnsyncByteArrayInputStream(s.getBytes()));
171
172 List<String> propertyNames = Collections.list(
173 (Enumeration<String>)properties.propertyNames());
174
175 for (int i = 0; i < propertyNames.size(); i++) {
176 String key = propertyNames.get(i);
177
178 String value = properties.getProperty(key);
179
180
181
182
183
184 if (value != null) {
185 value = value.trim();
186
187 properties.setProperty(key, value);
188 }
189 }
190 }
191
192 public static Properties load(String s) throws IOException {
193 return load(s, StringPool.UTF8);
194 }
195
196 public static Properties load(String s, String charsetName)
197 throws IOException {
198
199 if (JavaDetector.isJDK6()) {
200 return loadJDK6(new UnsyncStringReader(s));
201 }
202 else {
203 ByteBuffer byteBuffer = CharsetEncoderUtil.encode(charsetName, s);
204
205 InputStream is = new UnsyncByteArrayInputStream(
206 byteBuffer.array(), byteBuffer.arrayOffset(),
207 byteBuffer.limit());
208
209 return loadJDK5(is, charsetName);
210 }
211 }
212
213 public static Properties loadJDK5(InputStream is, String charsetName)
214 throws IOException {
215
216 Properties iso8859_1Properties = new Properties();
217
218 iso8859_1Properties.load(is);
219
220 Properties properties = new Properties();
221
222 CharsetEncoder charsetEncoder = CharsetEncoderUtil.getCharsetEncoder(
223 StringPool.ISO_8859_1);
224
225 CharsetDecoder charsetDecoder = CharsetDecoderUtil.getCharsetDecoder(
226 charsetName);
227
228 for (Map.Entry<Object, Object> entry : iso8859_1Properties.entrySet()) {
229 String key = (String)entry.getKey();
230 String value = (String)entry.getValue();
231
232 key = charsetDecoder.decode(
233 charsetEncoder.encode(CharBuffer.wrap(key))).toString();
234 value = charsetDecoder.decode(
235 charsetEncoder.encode(CharBuffer.wrap(value))).toString();
236
237 properties.put(key, value);
238 }
239
240 return properties;
241 }
242
243 public static Properties loadJDK6(Reader reader) throws IOException {
244 try {
245 Properties properties = new Properties();
246
247 if (_jdk6LoadMethod == null) {
248 _jdk6LoadMethod = ReflectionUtil.getDeclaredMethod(
249 Properties.class, "load", Reader.class);
250 }
251
252 _jdk6LoadMethod.invoke(properties, reader);
253
254 return properties;
255 }
256 catch (Exception e) {
257 Throwable cause = e.getCause();
258
259 if (cause instanceof IOException) {
260 throw (IOException)cause;
261 }
262
263 throw new IllegalStateException(
264 "Failed to invoke java.util.Properties.load(Reader reader)", e);
265 }
266 }
267
268 public static void merge(Properties properties1, Properties properties2) {
269 Enumeration<String> enu =
270 (Enumeration<String>)properties2.propertyNames();
271
272 while (enu.hasMoreElements()) {
273 String key = enu.nextElement();
274 String value = properties2.getProperty(key);
275
276 properties1.setProperty(key, value);
277 }
278 }
279
280 public static String toString(Properties properties) {
281 SafeProperties safeProperties = null;
282
283 if (properties instanceof SafeProperties) {
284 safeProperties = (SafeProperties)properties;
285 }
286
287 StringBundler sb = null;
288
289 if (properties.isEmpty()) {
290 sb = new StringBundler();
291 }
292 else {
293 sb = new StringBundler(properties.size() * 4);
294 }
295
296 Enumeration<String> enu =
297 (Enumeration<String>)properties.propertyNames();
298
299 while (enu.hasMoreElements()) {
300 String key = enu.nextElement();
301
302 sb.append(key);
303 sb.append(StringPool.EQUAL);
304
305 if (safeProperties != null) {
306 sb.append(safeProperties.getEncodedProperty(key));
307 }
308 else {
309 sb.append(properties.getProperty(key));
310 }
311
312 sb.append(StringPool.NEW_LINE);
313 }
314
315 return sb.toString();
316 }
317
318 public static void trimKeys(Properties properties) {
319 Enumeration<String> enu =
320 (Enumeration<String>)properties.propertyNames();
321
322 while (enu.hasMoreElements()) {
323 String key = enu.nextElement();
324 String value = properties.getProperty(key);
325
326 String trimmedKey = key.trim();
327
328 if (!key.equals(trimmedKey)) {
329 properties.remove(key);
330 properties.setProperty(trimmedKey, value);
331 }
332 }
333 }
334
335 private static Method _jdk6LoadMethod;
336
337 }