fix whitespace issues in new XML lib, also add methods (issue #904)

This commit is contained in:
benfry
2012-04-02 21:46:09 +00:00
parent 4b3d7d14d6
commit aae83cef59
2 changed files with 61 additions and 30 deletions

View File

@@ -3,7 +3,7 @@
/*
Part of the Processing project - http://processing.org
Copyright (c) 2009-11 Ben Fry and Casey Reas
Copyright (c) 2009-12 Ben Fry and Casey Reas
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@@ -68,27 +68,15 @@ public class XML implements Serializable {
}
// public XML(String xml) {
// this(new StringReader(xml));
// }
public XML(Reader reader) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// factory.setValidating(false);
// factory.setAttribute("http://xml.org/sax/features/namespaces", true);
// factory.setAttribute("http://xml.org/sax/features/validation", false);
// factory.setAttribute("http://xml.org/sax/features/validation", true);
// factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
// enable this to temporarily get around some parsing quirks (and get a proper error msg)
// factory.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
// Prevent 503 errors from www.w3.org
factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
// factory.setAttribute("http://apache.org/xml/features/dom/create-entity-ref-nodes", false);
// doesn't seem to help the NPE issues caused by new whitespace parsing
//factory.setIgnoringElementContentWhitespace(true);
// without a validating DTD, this doesn't do anything since it doesn't know what is ignorable
// factory.setIgnoringElementContentWhitespace(true);
factory.setExpandEntityReferences(false);
// factory.setExpandEntityReferences(true);
@@ -156,10 +144,7 @@ public class XML implements Serializable {
protected XML(XML parent, Node node) {
this.node = node;
this.parent = parent;
if (node.getNodeType() == Node.ELEMENT_NODE) {
name = node.getNodeName();
}
this.name = node.getNodeName();
}
@@ -168,6 +153,23 @@ public class XML implements Serializable {
}
public boolean save(OutputStream output) {
return save(PApplet.createWriter(output));
}
public boolean save(File file) {
return save(PApplet.createWriter(file));
}
public boolean save(PrintWriter output) {
output.print(toString(2));
output.flush();
return true;
}
/**
* Returns the parent element. This method returns null for the root
* element.
@@ -177,6 +179,11 @@ public class XML implements Serializable {
}
protected Node getNode() {
return node;
}
/**
* Returns the full name (i.e. the name including an eventual namespace
* prefix) of the element.
@@ -187,6 +194,13 @@ public class XML implements Serializable {
}
public void setName(String newName) {
Document document = node.getOwnerDocument();
node = document.renameNode(node, null, newName);
name = node.getNodeName();
}
/**
* Returns the name of the element (without namespace prefix).
* @return the name, or null if the element only contains #PCDATA.
@@ -372,12 +386,25 @@ public class XML implements Serializable {
public XML addChild(String tag) {
Document document = node.getOwnerDocument();
Node newChild = document.createElement(tag);
node.appendChild(newChild);
XML pn = new XML(this, newChild);
return appendChild(newChild);
}
public XML addChild(XML child) {
Document document = node.getOwnerDocument();
Node newChild = document.importNode(child.getNode(), true);
return appendChild(newChild);
}
/** Internal handler to add the node structure. */
protected XML appendChild(Node newNode) {
node.appendChild(newNode);
XML newbie = new XML(this, newNode);
if (children != null) {
children = (XML[]) PApplet.concat(children, new XML[] { pn });
children = (XML[]) PApplet.concat(children, new XML[] { newbie });
}
return pn;
return newbie;
}

View File

@@ -23,18 +23,22 @@ o questions re: final xml api changes
o http://code.google.com/p/processing/issues/detail?id=612
X look into replacing nanoxml
X http://www.exampledepot.com/egs/javax.xml.parsers/pkg.html
X add save() method
_ Wishlist for the new XML class in Processing 2.0
_ http://code.google.com/p/processing/issues/detail?id=904
_ void addChild(XML child)
X void addChild(XML child)
X void setContent(String content)
_ void setName(String name)
_ do we need an option to disable XML whitespace?
_ should this be the default to be more like old XML?
_ otherwise document how things are sometimes null in XML
_ test xml examples to see if they break
X void setName(String name)
X do we need an option to disable XML whitespace?
o should this be the default to be more like old XML?
o otherwise document how things are sometimes null in XML
o test xml examples to see if they break
X changed to make it return #text for the name, which is more correct
_ finish updating XML documentation
_ http://code.google.com/p/processing/issues/detail?id=382
_ several other items under the LIBRARIES / XML section below
_ move to processing.data.* package
_ update the internal code for Android and desktop to add the import
_ shared intf for 3D view data across PGraphicsOpenGL and PGraphicsAndroid3D
_ libraries have to do a lot of casting