add toString() for XML, looking through todos

This commit is contained in:
benfry
2012-03-24 20:03:03 +00:00
parent 4e28c6b4e7
commit fff0a12536
5 changed files with 139 additions and 94 deletions
+116 -64
View File
@@ -10,8 +10,8 @@
License as published by the Free Software Foundation, version 2.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
@@ -24,6 +24,9 @@ package processing.core;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import org.xml.sax.*;
@@ -32,27 +35,27 @@ import processing.core.PApplet;
/**
* This is the base class used for the Processing XML library,
* This is the base class used for the Processing XML library,
* representing a single node of an XML tree.
*/
public class XML implements Serializable {
/** The internal representation, a DOM node. */
protected Node node;
/** Cached locally because it's used often. */
protected String name;
/** The parent element. */
protected XML parent;
/** Child elements, once loaded. */
protected XML[] children;
protected XML() { }
/**
* Begin parsing XML data passed in from a PApplet. This code
* wraps exception handling, for more advanced exception handling,
@@ -87,17 +90,17 @@ public class XML implements Serializable {
// builderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
DocumentBuilder builder = factory.newDocumentBuilder();
// builder.setEntityResolver()
// SAXParserFactory spf = SAXParserFactory.newInstance();
// spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
// SAXParser p = spf.newSAXParser();
// builder = DocumentBuilderFactory.newDocumentBuilder();
// builder = new SAXBuilder();
// builder.setValidation(validating);
// print(dataPath("1broke.html"), System.out);
// Document document = builder.parse(dataPath("1_alt.html"));
Document document = builder.parse(new InputSource(reader));
node = document.getDocumentElement();
@@ -106,14 +109,14 @@ public class XML implements Serializable {
// for (int i = 0; i < nodeList.getLength(); i++) {
// }
// print(createWriter("data/1_alt_reparse.html"), document.getDocumentElement(), 0);
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (SAXException e2) {
e2.printStackTrace();
}
}
}
@@ -125,16 +128,16 @@ public class XML implements Serializable {
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
node = document.createElement(name);
this.name = name;
this.parent = null;
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
// public PNodeXML(String name, PNode parent) {
// PNodeXML pxml = PNodeXML.parse("<" + name + ">");
// this.node = pxml.node;
@@ -142,18 +145,18 @@ public class XML implements Serializable {
// this.parent = parent;
// }
protected XML(XML parent, Node node) {
this.node = node;
this.parent = parent;
if (node.getNodeType() == Node.ELEMENT_NODE) {
name = node.getNodeName();
}
}
static public XML parse(String xml) {
static public XML parse(String xml) {
return new XML(new StringReader(xml));
}
@@ -166,7 +169,7 @@ public class XML implements Serializable {
return this.parent;
}
/**
* Returns the full name (i.e. the name including an eventual namespace
* prefix) of the element.
@@ -176,7 +179,7 @@ public class XML implements Serializable {
return name;
}
/**
* Returns the name of the element (without namespace prefix).
* @return the name, or null if the element only contains #PCDATA.
@@ -210,9 +213,9 @@ public class XML implements Serializable {
return children.length;
}
/**
* Put the names of all children into an array. Same as looping through
* Put the names of all children into an array. Same as looping through
* each child and calling getName() on each XMLElement.
*/
public String[] listChildren() {
@@ -232,8 +235,8 @@ public class XML implements Serializable {
}
return outgoing;
}
/**
* Returns an array containing all the child elements.
*/
@@ -357,8 +360,8 @@ public class XML implements Serializable {
}
return outgoing;
}
public XML addChild(String tag) {
Document document = node.getOwnerDocument();
Node newChild = document.createElement(tag);
@@ -384,7 +387,7 @@ public class XML implements Serializable {
return node.getAttributes().getLength();
}
/**
* Get a list of the names for all of the attributes for this node.
*/
@@ -428,7 +431,7 @@ public class XML implements Serializable {
// return (attr == null) ? defaultValue : attr.getNodeValue();
// }
public String getString(String name) {
return getString(name, null);
}
@@ -449,12 +452,12 @@ public class XML implements Serializable {
return getInt(name, 0);
}
public void setInt(String name, int value) {
setString(name, String.valueOf(value));
}
/**
* Returns the value of an attribute.
*
@@ -475,8 +478,8 @@ public class XML implements Serializable {
public float getFloat(String name) {
return getFloat(name, 0);
}
/**
* Returns the value of an attribute.
*
@@ -495,12 +498,12 @@ public class XML implements Serializable {
setString(name, String.valueOf(value));
}
public double getDouble(String name) {
return getDouble(name, 0);
}
/**
* Returns the value of an attribute.
*
@@ -513,8 +516,8 @@ public class XML implements Serializable {
String value = getString(name);
return (value == null) ? defaultValue : Double.parseDouble(value);
}
public void setDouble(String name, double value) {
setString(name, String.valueOf(value));
}
@@ -529,35 +532,84 @@ public class XML implements Serializable {
* @return the content.
*/
public String getContent() {
//return node.getTextContent(); // requires Android 2.2
StringBuilder buffer = new StringBuilder();
NodeList childList = node.getChildNodes();
for (int i = 0; i < childList.getLength(); i++) {
Node child = childList.item(i);
if (child.getNodeType() == Node.TEXT_NODE) { // skip non-text nodes
buffer.append(child.getNodeValue());
}
}
return buffer.toString();
return node.getTextContent();
}
public void setContent(String text) {
node.setTextContent(text);
}
public String toString() {
return toString(2);
}
public String toString(int indent) {
return super.toString();
// requires API level 8
// javax.xml.transform.TransformerFactory factory = new javax.xml.transform.TransformerFactory();
// javax.xml.transform.Transformer transformer = factory.newTransformer();
// javax.xml.transform.dom.DOMSource domSource = new javax.xml.transform.dom.DOMSource(rootNode);
// javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(outputStream);
// transformer(domSource, result);
try {
// node.normalize(); // does nothing useful
DOMSource dumSource = new DOMSource(node);
// entities = doctype.getEntities()
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
// if this is the root, output the decl, if not, hide it
if (parent != null) {
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
// } else {
// transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
}
// transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "sample.dtd");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
// transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "yes"); // huh?
// transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
// "-//W3C//DTD XHTML 1.0 Transitional//EN");
// transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
// "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd");
// transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
// transformer.setOutputProperty(OutputKeys.ENCODING,"UTF8");
transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
// transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS
// indent by default, but sometimes this needs to be turned off
if (indent != 0) {
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
}
// Properties p = transformer.getOutputProperties();
// for (Object key : p.keySet()) {
// System.out.println(key + " -> " + p.get(key));
// }
StringWriter sw = new StringWriter();
StreamResult sr = new StreamResult(sw);
transformer.transform(dumSource, sr);
return sw.toString();
// Document document = node.getOwnerDocument();
// OutputFormat format = new OutputFormat(document);
// format.setLineWidth(65);
// format.setIndenting(true);
// format.setIndent(2);
// StringWriter sw = new StringWriter();
// XMLSerializer serializer = new XMLSerializer(sw, format);
// serializer.serialize(document);
// return sw.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
/*
javax.xml.transform.TransformerFactory factory = new javax.xml.transform.TransformerFactory();
javax.xml.transform.Transformer transformer = factory.newTransformer();
javax.xml.transform.dom.DOMSource domSource = new javax.xml.transform.dom.DOMSource(rootNode);
javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(outputStream);
transformer(domSource, result);
*/
/*
try {
DOMSource dumSource = new DOMSource(node);
@@ -579,7 +631,7 @@ public class XML implements Serializable {
StreamResult sr = new StreamResult(sw);
transformer.transform(dumSource, sr);
return sw.toString();
} catch (Exception e) {
e.printStackTrace();
}
+6
View File
@@ -1,7 +1,13 @@
0205 android
X finish xml writing on android
X http://stackoverflow.com/questions/2290945/writing-xml-on-android
X consider switch to android 2.2 as the minimum
X remove unnecessary processing.xml.* code from android-core
_ http://code.google.com/p/processing/issues/detail?id=214
_ Android mode is broken on Windows in Processing 2.0a5
_ file a bug for this w/ Google
_ http://code.google.com/p/processing/issues/detail?id=1022
_ lack of registerXxx() implementation is breaking pre() and post() in particles
+1
View File
@@ -237,6 +237,7 @@ public class PApplet extends Applet
*/
static public boolean useQuartz = true;
/**
* Modifier flags for the shortcut key used to trigger menus.
* (Cmd on Mac OS X, Ctrl on Linux and Windows)
+12 -27
View File
@@ -1,6 +1,18 @@
0205 core
_ look into using BufferStrategy again to improve performance
_ there are more improvements to be made ala issue #729
_ memory leak when many createGraphics(..., JAVA2D) calls are used
_ http://code.google.com/p/processing/issues/detail?id=507
_ need to add clear() method so clear out ARGB surfaces
_ pause()/resume() need to work on the desktop side as well
_ stop() not called in 1.5
_ http://code.google.com/p/processing/issues/detail?id=636
_ In reply to c#1, I noticed that dispose() is getting called. stop() isn't.
_ selectInput() and selectOutput() freezes
_ http://code.google.com/p/processing/issues/detail?id=173
_ -> now includes a patch that addresses one specific problem
@@ -17,8 +29,6 @@ _ fix edge + 1 issue on stroke/fill for rectangles
_ add reference/docs for urlEncode() and urlDecode()
_ pause()/resume() need to work on the desktop side as well
_ Wishlist for the new XML class in Processing 2.0
_ http://code.google.com/p/processing/issues/detail?id=904
@@ -29,9 +39,6 @@ _ should this be the default to be more like old XML?
_ add() to add things to lists, sum() for the math (sum is used less after all)
_ consider using BufferStrategy to improve performance
_ there are more improvements to be made ala issue #729
_ add inputPath() and outputPath() -> sketch folder or sd card
_ or should this just be a change to sketchPath() on Android?
_ also because input/output won't be different (since not data folder)
@@ -149,31 +156,11 @@ _ and on android, start/stop can be used to save state information
_ need to fix opengl applets so that we can safely kill P3D
more xml/json changes
_ finish xml writing on android
_ http://stackoverflow.com/questions/2290945/writing-xml-on-android
_ consider switch to android 2.2 as the minimum
_ finish updating XML documentation
_ http://code.google.com/p/processing/issues/detail?id=382
_ json issues
_ http://www.xml.com/lpt/a/1658
_ http://www.json.org/java/
_ remove unnecessary processing.xml.* code from android-core
_ http://code.google.com/p/processing/issues/detail?id=214
casey [Moved to web/todo.txt 17 SEP 2011]
o pdf fonts
o rewrite documentation about pdf fonts (errors with non-native fonts)
o figure out why font naming not working correctly
o add blendMode() to reference, remove blend()
o need documentation for quadraticVertex()
o docs for rect(x, y, w, h, r) and rect(x, y, w, h, tl, tr, br, bl)
o docs for new arc()
o "Find in Reference" for copy() goes to PVector (oog)
X screen.width/height shouldn't be anywhere
X add docs for screenWidth and screenHeight
X remove delay() documentation
X remove textMode(SCREEN) references
2.0
_ PNode.getChildren() shouldn't make a new array.. toArray() can do that
@@ -233,8 +220,6 @@ _ http://code.google.com/p/processing/issues/detail?id=697
_ sleep time needs to be set *much* higher for dormant applets
_ 10s should be fine--no need to keep spinning (bad for android too)
_ just call interrupt() when it's time to get back to work
_ memory leak when many createGraphics(..., JAVA2D) calls are used
_ http://code.google.com/p/processing/issues/detail?id=507
_ add methods to PShape to apply all transformations in the tree
_ need to clean up the hints in the reference/source
+4 -3
View File
@@ -6,9 +6,10 @@ X get rid of restore sketch feature
_ remove pref for restoring sketches
_ implement recent sketches menu
_ stop() not called in 1.5
_ http://code.google.com/p/processing/issues/detail?id=636
_ In reply to c#1, I noticed that dispose() is getting called. stop() isn't.
_ find in reference for copy() (on image) tries to open PVector.copy()
_ might need disambiguation pages?
_ if a reference page is missing, throws a bunch of exceptions
_ i.e. PVector.copy() not in the reference
_ check on new JDK and how it works on OS X
_ http://jdk7.java.net/macportpreview/