1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.commons.configuration.plist; |
19 | |
|
20 | |
import java.io.File; |
21 | |
import java.io.PrintWriter; |
22 | |
import java.io.Reader; |
23 | |
import java.io.Writer; |
24 | |
import java.net.URL; |
25 | |
import java.util.ArrayList; |
26 | |
import java.util.Calendar; |
27 | |
import java.util.Date; |
28 | |
import java.util.Iterator; |
29 | |
import java.util.List; |
30 | |
import java.util.Map; |
31 | |
import java.util.TimeZone; |
32 | |
|
33 | |
import org.apache.commons.codec.binary.Hex; |
34 | |
import org.apache.commons.configuration.AbstractHierarchicalFileConfiguration; |
35 | |
import org.apache.commons.configuration.Configuration; |
36 | |
import org.apache.commons.configuration.ConfigurationException; |
37 | |
import org.apache.commons.configuration.HierarchicalConfiguration; |
38 | |
import org.apache.commons.configuration.MapConfiguration; |
39 | |
import org.apache.commons.lang.StringUtils; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public class PropertyListConfiguration extends AbstractHierarchicalFileConfiguration |
84 | |
{ |
85 | |
|
86 | 3 | private static final DateComponentParser DATE_SEPARATOR_PARSER = new DateSeparatorParser( |
87 | |
"-"); |
88 | |
|
89 | |
|
90 | 3 | private static final DateComponentParser TIME_SEPARATOR_PARSER = new DateSeparatorParser( |
91 | |
":"); |
92 | |
|
93 | |
|
94 | 3 | private static final DateComponentParser BLANK_SEPARATOR_PARSER = new DateSeparatorParser( |
95 | |
" "); |
96 | |
|
97 | |
|
98 | 3 | private static final DateComponentParser[] DATE_PARSERS = |
99 | |
{new DateSeparatorParser("<*D"), new DateFieldParser(Calendar.YEAR, 4), |
100 | |
DATE_SEPARATOR_PARSER, new DateFieldParser(Calendar.MONTH, 2, 1), |
101 | |
DATE_SEPARATOR_PARSER, new DateFieldParser(Calendar.DATE, 2), |
102 | |
BLANK_SEPARATOR_PARSER, |
103 | |
new DateFieldParser(Calendar.HOUR_OF_DAY, 2), |
104 | |
TIME_SEPARATOR_PARSER, new DateFieldParser(Calendar.MINUTE, 2), |
105 | |
TIME_SEPARATOR_PARSER, new DateFieldParser(Calendar.SECOND, 2), |
106 | |
BLANK_SEPARATOR_PARSER, new DateTimeZoneParser(), |
107 | |
new DateSeparatorParser(">")}; |
108 | |
|
109 | |
|
110 | |
private static final String TIME_ZONE_PREFIX = "GMT"; |
111 | |
|
112 | |
|
113 | |
private static final long serialVersionUID = 3227248503779092127L; |
114 | |
|
115 | |
|
116 | |
private static final int MILLIS_PER_MINUTE = 1000 * 60; |
117 | |
|
118 | |
|
119 | |
private static final int MINUTES_PER_HOUR = 60; |
120 | |
|
121 | |
|
122 | |
private static final int INDENT_SIZE = 4; |
123 | |
|
124 | |
|
125 | |
private static final int TIME_ZONE_LENGTH = 5; |
126 | |
|
127 | |
|
128 | |
private static final char PAD_CHAR = '0'; |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public PropertyListConfiguration() |
136 | 291 | { |
137 | 291 | } |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
public PropertyListConfiguration(HierarchicalConfiguration c) |
147 | |
{ |
148 | 1 | super(c); |
149 | 1 | } |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
public PropertyListConfiguration(String fileName) throws ConfigurationException |
158 | |
{ |
159 | 2 | super(fileName); |
160 | 2 | } |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
public PropertyListConfiguration(File file) throws ConfigurationException |
169 | |
{ |
170 | 11 | super(file); |
171 | 11 | } |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
public PropertyListConfiguration(URL url) throws ConfigurationException |
180 | |
{ |
181 | 0 | super(url); |
182 | 0 | } |
183 | |
|
184 | |
public void setProperty(String key, Object value) |
185 | |
{ |
186 | |
|
187 | 4 | if (value instanceof byte[]) |
188 | |
{ |
189 | 2 | fireEvent(EVENT_SET_PROPERTY, key, value, true); |
190 | 2 | setDetailEvents(false); |
191 | |
try |
192 | |
{ |
193 | 2 | clearProperty(key); |
194 | 2 | addPropertyDirect(key, value); |
195 | |
} |
196 | |
finally |
197 | |
{ |
198 | 2 | setDetailEvents(true); |
199 | 2 | } |
200 | 2 | fireEvent(EVENT_SET_PROPERTY, key, value, false); |
201 | 2 | } |
202 | |
else |
203 | |
{ |
204 | 2 | super.setProperty(key, value); |
205 | |
} |
206 | 4 | } |
207 | |
|
208 | |
public void addProperty(String key, Object value) |
209 | |
{ |
210 | 14 | if (value instanceof byte[]) |
211 | |
{ |
212 | 2 | fireEvent(EVENT_ADD_PROPERTY, key, value, true); |
213 | 2 | addPropertyDirect(key, value); |
214 | 2 | fireEvent(EVENT_ADD_PROPERTY, key, value, false); |
215 | 2 | } |
216 | |
else |
217 | |
{ |
218 | 12 | super.addProperty(key, value); |
219 | |
} |
220 | 14 | } |
221 | |
|
222 | |
public void load(Reader in) throws ConfigurationException |
223 | |
{ |
224 | 36 | PropertyListParser parser = new PropertyListParser(in); |
225 | |
try |
226 | |
{ |
227 | 36 | HierarchicalConfiguration config = parser.parse(); |
228 | 35 | setRoot(config.getRoot()); |
229 | |
} |
230 | 1 | catch (ParseException e) |
231 | |
{ |
232 | 1 | throw new ConfigurationException(e); |
233 | 35 | } |
234 | 35 | } |
235 | |
|
236 | |
public void save(Writer out) throws ConfigurationException |
237 | |
{ |
238 | 3 | PrintWriter writer = new PrintWriter(out); |
239 | 3 | printNode(writer, 0, getRoot()); |
240 | 3 | writer.flush(); |
241 | 3 | } |
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
private void printNode(PrintWriter out, int indentLevel, Node node) |
247 | |
{ |
248 | 27 | String padding = StringUtils.repeat(" ", indentLevel * INDENT_SIZE); |
249 | |
|
250 | 27 | if (node.getName() != null) |
251 | |
{ |
252 | 22 | out.print(padding + quoteString(node.getName()) + " = "); |
253 | |
} |
254 | |
|
255 | |
|
256 | 27 | List children = new ArrayList(node.getChildren()); |
257 | 27 | Iterator it = children.iterator(); |
258 | 50 | while (it.hasNext()) |
259 | |
{ |
260 | 23 | Node child = (Node) it.next(); |
261 | 23 | if (child.getValue() == null && (child.getChildren() == null || child.getChildren().isEmpty())) |
262 | |
{ |
263 | 1 | it.remove(); |
264 | |
} |
265 | 23 | } |
266 | |
|
267 | 27 | if (!children.isEmpty()) |
268 | |
{ |
269 | |
|
270 | 9 | if (indentLevel > 0) |
271 | |
{ |
272 | 6 | out.println(); |
273 | |
} |
274 | |
|
275 | 9 | out.println(padding + "{"); |
276 | |
|
277 | |
|
278 | 9 | it = children.iterator(); |
279 | 31 | while (it.hasNext()) |
280 | |
{ |
281 | 22 | Node child = (Node) it.next(); |
282 | |
|
283 | 22 | printNode(out, indentLevel + 1, child); |
284 | |
|
285 | |
|
286 | 22 | Object value = child.getValue(); |
287 | 22 | if (value != null && !(value instanceof Map) && !(value instanceof Configuration)) |
288 | |
{ |
289 | 18 | out.println(";"); |
290 | |
} |
291 | |
|
292 | |
|
293 | 22 | if (it.hasNext() && (value == null || value instanceof List)) |
294 | |
{ |
295 | 6 | out.println(); |
296 | |
} |
297 | 22 | } |
298 | |
|
299 | 9 | out.print(padding + "}"); |
300 | |
|
301 | |
|
302 | 9 | if (node.getParent() != null) |
303 | |
{ |
304 | 4 | out.println(); |
305 | 4 | } |
306 | |
} |
307 | |
else |
308 | |
{ |
309 | |
|
310 | 18 | Object value = node.getValue(); |
311 | 18 | printValue(out, indentLevel, value); |
312 | |
} |
313 | 27 | } |
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
private void printValue(PrintWriter out, int indentLevel, Object value) |
319 | |
{ |
320 | 29 | String padding = StringUtils.repeat(" ", indentLevel * INDENT_SIZE); |
321 | |
|
322 | 29 | if (value instanceof List) |
323 | |
{ |
324 | 6 | out.print("( "); |
325 | 6 | Iterator it = ((List) value).iterator(); |
326 | 17 | while (it.hasNext()) |
327 | |
{ |
328 | 11 | printValue(out, indentLevel + 1, it.next()); |
329 | 11 | if (it.hasNext()) |
330 | |
{ |
331 | 6 | out.print(", "); |
332 | 6 | } |
333 | |
} |
334 | 6 | out.print(" )"); |
335 | 6 | } |
336 | 23 | else if (value instanceof HierarchicalConfiguration) |
337 | |
{ |
338 | 2 | printNode(out, indentLevel, ((HierarchicalConfiguration) value).getRoot()); |
339 | 2 | } |
340 | 21 | else if (value instanceof Configuration) |
341 | |
{ |
342 | |
|
343 | 0 | out.println(); |
344 | 0 | out.println(padding + "{"); |
345 | |
|
346 | 0 | Configuration config = (Configuration) value; |
347 | 0 | Iterator it = config.getKeys(); |
348 | 0 | while (it.hasNext()) |
349 | |
{ |
350 | 0 | String key = (String) it.next(); |
351 | 0 | Node node = new Node(key); |
352 | 0 | node.setValue(config.getProperty(key)); |
353 | |
|
354 | 0 | printNode(out, indentLevel + 1, node); |
355 | 0 | out.println(";"); |
356 | 0 | } |
357 | 0 | out.println(padding + "}"); |
358 | 0 | } |
359 | 21 | else if (value instanceof Map) |
360 | |
{ |
361 | |
|
362 | 0 | Map map = (Map) value; |
363 | 0 | printValue(out, indentLevel, new MapConfiguration(map)); |
364 | 0 | } |
365 | 21 | else if (value instanceof byte[]) |
366 | |
{ |
367 | 4 | out.print("<" + new String(Hex.encodeHex((byte[]) value)) + ">"); |
368 | 4 | } |
369 | 17 | else if (value instanceof Date) |
370 | |
{ |
371 | 1 | out.print(formatDate((Date) value)); |
372 | 1 | } |
373 | 16 | else if (value != null) |
374 | |
{ |
375 | 16 | out.print(quoteString(String.valueOf(value))); |
376 | |
} |
377 | 29 | } |
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
String quoteString(String s) |
397 | |
{ |
398 | 43 | if (s == null) |
399 | |
{ |
400 | 1 | return null; |
401 | |
} |
402 | |
|
403 | 42 | if (s.indexOf(' ') != -1 |
404 | |
|| s.indexOf('\t') != -1 |
405 | |
|| s.indexOf('\r') != -1 |
406 | |
|| s.indexOf('\n') != -1 |
407 | |
|| s.indexOf('"') != -1 |
408 | |
|| s.indexOf('(') != -1 |
409 | |
|| s.indexOf(')') != -1 |
410 | |
|| s.indexOf('{') != -1 |
411 | |
|| s.indexOf('}') != -1 |
412 | |
|| s.indexOf('=') != -1 |
413 | |
|| s.indexOf(',') != -1 |
414 | |
|| s.indexOf(';') != -1) |
415 | |
{ |
416 | 5 | s = StringUtils.replace(s, "\"", "\\\""); |
417 | 5 | s = "\"" + s + "\""; |
418 | |
} |
419 | |
|
420 | 42 | return s; |
421 | |
} |
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
static Date parseDate(String s) throws ParseException |
432 | |
{ |
433 | 38 | Calendar cal = Calendar.getInstance(); |
434 | 38 | cal.clear(); |
435 | 38 | int index = 0; |
436 | |
|
437 | 566 | for (int i = 0; i < DATE_PARSERS.length; i++) |
438 | |
{ |
439 | 532 | index += DATE_PARSERS[i].parseComponent(s, index, cal); |
440 | |
} |
441 | |
|
442 | 34 | return cal.getTime(); |
443 | |
} |
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
|
452 | |
static String formatDate(Calendar cal) |
453 | |
{ |
454 | 3 | StringBuffer buf = new StringBuffer(); |
455 | |
|
456 | 48 | for (int i = 0; i < DATE_PARSERS.length; i++) |
457 | |
{ |
458 | 45 | DATE_PARSERS[i].formatComponent(buf, cal); |
459 | |
} |
460 | |
|
461 | 3 | return buf.toString(); |
462 | |
} |
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
|
468 | |
|
469 | |
|
470 | |
static String formatDate(Date date) |
471 | |
{ |
472 | 1 | Calendar cal = Calendar.getInstance(); |
473 | 1 | cal.setTime(date); |
474 | 1 | return formatDate(cal); |
475 | |
} |
476 | |
|
477 | |
|
478 | |
|
479 | |
|
480 | |
|
481 | |
|
482 | |
|
483 | |
|
484 | 72 | private abstract static class DateComponentParser |
485 | |
{ |
486 | |
|
487 | |
|
488 | |
|
489 | |
|
490 | |
|
491 | |
|
492 | |
|
493 | |
|
494 | |
|
495 | |
public abstract int parseComponent(String s, int index, Calendar cal) |
496 | |
throws ParseException; |
497 | |
|
498 | |
|
499 | |
|
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
public abstract void formatComponent(StringBuffer buf, Calendar cal); |
506 | |
|
507 | |
|
508 | |
|
509 | |
|
510 | |
|
511 | |
|
512 | |
|
513 | |
|
514 | |
|
515 | |
|
516 | |
|
517 | |
protected void checkLength(String s, int index, int length) |
518 | |
throws ParseException |
519 | |
{ |
520 | 532 | int len = (s == null) ? 0 : s.length(); |
521 | 532 | if (index + length > len) |
522 | |
{ |
523 | 1 | throw new ParseException("Input string too short: " + s |
524 | |
+ ", index: " + index); |
525 | |
} |
526 | 531 | } |
527 | |
|
528 | |
|
529 | |
|
530 | |
|
531 | |
|
532 | |
|
533 | |
|
534 | |
|
535 | |
|
536 | |
protected void padNum(StringBuffer buf, int num, int length) |
537 | |
{ |
538 | 24 | buf.append(StringUtils.leftPad(String.valueOf(num), length, |
539 | |
PAD_CHAR)); |
540 | 24 | } |
541 | |
} |
542 | |
|
543 | |
|
544 | |
|
545 | |
|
546 | |
|
547 | |
|
548 | |
private static class DateFieldParser extends DateComponentParser |
549 | |
{ |
550 | |
|
551 | |
private int calendarField; |
552 | |
|
553 | |
|
554 | |
private int length; |
555 | |
|
556 | |
|
557 | |
private int offset; |
558 | |
|
559 | |
|
560 | |
|
561 | |
|
562 | |
|
563 | |
|
564 | |
|
565 | |
public DateFieldParser(int calFld, int len) |
566 | |
{ |
567 | 15 | this(calFld, len, 0); |
568 | 15 | } |
569 | |
|
570 | |
|
571 | |
|
572 | |
|
573 | |
|
574 | |
|
575 | |
|
576 | |
|
577 | |
|
578 | |
public DateFieldParser(int calFld, int len, int ofs) |
579 | 18 | { |
580 | 18 | calendarField = calFld; |
581 | 18 | length = len; |
582 | 18 | offset = ofs; |
583 | 18 | } |
584 | |
|
585 | |
public void formatComponent(StringBuffer buf, Calendar cal) |
586 | |
{ |
587 | 18 | padNum(buf, cal.get(calendarField) + offset, length); |
588 | 18 | } |
589 | |
|
590 | |
public int parseComponent(String s, int index, Calendar cal) |
591 | |
throws ParseException |
592 | |
{ |
593 | 214 | checkLength(s, index, length); |
594 | |
try |
595 | |
{ |
596 | 214 | cal.set(calendarField, Integer.parseInt(s.substring(index, |
597 | |
index + length)) |
598 | |
- offset); |
599 | 212 | return length; |
600 | |
} |
601 | 2 | catch (NumberFormatException nfex) |
602 | |
{ |
603 | 2 | throw new ParseException("Invalid number: " + s + ", index " |
604 | |
+ index); |
605 | |
} |
606 | |
} |
607 | |
} |
608 | |
|
609 | |
|
610 | |
|
611 | |
|
612 | |
|
613 | |
private static class DateSeparatorParser extends DateComponentParser |
614 | |
{ |
615 | |
|
616 | |
private String separator; |
617 | |
|
618 | |
|
619 | |
|
620 | |
|
621 | |
|
622 | |
|
623 | |
|
624 | |
public DateSeparatorParser(String sep) |
625 | 15 | { |
626 | 15 | separator = sep; |
627 | 15 | } |
628 | |
|
629 | |
public void formatComponent(StringBuffer buf, Calendar cal) |
630 | |
{ |
631 | 24 | buf.append(separator); |
632 | 24 | } |
633 | |
|
634 | |
public int parseComponent(String s, int index, Calendar cal) |
635 | |
throws ParseException |
636 | |
{ |
637 | 284 | checkLength(s, index, separator.length()); |
638 | 283 | if (!s.startsWith(separator, index)) |
639 | |
{ |
640 | 1 | throw new ParseException("Invalid input: " + s + ", index " |
641 | |
+ index + ", expected " + separator); |
642 | |
} |
643 | 282 | return separator.length(); |
644 | |
} |
645 | |
} |
646 | |
|
647 | |
|
648 | |
|
649 | |
|
650 | |
|
651 | 6 | private static class DateTimeZoneParser extends DateComponentParser |
652 | |
{ |
653 | |
public void formatComponent(StringBuffer buf, Calendar cal) |
654 | |
{ |
655 | 3 | TimeZone tz = cal.getTimeZone(); |
656 | 3 | int ofs = tz.getRawOffset() / MILLIS_PER_MINUTE; |
657 | 3 | if (ofs < 0) |
658 | |
{ |
659 | 1 | buf.append('-'); |
660 | 1 | ofs = -ofs; |
661 | 1 | } |
662 | |
else |
663 | |
{ |
664 | 2 | buf.append('+'); |
665 | |
} |
666 | 3 | int hour = ofs / MINUTES_PER_HOUR; |
667 | 3 | int min = ofs % MINUTES_PER_HOUR; |
668 | 3 | padNum(buf, hour, 2); |
669 | 3 | padNum(buf, min, 2); |
670 | 3 | } |
671 | |
|
672 | |
public int parseComponent(String s, int index, Calendar cal) |
673 | |
throws ParseException |
674 | |
{ |
675 | 34 | checkLength(s, index, TIME_ZONE_LENGTH); |
676 | 34 | TimeZone tz = TimeZone.getTimeZone(TIME_ZONE_PREFIX |
677 | |
+ s.substring(index, index + TIME_ZONE_LENGTH)); |
678 | 34 | cal.setTimeZone(tz); |
679 | 34 | return TIME_ZONE_LENGTH; |
680 | |
} |
681 | |
} |
682 | |
} |