|
3 | 3 | /* |
4 | 4 | Part of the Processing project - http://processing.org |
5 | 5 |
|
| 6 | + Copyright (c) 2012 The Processing Foundation |
6 | 7 | Copyright (c) 2009-12 Ben Fry and Casey Reas |
7 | 8 |
|
8 | 9 | This library is free software; you can redistribute it and/or |
@@ -138,14 +139,6 @@ public XML(String name) { |
138 | 139 | } |
139 | 140 |
|
140 | 141 |
|
141 | | -// public PNodeXML(String name, PNode parent) { |
142 | | -// PNodeXML pxml = PNodeXML.parse("<" + name + ">"); |
143 | | -// this.node = pxml.node; |
144 | | -// this.name = name; |
145 | | -// this.parent = parent; |
146 | | -// } |
147 | | - |
148 | | - |
149 | 142 | protected XML(XML parent, Node node) { |
150 | 143 | this.node = node; |
151 | 144 | this.parent = parent; |
@@ -174,7 +167,7 @@ public boolean save(File file) { |
174 | 167 |
|
175 | 168 |
|
176 | 169 | public boolean save(PrintWriter output) { |
177 | | - output.print(toString(2)); |
| 170 | + output.print(format(2)); |
178 | 171 | output.flush(); |
179 | 172 | return true; |
180 | 173 | } |
@@ -430,6 +423,38 @@ public void removeChild(XML kid) { |
430 | 423 | } |
431 | 424 |
|
432 | 425 |
|
| 426 | + /** Remove whitespace nodes. */ |
| 427 | + public void trim() { |
| 428 | +//// public static boolean isWhitespace(XML xml) { |
| 429 | +//// if (xml.node.getNodeType() != Node.TEXT_NODE) |
| 430 | +//// return false; |
| 431 | +//// Matcher m = whitespace.matcher(xml.node.getNodeValue()); |
| 432 | +//// return m.matches(); |
| 433 | +//// } |
| 434 | +// trim(this); |
| 435 | +// } |
| 436 | +// |
| 437 | +// |
| 438 | +// protected void trim() { |
| 439 | + checkChildren(); |
| 440 | + int index = 0; |
| 441 | + for (int i = 0; i < children.length; i++) { |
| 442 | + if (i != index) { |
| 443 | + children[index] = children[i]; |
| 444 | + } |
| 445 | + Node childNode = children[i].getNode(); |
| 446 | + if (childNode.getNodeType() != Node.TEXT_NODE || |
| 447 | + children[i].getContent().trim().length() > 0) { |
| 448 | + children[i].trim(); |
| 449 | + index++; |
| 450 | + } |
| 451 | + } |
| 452 | + if (index != children.length) { |
| 453 | + children = (XML[]) PApplet.subset(children, 0, index); |
| 454 | + } |
| 455 | + } |
| 456 | + |
| 457 | + |
433 | 458 | /** |
434 | 459 | * Returns the number of attributes. |
435 | 460 | */ |
@@ -591,24 +616,17 @@ public void setContent(String text) { |
591 | 616 | } |
592 | 617 |
|
593 | 618 |
|
594 | | - @Override |
595 | | - public String toString() { |
596 | | - return toString(2); |
597 | | - } |
598 | | - |
599 | | - |
600 | | - public String toString(int indent) { |
| 619 | + public String format(int indent) { |
601 | 620 | try { |
602 | | -// node.normalize(); // does nothing useful |
603 | 621 | DOMSource dumSource = new DOMSource(node); |
604 | 622 | // entities = doctype.getEntities() |
605 | 623 | TransformerFactory tf = TransformerFactory.newInstance(); |
606 | 624 | Transformer transformer = tf.newTransformer(); |
607 | 625 | // if this is the root, output the decl, if not, hide it |
608 | | - if (parent != null) { |
| 626 | + if (indent == -1 || parent != null) { |
609 | 627 | transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
610 | | -// } else { |
611 | | -// transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); |
| 628 | + } else { |
| 629 | + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); |
612 | 630 | } |
613 | 631 | // transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "sample.dtd"); |
614 | 632 | transformer.setOutputProperty(OutputKeys.METHOD, "xml"); |
@@ -638,56 +656,16 @@ public String toString(int indent) { |
638 | 656 | transformer.transform(dumSource, sr); |
639 | 657 | return sw.toString(); |
640 | 658 |
|
641 | | -// Document document = node.getOwnerDocument(); |
642 | | -// OutputFormat format = new OutputFormat(document); |
643 | | -// format.setLineWidth(65); |
644 | | -// format.setIndenting(true); |
645 | | -// format.setIndent(2); |
646 | | -// StringWriter sw = new StringWriter(); |
647 | | -// XMLSerializer serializer = new XMLSerializer(sw, format); |
648 | | -// serializer.serialize(document); |
649 | | -// return sw.toString(); |
650 | | - |
651 | 659 | } catch (Exception e) { |
652 | 660 | e.printStackTrace(); |
653 | 661 | } |
654 | 662 | return null; |
655 | | - |
656 | | -// DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
657 | | -// try { |
658 | | -// DocumentBuilder builder = factory.newDocumentBuilder(); |
659 | | -// //builder.get |
660 | | -//// Document document = builder. |
661 | | -// |
662 | | -// } catch (ParserConfigurationException e) { |
663 | | -// e.printStackTrace(); |
664 | | -// } |
665 | | - |
666 | | - |
667 | | - |
668 | | - // Document doc = new DocumentImpl(); |
669 | | -// return node.toString(); |
670 | | - |
671 | | -// TransformerFactory transfac = TransformerFactory.newInstance(); |
672 | | -// Transformer trans = transfac.newTransformer(); |
673 | | -// trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
674 | | -// trans.setOutputProperty(OutputKeys.INDENT, "yes"); |
675 | | -// |
676 | | -// //create string from xml tree |
677 | | -// StringWriter sw = new StringWriter(); |
678 | | -// StreamResult result = new StreamResult(sw); |
679 | | -//// Document doc = |
680 | | -// DOMSource source = new DOMSource(doc); |
681 | | -// trans.transform(source, result); |
682 | | -// String xmlString = sw.toString(); |
683 | | - |
684 | 663 | } |
685 | 664 |
|
686 | 665 |
|
687 | | -// static final String HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; |
688 | | -// |
689 | | -// public void write(PrintWriter writer) { |
690 | | -// writer.println(HEADER); |
691 | | -// writer.print(toString(2)); |
692 | | -// } |
| 666 | + @Override |
| 667 | + /** Return the XML data as a single line, with no DOCTYPE declaration. */ |
| 668 | + public String toString() { |
| 669 | + return format(-1); |
| 670 | + } |
693 | 671 | } |
0 commit comments