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