Skip to content

Commit e0faab9

Browse files
committed
basic trim() implementation
1 parent 9fe8aff commit e0faab9

File tree

2 files changed

+46
-65
lines changed

2 files changed

+46
-65
lines changed

core/src/processing/data/XML.java

Lines changed: 43 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/*
44
Part of the Processing project - http://processing.org
55
6+
Copyright (c) 2012 The Processing Foundation
67
Copyright (c) 2009-12 Ben Fry and Casey Reas
78
89
This library is free software; you can redistribute it and/or
@@ -138,14 +139,6 @@ public XML(String name) {
138139
}
139140

140141

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-
149142
protected XML(XML parent, Node node) {
150143
this.node = node;
151144
this.parent = parent;
@@ -174,7 +167,7 @@ public boolean save(File file) {
174167

175168

176169
public boolean save(PrintWriter output) {
177-
output.print(toString(2));
170+
output.print(format(2));
178171
output.flush();
179172
return true;
180173
}
@@ -430,6 +423,38 @@ public void removeChild(XML kid) {
430423
}
431424

432425

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+
433458
/**
434459
* Returns the number of attributes.
435460
*/
@@ -591,24 +616,17 @@ public void setContent(String text) {
591616
}
592617

593618

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) {
601620
try {
602-
// node.normalize(); // does nothing useful
603621
DOMSource dumSource = new DOMSource(node);
604622
// entities = doctype.getEntities()
605623
TransformerFactory tf = TransformerFactory.newInstance();
606624
Transformer transformer = tf.newTransformer();
607625
// if this is the root, output the decl, if not, hide it
608-
if (parent != null) {
626+
if (indent == -1 || parent != null) {
609627
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");
612630
}
613631
// transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "sample.dtd");
614632
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
@@ -638,56 +656,16 @@ public String toString(int indent) {
638656
transformer.transform(dumSource, sr);
639657
return sw.toString();
640658

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-
651659
} catch (Exception e) {
652660
e.printStackTrace();
653661
}
654662
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-
684663
}
685664

686665

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+
}
693671
}

core/todo.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
0210 core
22

33

4+
hint(OPENGL_ERRORS) should be the opposite to enable the reporting, no?
5+
6+
47
xml tweaks
58
_ beginning slash in getChild() threw an NPE
69
_ do we need a trim() method for XML? or enableWhiteSpace()? or?

0 commit comments

Comments
 (0)