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.math.BigDecimal; |
25 | |
import java.math.BigInteger; |
26 | |
import java.net.URL; |
27 | |
import java.text.DateFormat; |
28 | |
import java.text.ParseException; |
29 | |
import java.text.SimpleDateFormat; |
30 | |
import java.util.ArrayList; |
31 | |
import java.util.Calendar; |
32 | |
import java.util.Collection; |
33 | |
import java.util.Date; |
34 | |
import java.util.Iterator; |
35 | |
import java.util.List; |
36 | |
import java.util.Map; |
37 | |
import java.util.TimeZone; |
38 | |
|
39 | |
import javax.xml.parsers.SAXParser; |
40 | |
import javax.xml.parsers.SAXParserFactory; |
41 | |
|
42 | |
import org.apache.commons.codec.binary.Base64; |
43 | |
import org.apache.commons.configuration.AbstractHierarchicalFileConfiguration; |
44 | |
import org.apache.commons.configuration.Configuration; |
45 | |
import org.apache.commons.configuration.ConfigurationException; |
46 | |
import org.apache.commons.configuration.HierarchicalConfiguration; |
47 | |
import org.apache.commons.configuration.MapConfiguration; |
48 | |
import org.apache.commons.lang.StringEscapeUtils; |
49 | |
import org.apache.commons.lang.StringUtils; |
50 | |
import org.xml.sax.Attributes; |
51 | |
import org.xml.sax.EntityResolver; |
52 | |
import org.xml.sax.InputSource; |
53 | |
import org.xml.sax.SAXException; |
54 | |
import org.xml.sax.helpers.DefaultHandler; |
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 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public class XMLPropertyListConfiguration extends AbstractHierarchicalFileConfiguration |
123 | |
{ |
124 | |
|
125 | |
|
126 | |
|
127 | |
private static final long serialVersionUID = -3162063751042475985L; |
128 | |
|
129 | |
|
130 | |
private static final int INDENT_SIZE = 4; |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public XMLPropertyListConfiguration() |
138 | 69 | { |
139 | 69 | } |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
public XMLPropertyListConfiguration(HierarchicalConfiguration configuration) |
149 | |
{ |
150 | 1 | super(configuration); |
151 | 1 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
public XMLPropertyListConfiguration(String fileName) throws ConfigurationException |
161 | |
{ |
162 | 2 | super(fileName); |
163 | 2 | } |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
public XMLPropertyListConfiguration(File file) throws ConfigurationException |
172 | |
{ |
173 | 11 | super(file); |
174 | 11 | } |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
public XMLPropertyListConfiguration(URL url) throws ConfigurationException |
183 | |
{ |
184 | 0 | super(url); |
185 | 0 | } |
186 | |
|
187 | |
public void setProperty(String key, Object value) |
188 | |
{ |
189 | |
|
190 | 4 | if (value instanceof byte[]) |
191 | |
{ |
192 | 2 | fireEvent(EVENT_SET_PROPERTY, key, value, true); |
193 | 2 | setDetailEvents(false); |
194 | |
try |
195 | |
{ |
196 | 2 | clearProperty(key); |
197 | 2 | addPropertyDirect(key, value); |
198 | |
} |
199 | |
finally |
200 | |
{ |
201 | 2 | setDetailEvents(true); |
202 | 2 | } |
203 | 2 | fireEvent(EVENT_SET_PROPERTY, key, value, false); |
204 | |
} |
205 | |
else |
206 | |
{ |
207 | 2 | super.setProperty(key, value); |
208 | |
} |
209 | 4 | } |
210 | |
|
211 | |
public void addProperty(String key, Object value) |
212 | |
{ |
213 | 14 | if (value instanceof byte[]) |
214 | |
{ |
215 | 2 | fireEvent(EVENT_ADD_PROPERTY, key, value, true); |
216 | 2 | addPropertyDirect(key, value); |
217 | 2 | fireEvent(EVENT_ADD_PROPERTY, key, value, false); |
218 | |
} |
219 | |
else |
220 | |
{ |
221 | 12 | super.addProperty(key, value); |
222 | |
} |
223 | 14 | } |
224 | |
|
225 | |
public void load(Reader in) throws ConfigurationException |
226 | |
{ |
227 | |
|
228 | 28 | EntityResolver resolver = new EntityResolver() |
229 | |
{ |
230 | 28 | public InputSource resolveEntity(String publicId, String systemId) |
231 | |
{ |
232 | 28 | return new InputSource(getClass().getClassLoader().getResourceAsStream("PropertyList-1.0.dtd")); |
233 | |
} |
234 | |
}; |
235 | |
|
236 | |
|
237 | 28 | XMLPropertyListHandler handler = new XMLPropertyListHandler(getRoot()); |
238 | |
try |
239 | |
{ |
240 | 28 | SAXParserFactory factory = SAXParserFactory.newInstance(); |
241 | 28 | factory.setValidating(true); |
242 | |
|
243 | 28 | SAXParser parser = factory.newSAXParser(); |
244 | 28 | parser.getXMLReader().setEntityResolver(resolver); |
245 | 28 | parser.getXMLReader().setContentHandler(handler); |
246 | 28 | parser.getXMLReader().parse(new InputSource(in)); |
247 | |
} |
248 | 0 | catch (Exception e) |
249 | |
{ |
250 | 0 | throw new ConfigurationException("Unable to parse the configuration file", e); |
251 | 28 | } |
252 | 28 | } |
253 | |
|
254 | |
public void save(Writer out) throws ConfigurationException |
255 | |
{ |
256 | 3 | PrintWriter writer = new PrintWriter(out); |
257 | |
|
258 | 3 | if (getEncoding() != null) |
259 | |
{ |
260 | 0 | writer.println("<?xml version=\"1.0\" encoding=\"" + getEncoding() + "\"?>"); |
261 | |
} |
262 | |
else |
263 | |
{ |
264 | 3 | writer.println("<?xml version=\"1.0\"?>"); |
265 | |
} |
266 | |
|
267 | 3 | writer.println("<!DOCTYPE plist SYSTEM \"file://localhost/System/Library/DTDs/PropertyList.dtd\">"); |
268 | 3 | writer.println("<plist version=\"1.0\">"); |
269 | |
|
270 | 3 | printNode(writer, 1, getRoot()); |
271 | |
|
272 | 3 | writer.println("</plist>"); |
273 | 3 | writer.flush(); |
274 | 3 | } |
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
private void printNode(PrintWriter out, int indentLevel, Node node) |
280 | |
{ |
281 | 28 | String padding = StringUtils.repeat(" ", indentLevel * INDENT_SIZE); |
282 | |
|
283 | 28 | if (node.getName() != null) |
284 | |
{ |
285 | 23 | out.println(padding + "<key>" + StringEscapeUtils.escapeXml(node.getName()) + "</key>"); |
286 | |
} |
287 | |
|
288 | 28 | List children = node.getChildren(); |
289 | 28 | if (!children.isEmpty()) |
290 | |
{ |
291 | 9 | out.println(padding + "<dict>"); |
292 | |
|
293 | 9 | Iterator it = children.iterator(); |
294 | 32 | while (it.hasNext()) |
295 | |
{ |
296 | 23 | Node child = (Node) it.next(); |
297 | 23 | printNode(out, indentLevel + 1, child); |
298 | |
|
299 | 23 | if (it.hasNext()) |
300 | |
{ |
301 | 14 | out.println(); |
302 | |
} |
303 | |
} |
304 | |
|
305 | 9 | out.println(padding + "</dict>"); |
306 | |
} |
307 | |
else |
308 | |
{ |
309 | 19 | Object value = node.getValue(); |
310 | 19 | printValue(out, indentLevel, value); |
311 | |
} |
312 | 28 | } |
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
private void printValue(PrintWriter out, int indentLevel, Object value) |
318 | |
{ |
319 | 30 | String padding = StringUtils.repeat(" ", indentLevel * INDENT_SIZE); |
320 | |
|
321 | 30 | if (value instanceof Date) |
322 | |
{ |
323 | 2 | synchronized (PListNode.format) |
324 | |
{ |
325 | 2 | out.println(padding + "<date>" + PListNode.format.format((Date) value) + "</date>"); |
326 | 2 | } |
327 | |
} |
328 | 28 | else if (value instanceof Calendar) |
329 | |
{ |
330 | 0 | printValue(out, indentLevel, ((Calendar) value).getTime()); |
331 | |
} |
332 | 28 | else if (value instanceof Number) |
333 | |
{ |
334 | 2 | if (value instanceof Double || value instanceof Float || value instanceof BigDecimal) |
335 | |
{ |
336 | 1 | out.println(padding + "<real>" + value.toString() + "</real>"); |
337 | |
} |
338 | |
else |
339 | |
{ |
340 | 1 | out.println(padding + "<integer>" + value.toString() + "</integer>"); |
341 | |
} |
342 | |
} |
343 | 26 | else if (value instanceof Boolean) |
344 | |
{ |
345 | 2 | if (((Boolean) value).booleanValue()) |
346 | |
{ |
347 | 1 | out.println(padding + "<true/>"); |
348 | |
} |
349 | |
else |
350 | |
{ |
351 | 1 | out.println(padding + "<false/>"); |
352 | |
} |
353 | |
} |
354 | 24 | else if (value instanceof List) |
355 | |
{ |
356 | 5 | out.println(padding + "<array>"); |
357 | 5 | Iterator it = ((List) value).iterator(); |
358 | 16 | while (it.hasNext()) |
359 | |
{ |
360 | 11 | printValue(out, indentLevel + 1, it.next()); |
361 | |
} |
362 | 5 | out.println(padding + "</array>"); |
363 | |
} |
364 | 19 | else if (value instanceof HierarchicalConfiguration) |
365 | |
{ |
366 | 2 | printNode(out, indentLevel, ((HierarchicalConfiguration) value).getRoot()); |
367 | |
} |
368 | 17 | else if (value instanceof Configuration) |
369 | |
{ |
370 | |
|
371 | 0 | out.println(padding + "<dict>"); |
372 | |
|
373 | 0 | Configuration config = (Configuration) value; |
374 | 0 | Iterator it = config.getKeys(); |
375 | 0 | while (it.hasNext()) |
376 | |
{ |
377 | |
|
378 | 0 | String key = (String) it.next(); |
379 | 0 | Node node = new Node(key); |
380 | 0 | node.setValue(config.getProperty(key)); |
381 | |
|
382 | |
|
383 | 0 | printNode(out, indentLevel + 1, node); |
384 | |
|
385 | 0 | if (it.hasNext()) |
386 | |
{ |
387 | 0 | out.println(); |
388 | |
} |
389 | |
} |
390 | 0 | out.println(padding + "</dict>"); |
391 | |
} |
392 | 17 | else if (value instanceof Map) |
393 | |
{ |
394 | |
|
395 | 0 | Map map = (Map) value; |
396 | 0 | printValue(out, indentLevel, new MapConfiguration(map)); |
397 | |
} |
398 | 17 | else if (value instanceof byte[]) |
399 | |
{ |
400 | 3 | String base64 = new String(Base64.encodeBase64((byte[]) value)); |
401 | 3 | out.println(padding + "<data>" + StringEscapeUtils.escapeXml(base64) + "</data>"); |
402 | |
} |
403 | |
else |
404 | |
{ |
405 | 14 | out.println(padding + "<string>" + StringEscapeUtils.escapeXml(String.valueOf(value)) + "</string>"); |
406 | |
} |
407 | 30 | } |
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
private static class XMLPropertyListHandler extends DefaultHandler |
413 | |
{ |
414 | |
|
415 | 28 | private StringBuffer buffer = new StringBuffer(); |
416 | |
|
417 | |
|
418 | 28 | private List stack = new ArrayList(); |
419 | |
|
420 | |
public XMLPropertyListHandler(Node root) |
421 | 28 | { |
422 | 28 | push(root); |
423 | 28 | } |
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
private Node peek() |
429 | |
{ |
430 | 2168 | if (!stack.isEmpty()) |
431 | |
{ |
432 | 2140 | return (Node) stack.get(stack.size() - 1); |
433 | |
} |
434 | |
else |
435 | |
{ |
436 | 28 | return null; |
437 | |
} |
438 | |
} |
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
private Node pop() |
444 | |
{ |
445 | 786 | if (!stack.isEmpty()) |
446 | |
{ |
447 | 758 | return (Node) stack.remove(stack.size() - 1); |
448 | |
} |
449 | |
else |
450 | |
{ |
451 | 28 | return null; |
452 | |
} |
453 | |
} |
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
private void push(Node node) |
459 | |
{ |
460 | 758 | stack.add(node); |
461 | 758 | } |
462 | |
|
463 | |
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException |
464 | |
{ |
465 | 1438 | if ("array".equals(qName)) |
466 | |
{ |
467 | 130 | push(new ArrayNode()); |
468 | |
} |
469 | 1308 | else if ("dict".equals(qName)) |
470 | |
{ |
471 | 184 | if (peek() instanceof ArrayNode) |
472 | |
{ |
473 | |
|
474 | 52 | XMLPropertyListConfiguration config = new XMLPropertyListConfiguration(); |
475 | |
|
476 | |
|
477 | 52 | ArrayNode node = (ArrayNode) peek(); |
478 | 52 | node.addValue(config); |
479 | |
|
480 | |
|
481 | 52 | push(config.getRoot()); |
482 | |
} |
483 | |
} |
484 | 1438 | } |
485 | |
|
486 | |
public void endElement(String uri, String localName, String qName) throws SAXException |
487 | |
{ |
488 | 1438 | if ("key".equals(qName)) |
489 | |
{ |
490 | |
|
491 | 548 | PListNode node = new PListNode(); |
492 | 548 | node.setName(buffer.toString()); |
493 | 548 | peek().addChild(node); |
494 | 548 | push(node); |
495 | |
} |
496 | 890 | else if ("dict".equals(qName)) |
497 | |
{ |
498 | |
|
499 | 184 | pop(); |
500 | |
} |
501 | |
else |
502 | |
{ |
503 | 706 | if ("string".equals(qName)) |
504 | |
{ |
505 | 364 | ((PListNode) peek()).addValue(buffer.toString()); |
506 | |
} |
507 | 342 | else if ("integer".equals(qName)) |
508 | |
{ |
509 | 26 | ((PListNode) peek()).addIntegerValue(buffer.toString()); |
510 | |
} |
511 | 316 | else if ("real".equals(qName)) |
512 | |
{ |
513 | 26 | ((PListNode) peek()).addRealValue(buffer.toString()); |
514 | |
} |
515 | 290 | else if ("true".equals(qName)) |
516 | |
{ |
517 | 26 | ((PListNode) peek()).addTrueValue(); |
518 | |
} |
519 | 264 | else if ("false".equals(qName)) |
520 | |
{ |
521 | 26 | ((PListNode) peek()).addFalseValue(); |
522 | |
} |
523 | 238 | else if ("data".equals(qName)) |
524 | |
{ |
525 | 28 | ((PListNode) peek()).addDataValue(buffer.toString()); |
526 | |
} |
527 | 210 | else if ("date".equals(qName)) |
528 | |
{ |
529 | 52 | ((PListNode) peek()).addDateValue(buffer.toString()); |
530 | |
} |
531 | 158 | else if ("array".equals(qName)) |
532 | |
{ |
533 | 130 | ArrayNode array = (ArrayNode) pop(); |
534 | 130 | ((PListNode) peek()).addList(array); |
535 | |
} |
536 | |
|
537 | |
|
538 | |
|
539 | 706 | if (!(peek() instanceof ArrayNode)) |
540 | |
{ |
541 | 472 | pop(); |
542 | |
} |
543 | |
} |
544 | |
|
545 | 1438 | buffer.setLength(0); |
546 | 1438 | } |
547 | |
|
548 | |
public void characters(char[] ch, int start, int length) throws SAXException |
549 | |
{ |
550 | 1044 | buffer.append(ch, start, length); |
551 | 1044 | } |
552 | |
} |
553 | |
|
554 | |
|
555 | |
|
556 | |
|
557 | |
|
558 | |
|
559 | 682 | public static class PListNode extends Node |
560 | |
{ |
561 | |
|
562 | |
|
563 | |
|
564 | |
private static final long serialVersionUID = -7614060264754798317L; |
565 | |
|
566 | |
|
567 | 2 | private static DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); |
568 | |
static |
569 | |
{ |
570 | 2 | format.setTimeZone(TimeZone.getTimeZone("UTC")); |
571 | |
} |
572 | |
|
573 | |
|
574 | 2 | private static DateFormat gnustepFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z"); |
575 | |
|
576 | |
|
577 | |
|
578 | |
|
579 | |
|
580 | |
|
581 | |
|
582 | |
|
583 | |
|
584 | |
public void addValue(Object value) |
585 | |
{ |
586 | 444 | if (getValue() == null) |
587 | |
{ |
588 | 444 | setValue(value); |
589 | |
} |
590 | 0 | else if (getValue() instanceof Collection) |
591 | |
{ |
592 | 0 | Collection collection = (Collection) getValue(); |
593 | 0 | collection.add(value); |
594 | |
} |
595 | |
else |
596 | |
{ |
597 | 0 | List list = new ArrayList(); |
598 | 0 | list.add(getValue()); |
599 | 0 | list.add(value); |
600 | 0 | setValue(list); |
601 | |
} |
602 | 444 | } |
603 | |
|
604 | |
|
605 | |
|
606 | |
|
607 | |
|
608 | |
|
609 | |
public void addDateValue(String value) |
610 | |
{ |
611 | |
try |
612 | |
{ |
613 | 52 | if (value.indexOf(' ') != -1) |
614 | |
{ |
615 | |
|
616 | 25 | synchronized (gnustepFormat) |
617 | |
{ |
618 | 25 | addValue(gnustepFormat.parse(value)); |
619 | 25 | } |
620 | |
} |
621 | |
else |
622 | |
{ |
623 | |
|
624 | 27 | synchronized (format) |
625 | |
{ |
626 | 27 | addValue(format.parse(value)); |
627 | 27 | } |
628 | |
} |
629 | |
} |
630 | 0 | catch (ParseException e) |
631 | |
{ |
632 | |
|
633 | |
; |
634 | 52 | } |
635 | 52 | } |
636 | |
|
637 | |
|
638 | |
|
639 | |
|
640 | |
|
641 | |
|
642 | |
|
643 | |
public void addDataValue(String value) |
644 | |
{ |
645 | 28 | addValue(Base64.decodeBase64(value.getBytes())); |
646 | 28 | } |
647 | |
|
648 | |
|
649 | |
|
650 | |
|
651 | |
|
652 | |
|
653 | |
public void addIntegerValue(String value) |
654 | |
{ |
655 | 26 | addValue(new BigInteger(value)); |
656 | 26 | } |
657 | |
|
658 | |
|
659 | |
|
660 | |
|
661 | |
|
662 | |
|
663 | |
public void addRealValue(String value) |
664 | |
{ |
665 | 26 | addValue(new BigDecimal(value)); |
666 | 26 | } |
667 | |
|
668 | |
|
669 | |
|
670 | |
|
671 | |
public void addTrueValue() |
672 | |
{ |
673 | 26 | addValue(Boolean.TRUE); |
674 | 26 | } |
675 | |
|
676 | |
|
677 | |
|
678 | |
|
679 | |
public void addFalseValue() |
680 | |
{ |
681 | 26 | addValue(Boolean.FALSE); |
682 | 26 | } |
683 | |
|
684 | |
|
685 | |
|
686 | |
|
687 | |
|
688 | |
|
689 | |
public void addList(ArrayNode node) |
690 | |
{ |
691 | 130 | addValue(node.getValue()); |
692 | 130 | } |
693 | |
} |
694 | |
|
695 | |
|
696 | |
|
697 | |
|
698 | |
|
699 | |
|
700 | 130 | public static class ArrayNode extends PListNode |
701 | |
{ |
702 | |
|
703 | |
|
704 | |
|
705 | |
private static final long serialVersionUID = 5586544306664205835L; |
706 | |
|
707 | |
|
708 | 130 | private List list = new ArrayList(); |
709 | |
|
710 | |
|
711 | |
|
712 | |
|
713 | |
|
714 | |
|
715 | |
public void addValue(Object value) |
716 | |
{ |
717 | 286 | list.add(value); |
718 | 286 | } |
719 | |
|
720 | |
|
721 | |
|
722 | |
|
723 | |
|
724 | |
|
725 | |
public Object getValue() |
726 | |
{ |
727 | 130 | return list; |
728 | |
} |
729 | |
} |
730 | |
} |