| 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 | |
|
| 36 | |
import org.apache.commons.codec.binary.Base64; |
| 37 | |
import org.apache.commons.configuration.AbstractHierarchicalFileConfiguration; |
| 38 | |
import org.apache.commons.configuration.Configuration; |
| 39 | |
import org.apache.commons.configuration.ConfigurationException; |
| 40 | |
import org.apache.commons.configuration.HierarchicalConfiguration; |
| 41 | |
import org.apache.commons.configuration.MapConfiguration; |
| 42 | |
import org.apache.commons.digester.AbstractObjectCreationFactory; |
| 43 | |
import org.apache.commons.digester.Digester; |
| 44 | |
import org.apache.commons.digester.ObjectCreateRule; |
| 45 | |
import org.apache.commons.digester.SetNextRule; |
| 46 | |
import org.apache.commons.lang.StringEscapeUtils; |
| 47 | |
import org.apache.commons.lang.StringUtils; |
| 48 | |
import org.xml.sax.Attributes; |
| 49 | |
import org.xml.sax.EntityResolver; |
| 50 | |
import org.xml.sax.InputSource; |
| 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 | |
|
| 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 | |
public class XMLPropertyListConfiguration extends AbstractHierarchicalFileConfiguration |
| 118 | |
{ |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
private static final long serialVersionUID = -3162063751042475985L; |
| 123 | |
|
| 124 | |
|
| 125 | |
private static final int INDENT_SIZE = 4; |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
public XMLPropertyListConfiguration() |
| 133 | 35 | { |
| 134 | 35 | } |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
public XMLPropertyListConfiguration(String fileName) throws ConfigurationException |
| 143 | |
{ |
| 144 | 0 | super(fileName); |
| 145 | 0 | } |
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
public XMLPropertyListConfiguration(File file) throws ConfigurationException |
| 154 | |
{ |
| 155 | 1 | super(file); |
| 156 | 1 | } |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
public XMLPropertyListConfiguration(URL url) throws ConfigurationException |
| 165 | |
{ |
| 166 | 0 | super(url); |
| 167 | 0 | } |
| 168 | |
|
| 169 | |
public void load(Reader in) throws ConfigurationException |
| 170 | |
{ |
| 171 | |
|
| 172 | 12 | Digester digester = new Digester(); |
| 173 | |
|
| 174 | |
|
| 175 | 14 | digester.setEntityResolver(new EntityResolver() |
| 176 | |
{ |
| 177 | 12 | public InputSource resolveEntity(String publicId, String systemId) |
| 178 | |
{ |
| 179 | 12 | return new InputSource(getClass().getClassLoader().getResourceAsStream("PropertyList-1.0.dtd")); |
| 180 | |
} |
| 181 | |
}); |
| 182 | 12 | digester.setValidating(true); |
| 183 | |
|
| 184 | |
|
| 185 | 12 | digester.addRule("*/key", new ObjectCreateRule(PListNode.class) |
| 186 | |
{ |
| 187 | 12 | public void end() throws Exception |
| 188 | |
{ |
| 189 | |
|
| 190 | 240 | } |
| 191 | |
}); |
| 192 | |
|
| 193 | 12 | digester.addCallMethod("*/key", "setName", 0); |
| 194 | |
|
| 195 | 12 | digester.addRule("*/dict/string", new SetNextAndPopRule("addChild")); |
| 196 | 12 | digester.addRule("*/dict/data", new SetNextAndPopRule("addChild")); |
| 197 | 12 | digester.addRule("*/dict/integer", new SetNextAndPopRule("addChild")); |
| 198 | 12 | digester.addRule("*/dict/real", new SetNextAndPopRule("addChild")); |
| 199 | 12 | digester.addRule("*/dict/true", new SetNextAndPopRule("addChild")); |
| 200 | 12 | digester.addRule("*/dict/false", new SetNextAndPopRule("addChild")); |
| 201 | 12 | digester.addRule("*/dict/date", new SetNextAndPopRule("addChild")); |
| 202 | 12 | digester.addRule("*/dict/dict", new SetNextAndPopRule("addChild")); |
| 203 | |
|
| 204 | 12 | digester.addCallMethod("*/dict/string", "addValue", 0); |
| 205 | 12 | digester.addCallMethod("*/dict/data", "addDataValue", 0); |
| 206 | 12 | digester.addCallMethod("*/dict/integer", "addIntegerValue", 0); |
| 207 | 12 | digester.addCallMethod("*/dict/real", "addRealValue", 0); |
| 208 | 12 | digester.addCallMethod("*/dict/true", "addTrueValue"); |
| 209 | 12 | digester.addCallMethod("*/dict/false", "addFalseValue"); |
| 210 | 12 | digester.addCallMethod("*/dict/date", "addDateValue", 0); |
| 211 | |
|
| 212 | |
|
| 213 | 12 | digester.addRule("*/dict/array", new SetNextAndPopRule("addChild")); |
| 214 | 12 | digester.addRule("*/dict/array", new ObjectCreateRule(ArrayNode.class)); |
| 215 | 12 | digester.addSetNext("*/dict/array", "addList"); |
| 216 | |
|
| 217 | 12 | digester.addRule("*/array/array", new ObjectCreateRule(ArrayNode.class)); |
| 218 | 12 | digester.addSetNext("*/array/array", "addList"); |
| 219 | |
|
| 220 | 12 | digester.addCallMethod("*/array/string", "addValue", 0); |
| 221 | 12 | digester.addCallMethod("*/array/data", "addDataValue", 0); |
| 222 | 12 | digester.addCallMethod("*/array/integer", "addIntegerValue", 0); |
| 223 | 12 | digester.addCallMethod("*/array/real", "addRealValue", 0); |
| 224 | 12 | digester.addCallMethod("*/array/true", "addTrueValue"); |
| 225 | 12 | digester.addCallMethod("*/array/false", "addFalseValue"); |
| 226 | 12 | digester.addCallMethod("*/array/date", "addDateValue", 0); |
| 227 | |
|
| 228 | |
|
| 229 | 12 | digester.addFactoryCreate("*/array/dict", new AbstractObjectCreationFactory() |
| 230 | |
{ |
| 231 | 12 | public Object createObject(Attributes attributes) throws Exception |
| 232 | |
{ |
| 233 | |
|
| 234 | 24 | XMLPropertyListConfiguration config = new XMLPropertyListConfiguration(); |
| 235 | |
|
| 236 | |
|
| 237 | 24 | ArrayNode node = (ArrayNode) getDigester().peek(); |
| 238 | 24 | node.addValue(config); |
| 239 | |
|
| 240 | |
|
| 241 | 24 | return config.getRoot(); |
| 242 | |
} |
| 243 | |
}); |
| 244 | |
|
| 245 | |
|
| 246 | 12 | digester.push(getRoot()); |
| 247 | |
try |
| 248 | |
{ |
| 249 | 12 | digester.parse(in); |
| 250 | 12 | } |
| 251 | |
catch (Exception e) |
| 252 | |
{ |
| 253 | 0 | throw new ConfigurationException("Unable to parse the configuration file", e); |
| 254 | |
} |
| 255 | 12 | } |
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
private class SetNextAndPopRule extends SetNextRule |
| 263 | |
{ |
| 264 | |
public SetNextAndPopRule(String methodName) |
| 265 | 108 | { |
| 266 | 108 | super(methodName); |
| 267 | 108 | } |
| 268 | |
|
| 269 | |
public void end(String namespace, String name) throws Exception |
| 270 | |
{ |
| 271 | 240 | super.end(namespace, name); |
| 272 | 240 | digester.pop(); |
| 273 | 240 | } |
| 274 | |
} |
| 275 | |
|
| 276 | |
public void save(Writer out) throws ConfigurationException |
| 277 | |
{ |
| 278 | 1 | PrintWriter writer = new PrintWriter(out); |
| 279 | |
|
| 280 | 1 | if (getEncoding() != null) |
| 281 | |
{ |
| 282 | 0 | writer.println("<?xml version=\"1.0\" encoding=\"" + getEncoding() + "\"?>"); |
| 283 | |
} |
| 284 | |
else |
| 285 | |
{ |
| 286 | 1 | writer.println("<?xml version=\"1.0\"?>"); |
| 287 | |
} |
| 288 | |
|
| 289 | 1 | writer.println("<!DOCTYPE plist SYSTEM \"file://localhost/System/Library/DTDs/PropertyList.dtd\">"); |
| 290 | 1 | writer.println("<plist version=\"1.0\">"); |
| 291 | |
|
| 292 | 1 | printNode(writer, 1, getRoot()); |
| 293 | |
|
| 294 | 1 | writer.println("</plist>"); |
| 295 | 1 | writer.flush(); |
| 296 | 1 | } |
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
private void printNode(PrintWriter out, int indentLevel, Node node) |
| 302 | |
{ |
| 303 | 23 | String padding = StringUtils.repeat(" ", indentLevel * INDENT_SIZE); |
| 304 | |
|
| 305 | 23 | if (node.getName() != null) |
| 306 | |
{ |
| 307 | 20 | out.println(padding + "<key>" + StringEscapeUtils.escapeXml(node.getName()) + "</key>"); |
| 308 | |
} |
| 309 | |
|
| 310 | 23 | List children = node.getChildren(); |
| 311 | 23 | if (!children.isEmpty()) |
| 312 | |
{ |
| 313 | 7 | out.println(padding + "<dict>"); |
| 314 | |
|
| 315 | 7 | Iterator it = children.iterator(); |
| 316 | 34 | while (it.hasNext()) |
| 317 | |
{ |
| 318 | 20 | Node child = (Node) it.next(); |
| 319 | 20 | printNode(out, indentLevel + 1, child); |
| 320 | |
|
| 321 | 20 | if (it.hasNext()) |
| 322 | |
{ |
| 323 | 13 | out.println(); |
| 324 | |
} |
| 325 | |
} |
| 326 | |
|
| 327 | 7 | out.println(padding + "</dict>"); |
| 328 | |
} |
| 329 | |
else |
| 330 | |
{ |
| 331 | 16 | Object value = node.getValue(); |
| 332 | 16 | printValue(out, indentLevel, value); |
| 333 | |
} |
| 334 | 23 | } |
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
private void printValue(PrintWriter out, int indentLevel, Object value) |
| 340 | |
{ |
| 341 | 27 | String padding = StringUtils.repeat(" ", indentLevel * INDENT_SIZE); |
| 342 | |
|
| 343 | 27 | if (value instanceof Date) |
| 344 | |
{ |
| 345 | 1 | out.println(padding + "<date>" + PListNode.format.format((Date) value) + "</date>"); |
| 346 | |
} |
| 347 | 26 | else if (value instanceof Calendar) |
| 348 | |
{ |
| 349 | 0 | printValue(out, indentLevel, ((Calendar) value).getTime()); |
| 350 | |
} |
| 351 | 26 | else if (value instanceof Number) |
| 352 | |
{ |
| 353 | 2 | if (value instanceof Double || value instanceof Float || value instanceof BigDecimal) |
| 354 | |
{ |
| 355 | 1 | out.println(padding + "<real>" + value.toString() + "</real>"); |
| 356 | |
} |
| 357 | |
else |
| 358 | |
{ |
| 359 | 1 | out.println(padding + "<integer>" + value.toString() + "</integer>"); |
| 360 | |
} |
| 361 | |
} |
| 362 | 24 | else if (value instanceof Boolean) |
| 363 | |
{ |
| 364 | 2 | if (((Boolean) value).booleanValue()) |
| 365 | |
{ |
| 366 | 1 | out.println(padding + "<true/>"); |
| 367 | |
} |
| 368 | |
else |
| 369 | |
{ |
| 370 | 1 | out.println(padding + "<false/>"); |
| 371 | |
} |
| 372 | |
} |
| 373 | 22 | else if (value instanceof List) |
| 374 | |
{ |
| 375 | 5 | out.println(padding + "<array>"); |
| 376 | 5 | Iterator it = ((List) value).iterator(); |
| 377 | 21 | while (it.hasNext()) |
| 378 | |
{ |
| 379 | 11 | printValue(out, indentLevel + 1, it.next()); |
| 380 | |
} |
| 381 | 5 | out.println(padding + "</array>"); |
| 382 | |
} |
| 383 | 17 | else if (value instanceof HierarchicalConfiguration) |
| 384 | |
{ |
| 385 | 2 | printNode(out, indentLevel, ((HierarchicalConfiguration) value).getRoot()); |
| 386 | |
} |
| 387 | 15 | else if (value instanceof Configuration) |
| 388 | |
{ |
| 389 | |
|
| 390 | 0 | out.println(padding + "<dict>"); |
| 391 | |
|
| 392 | 0 | Configuration config = (Configuration) value; |
| 393 | 0 | Iterator it = config.getKeys(); |
| 394 | 0 | while (it.hasNext()) |
| 395 | |
{ |
| 396 | |
|
| 397 | 0 | String key = (String) it.next(); |
| 398 | 0 | Node node = new Node(key); |
| 399 | 0 | node.setValue(config.getProperty(key)); |
| 400 | |
|
| 401 | |
|
| 402 | 0 | printNode(out, indentLevel + 1, node); |
| 403 | |
|
| 404 | 0 | if (it.hasNext()) |
| 405 | |
{ |
| 406 | 0 | out.println(); |
| 407 | |
} |
| 408 | |
} |
| 409 | 0 | out.println(padding + "</dict>"); |
| 410 | |
} |
| 411 | 15 | else if (value instanceof Map) |
| 412 | |
{ |
| 413 | |
|
| 414 | 0 | Map map = (Map) value; |
| 415 | 0 | printValue(out, indentLevel, new MapConfiguration(map)); |
| 416 | |
} |
| 417 | 15 | else if (value instanceof byte[]) |
| 418 | |
{ |
| 419 | 1 | String base64 = new String(Base64.encodeBase64((byte[]) value)); |
| 420 | 1 | out.println(padding + "<data>" + StringEscapeUtils.escapeXml(base64) + "</data>"); |
| 421 | |
} |
| 422 | |
else |
| 423 | |
{ |
| 424 | 14 | out.println(padding + "<string>" + StringEscapeUtils.escapeXml(String.valueOf(value)) + "</string>"); |
| 425 | |
} |
| 426 | 27 | } |
| 427 | |
|
| 428 | |
|
| 429 | |
|
| 430 | |
|
| 431 | |
|
| 432 | |
|
| 433 | |
|
| 434 | 301 | public static class PListNode extends Node |
| 435 | |
{ |
| 436 | |
|
| 437 | |
|
| 438 | |
|
| 439 | |
private static final long serialVersionUID = -7614060264754798317L; |
| 440 | |
|
| 441 | |
|
| 442 | 1 | private static DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); |
| 443 | |
|
| 444 | |
|
| 445 | |
|
| 446 | |
|
| 447 | |
|
| 448 | |
|
| 449 | |
|
| 450 | |
|
| 451 | |
|
| 452 | |
public void addValue(Object value) |
| 453 | |
{ |
| 454 | 192 | if (getValue() == null) |
| 455 | |
{ |
| 456 | 192 | setValue(value); |
| 457 | |
} |
| 458 | 0 | else if (getValue() instanceof List) |
| 459 | |
{ |
| 460 | 0 | List list = (List) getValue(); |
| 461 | 0 | list.add(value); |
| 462 | |
} |
| 463 | |
else |
| 464 | |
{ |
| 465 | 0 | List list = new ArrayList(); |
| 466 | 0 | list.add(getValue()); |
| 467 | 0 | list.add(value); |
| 468 | 0 | setValue(list); |
| 469 | |
} |
| 470 | 192 | } |
| 471 | |
|
| 472 | |
|
| 473 | |
|
| 474 | |
|
| 475 | |
|
| 476 | |
|
| 477 | |
public void addDateValue(String value) |
| 478 | |
{ |
| 479 | |
try |
| 480 | |
{ |
| 481 | 12 | addValue(format.parse(value)); |
| 482 | 12 | } |
| 483 | |
catch (ParseException e) |
| 484 | |
{ |
| 485 | 0 | e.printStackTrace(); |
| 486 | |
} |
| 487 | 12 | } |
| 488 | |
|
| 489 | |
|
| 490 | |
|
| 491 | |
|
| 492 | |
|
| 493 | |
|
| 494 | |
|
| 495 | |
public void addDataValue(String value) |
| 496 | |
{ |
| 497 | 12 | addValue(Base64.decodeBase64(value.getBytes())); |
| 498 | 12 | } |
| 499 | |
|
| 500 | |
|
| 501 | |
|
| 502 | |
|
| 503 | |
|
| 504 | |
|
| 505 | |
public void addIntegerValue(String value) |
| 506 | |
{ |
| 507 | 12 | addValue(new Integer(value)); |
| 508 | 12 | } |
| 509 | |
|
| 510 | |
|
| 511 | |
|
| 512 | |
|
| 513 | |
|
| 514 | |
|
| 515 | |
public void addRealValue(String value) |
| 516 | |
{ |
| 517 | 12 | addValue(new Double(value)); |
| 518 | 12 | } |
| 519 | |
|
| 520 | |
|
| 521 | |
|
| 522 | |
|
| 523 | |
public void addTrueValue() |
| 524 | |
{ |
| 525 | 12 | addValue(Boolean.TRUE); |
| 526 | 12 | } |
| 527 | |
|
| 528 | |
|
| 529 | |
|
| 530 | |
|
| 531 | |
public void addFalseValue() |
| 532 | |
{ |
| 533 | 12 | addValue(Boolean.FALSE); |
| 534 | 12 | } |
| 535 | |
|
| 536 | |
|
| 537 | |
|
| 538 | |
|
| 539 | |
|
| 540 | |
|
| 541 | |
public void addList(ArrayNode node) |
| 542 | |
{ |
| 543 | 60 | addValue(node.getValue()); |
| 544 | 60 | } |
| 545 | |
} |
| 546 | |
|
| 547 | |
|
| 548 | |
|
| 549 | |
|
| 550 | |
|
| 551 | |
|
| 552 | 120 | public static class ArrayNode extends PListNode |
| 553 | |
{ |
| 554 | |
|
| 555 | |
|
| 556 | |
|
| 557 | |
private static final long serialVersionUID = 5586544306664205835L; |
| 558 | |
|
| 559 | |
|
| 560 | 60 | private List list = new ArrayList(); |
| 561 | |
|
| 562 | |
|
| 563 | |
|
| 564 | |
|
| 565 | |
|
| 566 | |
|
| 567 | |
public void addValue(Object value) |
| 568 | |
{ |
| 569 | 132 | list.add(value); |
| 570 | 132 | } |
| 571 | |
|
| 572 | |
|
| 573 | |
|
| 574 | |
|
| 575 | |
|
| 576 | |
|
| 577 | |
public Object getValue() |
| 578 | |
{ |
| 579 | 60 | return list; |
| 580 | |
} |
| 581 | |
} |
| 582 | |
} |