From d9e37ab6b527763bd0e6a4ef4c8ef8edd99c066b Mon Sep 17 00:00:00 2001 From: benfry Date: Sat, 7 Aug 2010 20:10:18 +0000 Subject: [PATCH] incorporating edits from the desktop version--new xml api --- .../core/src/processing/core/PShapeSVG.java | 47 +- .../src/processing/xml/StdXMLBuilder.java | 54 +- .../core/src/processing/xml/StdXMLParser.java | 27 +- .../core/src/processing/xml/XMLAttribute.java | 16 +- .../core/src/processing/xml/XMLElement.java | 2492 +++++++++-------- .../core/src/processing/xml/XMLWriter.java | 127 +- android/todo.txt | 10 +- 7 files changed, 1452 insertions(+), 1321 deletions(-) diff --git a/android/core/src/processing/core/PShapeSVG.java b/android/core/src/processing/core/PShapeSVG.java index 188cda632..fc1fbac28 100644 --- a/android/core/src/processing/core/PShapeSVG.java +++ b/android/core/src/processing/core/PShapeSVG.java @@ -173,7 +173,7 @@ public class PShapeSVG extends PShape { // not proper parsing of the viewBox, but will cover us for cases where // the width and height of the object is not specified - String viewBoxStr = svg.getStringAttribute("viewBox"); + String viewBoxStr = svg.getString("viewBox"); if (viewBoxStr != null) { int[] viewBox = PApplet.parseInt(PApplet.splitTokens(viewBoxStr)); width = viewBox[2]; @@ -183,8 +183,8 @@ public class PShapeSVG extends PShape { // TODO if viewbox is not same as width/height, then use it to scale // the original objects. for now, viewbox only used when width/height // are empty values (which by the spec means w/h of "100%" - String unitWidth = svg.getStringAttribute("width"); - String unitHeight = svg.getStringAttribute("height"); + String unitWidth = svg.getString("width"); + String unitHeight = svg.getString("height"); if (unitWidth != null) { width = parseUnitSize(unitWidth); height = parseUnitSize(unitHeight); @@ -258,7 +258,7 @@ public class PShapeSVG extends PShape { } element = properties; - name = properties.getStringAttribute("id"); + name = properties.getString("id"); // @#$(* adobe illustrator mangles names of objects when re-saving if (name != null) { while (true) { @@ -269,10 +269,10 @@ public class PShapeSVG extends PShape { } } - String displayStr = properties.getStringAttribute("display", "inline"); + String displayStr = properties.getString("display", "inline"); visible = !displayStr.equals("none"); - String transformStr = properties.getStringAttribute("transform"); + String transformStr = properties.getString("transform"); if (transformStr != null) { matrix = parseMatrix(transformStr); } @@ -290,6 +290,9 @@ public class PShapeSVG extends PShape { for (XMLElement elem : elements) { PShape kid = parseChild(elem); if (kid != null) { +// if (kid.name != null) { +// System.out.println("adding child " + kid.name); +// } addChild(kid); } } @@ -442,7 +445,7 @@ public class PShapeSVG extends PShape { family = PATH; this.close = close; - String pointsAttr = element.getStringAttribute("points"); + String pointsAttr = element.getString("points"); if (pointsAttr != null) { String[] pointsBuffer = PApplet.splitTokens(pointsAttr); vertexCount = pointsBuffer.length; @@ -460,7 +463,7 @@ public class PShapeSVG extends PShape { family = PATH; primitive = 0; - String pathData = element.getStringAttribute("d"); + String pathData = element.getString("d"); if (pathData == null) return; char[] pathDataChars = pathData.toCharArray(); @@ -864,50 +867,50 @@ public class PShapeSVG extends PShape { protected void parseColors(XMLElement properties) { if (properties.hasAttribute("opacity")) { - String opacityText = properties.getStringAttribute("opacity"); + String opacityText = properties.getString("opacity"); setOpacity(opacityText); } if (properties.hasAttribute("stroke")) { - String strokeText = properties.getStringAttribute("stroke"); + String strokeText = properties.getString("stroke"); setColor(strokeText, false); } if (properties.hasAttribute("stroke-opacity")) { - String strokeOpacityText = properties.getStringAttribute("stroke-opacity"); + String strokeOpacityText = properties.getString("stroke-opacity"); setStrokeOpacity(strokeOpacityText); } if (properties.hasAttribute("stroke-width")) { // if NaN (i.e. if it's 'inherit') then default back to the inherit setting - String lineweight = properties.getStringAttribute("stroke-width"); + String lineweight = properties.getString("stroke-width"); setStrokeWeight(lineweight); } if (properties.hasAttribute("stroke-linejoin")) { - String linejoin = properties.getStringAttribute("stroke-linejoin"); + String linejoin = properties.getString("stroke-linejoin"); setStrokeJoin(linejoin); } if (properties.hasAttribute("stroke-linecap")) { - String linecap = properties.getStringAttribute("stroke-linecap"); + String linecap = properties.getString("stroke-linecap"); setStrokeCap(linecap); } // fill defaults to black (though stroke defaults to "none") // http://www.w3.org/TR/SVG/painting.html#FillProperties if (properties.hasAttribute("fill")) { - String fillText = properties.getStringAttribute("fill"); + String fillText = properties.getString("fill"); setColor(fillText, true); } if (properties.hasAttribute("fill-opacity")) { - String fillOpacityText = properties.getStringAttribute("fill-opacity"); + String fillOpacityText = properties.getString("fill-opacity"); setFillOpacity(fillOpacityText); } if (properties.hasAttribute("style")) { - String styleText = properties.getStringAttribute("style"); + String styleText = properties.getString("style"); String[] styleTokens = PApplet.splitTokens(styleText, ";"); //PApplet.println(styleTokens); @@ -1086,7 +1089,7 @@ public class PShapeSVG extends PShape { * @return unit-parsed version of the data */ static protected float getFloatWithUnit(XMLElement element, String attribute) { - String val = element.getStringAttribute(attribute); + String val = element.getString(attribute); return (val == null) ? 0 : parseUnitSize(val); } @@ -1146,14 +1149,14 @@ public class PShapeSVG extends PShape { XMLElement elem = elements[i]; String name = elem.getName(); if (name.equals("stop")) { - String offsetAttr = elem.getStringAttribute("offset"); + String offsetAttr = elem.getString("offset"); float div = 1.0f; if (offsetAttr.endsWith("%")) { div = 100.0f; offsetAttr = offsetAttr.substring(0, offsetAttr.length() - 1); } offset[count] = PApplet.parseFloat(offsetAttr) / div; - String style = elem.getStringAttribute("style"); + String style = elem.getString("style"); HashMap styles = parseStyleAttributes(style); String colorStr = styles.get("stop-color"); @@ -1184,7 +1187,7 @@ public class PShapeSVG extends PShape { this.y2 = getFloatWithUnit(properties, "y2"); String transformStr = - properties.getStringAttribute("gradientTransform"); + properties.getString("gradientTransform"); if (transformStr != null) { float t[] = parseMatrix(transformStr).get(null); @@ -1227,7 +1230,7 @@ public class PShapeSVG extends PShape { this.r = getFloatWithUnit(properties, "r"); String transformStr = - properties.getStringAttribute("gradientTransform"); + properties.getString("gradientTransform"); if (transformStr != null) { float t[] = parseMatrix(transformStr).get(null); diff --git a/android/core/src/processing/xml/StdXMLBuilder.java b/android/core/src/processing/xml/StdXMLBuilder.java index 54a223546..b66f7f447 100644 --- a/android/core/src/processing/xml/StdXMLBuilder.java +++ b/android/core/src/processing/xml/StdXMLBuilder.java @@ -28,24 +28,19 @@ package processing.xml; - import java.io.IOException; import java.io.Reader; import java.util.Stack; /** - * StdXMLBuilder is a concrete implementation of IXMLBuilder which creates a - * tree of IXMLElement from an XML data source. + * StdXMLBuilder creates a tree of XML elements from a data source. * * @see processing.xml.XMLElement * * @author Marc De Scheemaecker - * @version $Name: RELEASE_2_2_1 $, $Revision: 1.3 $ */ -public class StdXMLBuilder -{ - +public class StdXMLBuilder { /** * This stack contains the current element and its parents. */ @@ -59,42 +54,21 @@ public class StdXMLBuilder private XMLElement parent; - /** - * Prototype element for creating the tree. - */ - //private XMLElement prototype; - - /** * Creates the builder. */ - public StdXMLBuilder() - { + public StdXMLBuilder() { + this(new XMLElement()); this.stack = null; this.root = null; - //this(new XMLElement()); } - public StdXMLBuilder(XMLElement parent) - { + public StdXMLBuilder(XMLElement parent) { this.parent = parent; } - /** - * Creates the builder. - * - * @param prototype the prototype to use when building the tree. - */ -// public StdXMLBuilder(XMLElement prototype) -// { -// this.stack = null; -// this.root = null; -// this.prototype = prototype; -// } - - /** * Cleans up the object when it's destroyed. */ @@ -178,7 +152,7 @@ public class StdXMLBuilder if (this.stack.empty()) { //System.out.println("setting root"); - parent.set(fullName, nsURI, systemID, lineNr); + parent.init(fullName, nsURI, systemID, lineNr); stack.push(parent); root = parent; } else { @@ -232,11 +206,11 @@ public class StdXMLBuilder XMLElement elt = (XMLElement) this.stack.pop(); if (elt.getChildCount() == 1) { - XMLElement child = elt.getChildAtIndex(0); + XMLElement child = elt.getChild(0); if (child.getLocalName() == null) { elt.setContent(child.getContent()); - elt.removeChildAtIndex(0); + elt.removeChild(0); } } } @@ -276,15 +250,15 @@ public class StdXMLBuilder if (top.hasAttribute(fullName)) { throw new XMLParseException(top.getSystemID(), - top.getLineNr(), + top.getLine(), "Duplicate attribute: " + key); } - if (nsPrefix != null) { - top.setAttribute(fullName, nsURI, value); - } else { - top.setAttribute(fullName, value); - } +// if (nsPrefix != null) { +// top.setAttribute(fullName, nsURI, value); +// } else { + top.setString(fullName, value); +// } } diff --git a/android/core/src/processing/xml/StdXMLParser.java b/android/core/src/processing/xml/StdXMLParser.java index da60bbb5d..b38f9d73c 100644 --- a/android/core/src/processing/xml/StdXMLParser.java +++ b/android/core/src/processing/xml/StdXMLParser.java @@ -497,17 +497,18 @@ public class StdXMLParser { attrTypes.addElement("CDATA"); } - for (int i = 0; i < attrNames.size(); i++) { - String key = (String) attrNames.elementAt(i); - String value = (String) attrValues.elementAt(i); - //String type = (String) attrTypes.elementAt(i); + // post 1.2.1, just treat namespaces like any other attribute +// for (int i = 0; i < attrNames.size(); i++) { +// String key = (String) attrNames.elementAt(i); +// String value = (String) attrValues.elementAt(i); +// //String type = (String) attrTypes.elementAt(i); - if (key.equals("xmlns")) { - defaultNamespace = value; - } else if (key.startsWith("xmlns:")) { - namespaces.put(key.substring(6), value); - } - } +// if (key.equals("xmlns")) { +// defaultNamespace = value; +// } else if (key.startsWith("xmlns:")) { +// namespaces.put(key.substring(6), value); +// } +// } if (prefix == null) { this.builder.startElement(name, prefix, defaultNamespace, @@ -523,9 +524,9 @@ public class StdXMLParser { for (int i = 0; i < attrNames.size(); i++) { String key = (String) attrNames.elementAt(i); - if (key.startsWith("xmlns")) { - continue; - } +// if (key.startsWith("xmlns")) { +// continue; +// } String value = (String) attrValues.elementAt(i); String type = (String) attrTypes.elementAt(i); diff --git a/android/core/src/processing/xml/XMLAttribute.java b/android/core/src/processing/xml/XMLAttribute.java index 759dd47fc..ebb71d1f0 100644 --- a/android/core/src/processing/xml/XMLAttribute.java +++ b/android/core/src/processing/xml/XMLAttribute.java @@ -43,13 +43,13 @@ class XMLAttribute /** * The full name of the attribute. */ - private String fullName; + private String name; /** * The short name of the attribute. */ - private String name; + private String localName; /** @@ -85,8 +85,8 @@ class XMLAttribute String value, String type) { - this.fullName = fullName; - this.name = name; + this.name = fullName; + this.localName = name; this.namespace = namespace; this.value = value; this.type = type; @@ -96,18 +96,18 @@ class XMLAttribute /** * Returns the full name of the attribute. */ - String getFullName() + String getName() { - return this.fullName; + return this.name; } /** * Returns the short name of the attribute. */ - String getName() + String getLocalName() { - return this.name; + return this.localName; } diff --git a/android/core/src/processing/xml/XMLElement.java b/android/core/src/processing/xml/XMLElement.java index 944df0cd8..91af0cfaa 100644 --- a/android/core/src/processing/xml/XMLElement.java +++ b/android/core/src/processing/xml/XMLElement.java @@ -31,6 +31,10 @@ import processing.core.PApplet; /** + * XMLElement is a representation of an XML object. The object is able to parse XML code. The methods described here are the most basic. More are documented in the Developer's Reference. + *

+ * The encoding parameter inside XML files is ignored, only UTF-8 (or plain ASCII) are parsed properly. + * =advanced * XMLElement is an XML element. This is the base class used for the * Processing XML library, representing a single node of an XML tree. * @@ -38,115 +42,72 @@ import processing.core.PApplet; * * @author Marc De Scheemaecker * @author processing.org + * + * @webref data:composite + * @usage Web & Application + * @instanceName xml any variable of type XMLElement */ public class XMLElement implements Serializable { + /** No line number defined. */ + public static final int NO_LINE = -1; - /** - * No line number defined. - */ - public static final int NO_LINE = -1; + /** The sketch creating this element. */ + private PApplet sketch; + + /** The parent element. */ + private XMLElement parent; + + /** The attributes of the element. */ + private Vector attributes; + + /** The child elements. */ + private Vector children; + + /** The name of the element. */ + private String name; + + /** The full name of the element (includes the namespace). */ + private String fullName; + + /** The namespace URI. */ + private String namespace; + + /** The content of the element. */ + private String content; + + /** The system ID of the source data where this element is located. */ + private String systemID; + + /** The line in the source data where this element starts. */ + private int line; - /** - * The parent element. - */ - private XMLElement parent; + /** + * Creates an empty element to be used for #PCDATA content. + * @nowebref + */ + public XMLElement() { + this(null, null, null, NO_LINE); + } - /** - * The attributes of the element. - */ - private Vector attributes; + /** + * Creates an empty element. + * + * @param fullName the name of the element. + */ + public XMLElement(String fullName) { + this(fullName, null, null, NO_LINE); + } - /** - * The child elements. - */ - private Vector children; - - - /** - * The name of the element. - */ - private String name; - - - /** - * The full name of the element. - */ - private String fullName; - - - /** - * The namespace URI. - */ - private String namespace; - - - /** - * The content of the element. - */ - private String content; - - - /** - * The system ID of the source data where this element is located. - */ - private String systemID; - - - /** - * The line in the source data where this element starts. - */ - private int lineNr; - - - /** - * Creates an empty element to be used for #PCDATA content. - */ - public XMLElement() { - this(null, null, null, NO_LINE); - } - - - protected void set(String fullName, - String namespace, - String systemID, - int lineNr) { - this.fullName = fullName; - if (namespace == null) { - this.name = fullName; - } else { - int index = fullName.indexOf(':'); - if (index >= 0) { - this.name = fullName.substring(index + 1); - } else { - this.name = fullName; - } - } - this.namespace = namespace; - this.lineNr = lineNr; - this.systemID = systemID; - } - - - /** - * Creates an empty element. - * - * @param fullName the name of the element. - */ -// public XMLElement(String fullName) { -// this(fullName, null, null, NO_LINE); -// } - - - /** - * Creates an empty element. - * - * @param fullName the name of the element. - * @param systemID the system ID of the XML data where the element starts. - * @param lineNr the line in the XML data where the element starts. - */ +// /** +// * Creates an empty element. +// * +// * @param fullName the name of the element. +// * @param systemID the system ID of the XML data where the element starts. +// * @param lineNr the line in the XML data where the element starts. +// */ // public XMLElement(String fullName, // String systemID, // int lineNr) { @@ -154,133 +115,151 @@ public class XMLElement implements Serializable { // } - /** - * Creates an empty element. - * - * @param fullName the full name of the element - * @param namespace the namespace URI. - */ +// /** +// * Creates an empty element. +// * +// * @param fullName the full name of the element +// * @param namespace the namespace URI. +// */ // public XMLElement(String fullName, // String namespace) { // this(fullName, namespace, null, NO_LINE); // } - /** - * Creates an empty element. - * - * @param fullName the full name of the element - * @param namespace the namespace URI. - * @param systemID the system ID of the XML data where the element starts. - * @param lineNr the line in the XML data where the element starts. - */ - public XMLElement(String fullName, + /** + * Creates an empty element. (Used internally for parsing/building.) + * + * @param fullName the full name of the element + * @param namespace the namespace URI. + * @param systemID the system ID of the XML data where the element starts. + * @param lineNr the line in the XML data where the element starts. + * @nowebref + */ + public XMLElement(String fullName, + String namespace, + String systemID, + int lineNr) { + this.attributes = new Vector(); + this.children = new Vector(8); + this.fullName = fullName; + if (namespace == null) { + this.name = fullName; + } else { + int index = fullName.indexOf(':'); + if (index >= 0) { + this.name = fullName.substring(index + 1); + } else { + this.name = fullName; + } + } + this.namespace = namespace; + this.content = null; + this.line = lineNr; + this.systemID = systemID; + this.parent = null; + } + + + /** + * Begin parsing XML data passed in from a PApplet. This code + * wraps exception handling, for more advanced exception handling, + * use the constructor that takes a Reader or InputStream. + * @author processing.org + * @param filename name of the XML file to load + * @param parent typically use "this" + */ + public XMLElement(PApplet sketch, String filename) { + this(); + this.sketch = sketch; + init(sketch.createReader(filename)); + } + + + public XMLElement(Reader reader) { + this(); + init(reader); + } + + + static public XMLElement parse(String xml) { + return parse(new StringReader(xml)); + } + + + static public XMLElement parse(Reader r) { + try { + StdXMLParser parser = new StdXMLParser(); + parser.setBuilder(new StdXMLBuilder()); + parser.setValidator(new XMLValidator()); + parser.setReader(new StdXMLReader(r)); + return (XMLElement) parser.parse(); + } catch (XMLException e) { + e.printStackTrace(); + return null; + } + } + + + protected void init(String fullName, String namespace, String systemID, - int lineNr) { - this.attributes = new Vector(); - this.children = new Vector(8); - this.fullName = fullName; - if (namespace == null) { - this.name = fullName; - } else { - int index = fullName.indexOf(':'); - if (index >= 0) { - this.name = fullName.substring(index + 1); - } else { - this.name = fullName; - } - } - this.namespace = namespace; - this.content = null; - this.lineNr = lineNr; - this.systemID = systemID; - this.parent = null; + int lineNr) { + this.fullName = fullName; + if (namespace == null) { + this.name = fullName; + } else { + int index = fullName.indexOf(':'); + if (index >= 0) { + this.name = fullName.substring(index + 1); + } else { + this.name = fullName; + } } + this.namespace = namespace; + this.line = lineNr; + this.systemID = systemID; + } - /** - * Begin parsing XML data passed in from a PApplet. This code - * wraps exception handling, for more advanced exception handling, - * use the constructor that takes a Reader or InputStream. - * @author processing.org - * @param filename - * @param parent - */ - public XMLElement(PApplet parent, String filename) { - this(); - parseFromReader(parent.createReader(filename)); + protected void init(Reader r) { + try { + StdXMLParser parser = new StdXMLParser(); + parser.setBuilder(new StdXMLBuilder(this)); + parser.setValidator(new XMLValidator()); + parser.setReader(new StdXMLReader(r)); + parser.parse(); + } catch (XMLException e) { + e.printStackTrace(); } + } - public XMLElement(Reader r) { - this(); - parseFromReader(r); - } - - - public XMLElement(String xml) { - this(); - parseFromReader(new StringReader(xml)); - } - - - protected void parseFromReader(Reader r) { - try { - StdXMLParser parser = new StdXMLParser(); - parser.setBuilder(new StdXMLBuilder(this)); - parser.setValidator(new XMLValidator()); - parser.setReader(new StdXMLReader(r)); - //System.out.println(parser.parse().getName()); - /*XMLElement xm = (XMLElement)*/ parser.parse(); - //System.out.println("xm name is " + xm.getName()); - //System.out.println(xm + " " + this); - //parser.parse(); - } catch (XMLException e) { - e.printStackTrace(); - } - } - - -// static public XMLElement parse(Reader r) { -// try { -// StdXMLParser parser = new StdXMLParser(); -// parser.setBuilder(new StdXMLBuilder()); -// parser.setValidator(new XMLValidator()); -// parser.setReader(new StdXMLReader(r)); -// return (XMLElement) parser.parse(); -// } catch (XMLException e) { -// e.printStackTrace(); -// return null; -// } +// /** +// * Creates an element to be used for #PCDATA content. +// */ +// public XMLElement createPCDataElement() { +// return new XMLElement(); // } - /** - * Creates an element to be used for #PCDATA content. - */ - public XMLElement createPCDataElement() { - return new XMLElement(); - } - - - /** - * Creates an empty element. - * - * @param fullName the name of the element. - */ +// /** +// * Creates an empty element. +// * +// * @param fullName the name of the element. +// */ // public XMLElement createElement(String fullName) { -// return new XMLElement(fullName); +// return new XMLElement(fullName); // } - /** - * Creates an empty element. - * - * @param fullName the name of the element. - * @param systemID the system ID of the XML data where the element starts. - * @param lineNr the line in the XML data where the element starts. - */ +// /** +// * Creates an empty element. +// * +// * @param fullName the name of the element. +// * @param systemID the system ID of the XML data where the element starts. +// * @param lineNr the line in the XML data where the element starts. +// */ // public XMLElement createElement(String fullName, // String systemID, // int lineNr) { @@ -289,231 +268,236 @@ public class XMLElement implements Serializable { // } - /** - * Creates an empty element. - * - * @param fullName the full name of the element - * @param namespace the namespace URI. - */ - public XMLElement createElement(String fullName, - String namespace) { - //return new XMLElement(fullName, namespace); - return new XMLElement(fullName, namespace, null, NO_LINE); +// /** +// * Creates an empty element. +// * +// * @param fullName the full name of the element +// * @param namespace the namespace URI. +// */ +// public XMLElement createElement(String fullName, +// String namespace) { +// //return new XMLElement(fullName, namespace); +// return new XMLElement(fullName, namespace, null, NO_LINE); +// } + + +// /** +// * Creates an empty element. +// * +// * @param fullName the full name of the element +// * @param namespace the namespace URI. +// * @param systemID the system ID of the XML data where the element starts. +// * @param lineNr the line in the XML data where the element starts. +// */ +// public XMLElement createElement(String fullName, +// String namespace, +// String systemID, +// int lineNr) { +// return new XMLElement(fullName, namespace, systemID, lineNr); +// } + + + /** + * Cleans up the object when it's destroyed. + */ + protected void finalize() throws Throwable { + this.attributes.clear(); + this.attributes = null; + this.children = null; + this.fullName = null; + this.name = null; + this.namespace = null; + this.content = null; + this.systemID = null; + this.parent = null; + super.finalize(); + } + + + /** + * Returns the parent element. This method returns null for the root + * element. + */ + public XMLElement getParent() { + return this.parent; + } + + + /** + * Returns the full name (i.e. the name including an eventual namespace + * prefix) of the element. + * + * @webref + * @brief Returns the name of the element. + * @return the name, or null if the element only contains #PCDATA. + */ + public String getName() { + return this.fullName; + } + + + /** + * Returns the name of the element without its namespace. + * + * @return the name, or null if the element only contains #PCDATA. + */ + public String getLocalName() { + return this.name; + } + + + /** + * Returns the namespace of the element. + * + * @return the namespace, or null if no namespace is associated with the + * element. + */ + public String getNamespace() { + return this.namespace; + } + + + /** + * Sets the full name. This method also sets the short name and clears the + * namespace URI. + * + * @param name the non-null name. + */ + public void setName(String name) { + this.name = name; + this.fullName = name; + this.namespace = null; + } + + + /** + * Sets the name. + * + * @param fullName the non-null full name. + * @param namespace the namespace URI, which may be null. + */ + public void setName(String fullName, String namespace) { + int index = fullName.indexOf(':'); + if ((namespace == null) || (index < 0)) { + this.name = fullName; + } else { + this.name = fullName.substring(index + 1); } + this.fullName = fullName; + this.namespace = namespace; + } - /** - * Creates an empty element. - * - * @param fullName the full name of the element - * @param namespace the namespace URI. - * @param systemID the system ID of the XML data where the element starts. - * @param lineNr the line in the XML data where the element starts. - */ - public XMLElement createElement(String fullName, - String namespace, - String systemID, - int lineNr) { - return new XMLElement(fullName, namespace, systemID, lineNr); + /** + * Adds a child element. + * + * @param child the non-null child to add. + */ + public void addChild(XMLElement child) { + if (child == null) { + throw new IllegalArgumentException("child must not be null"); } + if ((child.getLocalName() == null) && (! this.children.isEmpty())) { + XMLElement lastChild = (XMLElement) this.children.lastElement(); - - /** - * Cleans up the object when it's destroyed. - */ - protected void finalize() throws Throwable { - this.attributes.clear(); - this.attributes = null; - this.children = null; - this.fullName = null; - this.name = null; - this.namespace = null; - this.content = null; - this.systemID = null; - this.parent = null; - super.finalize(); + if (lastChild.getLocalName() == null) { + lastChild.setContent(lastChild.getContent() + + child.getContent()); + return; + } } + ((XMLElement)child).parent = this; + this.children.addElement(child); + } - /** - * Returns the parent element. This method returns null for the root - * element. - */ - public XMLElement getParent() { - return this.parent; + /** + * Inserts a child element. + * + * @param child the non-null child to add. + * @param index where to put the child. + */ + public void insertChild(XMLElement child, int index) { + if (child == null) { + throw new IllegalArgumentException("child must not be null"); } - - - /** - * Returns the full name (i.e. the name including an eventual namespace - * prefix) of the element. - * - * @return the name, or null if the element only contains #PCDATA. - */ - public String getName() { - return this.fullName; + if ((child.getLocalName() == null) && (! this.children.isEmpty())) { + XMLElement lastChild = (XMLElement) this.children.lastElement(); + if (lastChild.getLocalName() == null) { + lastChild.setContent(lastChild.getContent() + + child.getContent()); + return; + } } + ((XMLElement) child).parent = this; + this.children.insertElementAt(child, index); + } - /** - * Returns the name of the element. - * - * @return the name, or null if the element only contains #PCDATA. - */ - public String getLocalName() { - return this.name; + /** + * Removes a child element. + * + * @param child the non-null child to remove. + */ + public void removeChild(XMLElement child) { + if (child == null) { + throw new IllegalArgumentException("child must not be null"); } + this.children.removeElement(child); + } - /** - * Returns the namespace of the element. - * - * @return the namespace, or null if no namespace is associated with the - * element. - */ - public String getNamespace() { - return this.namespace; - } + /** + * Removes the child located at a certain index. + * + * @param index the index of the child, where the first child has index 0. + */ + public void removeChild(int index) { + children.removeElementAt(index); + } - /** - * Sets the full name. This method also sets the short name and clears the - * namespace URI. - * - * @param name the non-null name. - */ - public void setName(String name) { - this.name = name; - this.fullName = name; - this.namespace = null; - } +// /** +// * Returns an enumeration of all child elements. +// * +// * @return the non-null enumeration +// */ +// public Enumeration enumerateChildren() { +// return this.children.elements(); +// } - /** - * Sets the name. - * - * @param fullName the non-null full name. - * @param namespace the namespace URI, which may be null. - */ - public void setName(String fullName, String namespace) { - int index = fullName.indexOf(':'); - if ((namespace == null) || (index < 0)) { - this.name = fullName; - } else { - this.name = fullName.substring(index + 1); - } - this.fullName = fullName; - this.namespace = namespace; - } + /** + * Returns whether the element is a leaf element. + * + * @return true if the element has no children. + */ + public boolean isLeaf() { + return children.isEmpty(); + } - /** - * Adds a child element. - * - * @param child the non-null child to add. - */ - public void addChild(XMLElement child) { - if (child == null) { - throw new IllegalArgumentException("child must not be null"); - } - if ((child.getLocalName() == null) && (! this.children.isEmpty())) { - XMLElement lastChild = (XMLElement) this.children.lastElement(); - - if (lastChild.getLocalName() == null) { - lastChild.setContent(lastChild.getContent() - + child.getContent()); - return; - } - } - ((XMLElement)child).parent = this; - this.children.addElement(child); - } + /** + * Returns whether the element has children. + * + * @return true if the element has children. + */ + public boolean hasChildren() { + return (!children.isEmpty()); + } - /** - * Inserts a child element. - * - * @param child the non-null child to add. - * @param index where to put the child. - */ - public void insertChild(XMLElement child, int index) { - if (child == null) { - throw new IllegalArgumentException("child must not be null"); - } - if ((child.getLocalName() == null) && (! this.children.isEmpty())) { - XMLElement lastChild = (XMLElement) this.children.lastElement(); - if (lastChild.getLocalName() == null) { - lastChild.setContent(lastChild.getContent() - + child.getContent()); - return; - } - } - ((XMLElement) child).parent = this; - this.children.insertElementAt(child, index); - } - - - /** - * Removes a child element. - * - * @param child the non-null child to remove. - */ - public void removeChild(XMLElement child) { - if (child == null) { - throw new IllegalArgumentException("child must not be null"); - } - this.children.removeElement(child); - } - - - /** - * Removes the child located at a certain index. - * - * @param index the index of the child, where the first child has index 0. - */ - public void removeChildAtIndex(int index) { - this.children.removeElementAt(index); - } - - - /** - * Returns an enumeration of all child elements. - * - * @return the non-null enumeration - */ - public Enumeration enumerateChildren() { - return this.children.elements(); - } - - - /** - * Returns whether the element is a leaf element. - * - * @return true if the element has no children. - */ - public boolean isLeaf() { - return this.children.isEmpty(); - } - - - /** - * Returns whether the element has children. - * - * @return true if the element has children. - */ - public boolean hasChildren() { - return (! this.children.isEmpty()); - } - - - /** - * Returns the number of children. - * - * @return the count. - */ - public int getChildCount() { - return this.children.size(); - } + /** + * Returns the number of children for the element. + * + * @return the count. + * @webref + * @see processing.xml.XMLElement#getChild(int) + * @see processing.xml.XMLElement#getChildren(String) + */ + public int getChildCount() { + return this.children.size(); + } /** @@ -526,807 +510,985 @@ public class XMLElement implements Serializable { // } - /** - * Returns an array containing all the child elements. - */ - public XMLElement[] getChildren() { - int childCount = getChildCount(); - XMLElement[] kids = new XMLElement[childCount]; - children.copyInto(kids); - return kids; + /** + * Put the names of all children into an array. Same as looping through + * each child and calling getName() on each XMLElement. + */ + public String[] listChildren() { + int childCount = getChildCount(); + String[] outgoing = new String[childCount]; + for (int i = 0; i < childCount; i++) { + outgoing[i] = getChild(i).getName(); } + return outgoing; + } + + + /** + * Returns an array containing all the child elements. + */ + public XMLElement[] getChildren() { + int childCount = getChildCount(); + XMLElement[] kids = new XMLElement[childCount]; + children.copyInto(kids); + return kids; + } - /** - * Quick accessor for an element at a particular index. - * @author processing.org - */ - public XMLElement getChild(int which) { - return (XMLElement) children.elementAt(which); + /** + * Quick accessor for an element at a particular index. + * @author processing.org + * @param index the element + */ + public XMLElement getChild(int index) { + return (XMLElement) children.elementAt(index); + } + + + /** + * Returns the child XMLElement as specified by the index parameter. The value of the index parameter must be less than the total number of children to avoid going out of the array storing the child elements. + * When the path parameter is specified, then it will return all children that match that path. The path is a series of elements and sub-elements, separated by slashes. + * + * @return the element + * @author processing.org + * + * @webref + * @see processing.xml.XMLElement#getChildCount() + * @see processing.xml.XMLElement#getChildren(String) + * @brief Get a child by its name or path. + * @param path path to a particular element + */ + public XMLElement getChild(String path) { + if (path.indexOf('/') != -1) { + return getChildRecursive(PApplet.split(path, '/'), 0); } + int childCount = getChildCount(); + for (int i = 0; i < childCount; i++) { + XMLElement kid = getChild(i); + String kidName = kid.getName(); + if (kidName != null && kidName.equals(path)) { + return kid; + } + } + return null; + } - /** - * Get a child by its name or path. - * @param name element name or path/to/element - * @return the element - * @author processing.org - */ - public XMLElement getChild(String name) { - if (name.indexOf('/') != -1) { - return getChildRecursive(PApplet.split(name, '/'), 0); + /** + * Internal helper function for getChild(String). + * @param items result of splitting the query on slashes + * @param offset where in the items[] array we're currently looking + * @return matching element or null if no match + * @author processing.org + */ + protected XMLElement getChildRecursive(String[] items, int offset) { + // if it's a number, do an index instead + if (Character.isDigit(items[offset].charAt(0))) { + XMLElement kid = getChild(Integer.parseInt(items[offset])); + if (offset == items.length-1) { + return kid; + } else { + return kid.getChildRecursive(items, offset+1); + } + } + int childCount = getChildCount(); + for (int i = 0; i < childCount; i++) { + XMLElement kid = getChild(i); + String kidName = kid.getName(); + if (kidName != null && kidName.equals(items[offset])) { + if (offset == items.length-1) { + return kid; + } else { + return kid.getChildRecursive(items, offset+1); } - int childCount = getChildCount(); - for (int i = 0; i < childCount; i++) { - XMLElement kid = getChild(i); - if (kid.getName().equals(name)) { - return kid; - } - } - return null; + } } + return null; + } - /** - * Internal helper function for getChild(String). - * @param items result of splitting the query on slashes - * @param offset where in the items[] array we're currently looking - * @return matching element or null if no match - * @author processing.org - */ - protected XMLElement getChildRecursive(String[] items, int offset) { - // if it's a number, do an index instead - if (Character.isDigit(items[offset].charAt(0))) { - XMLElement kid = getChild(Integer.parseInt(items[offset])); - if (offset == items.length-1) { - return kid; - } else { - return kid.getChildRecursive(items, offset+1); - } - } - int childCount = getChildCount(); - for (int i = 0; i < childCount; i++) { - XMLElement kid = getChild(i); - if (kid.getName().equals(items[offset])) { - if (offset == items.length-1) { - return kid; - } else { - return kid.getChildRecursive(items, offset+1); - } - } - } - return null; +// /** +// * Returns the child at a specific index. +// * +// * @param index the index of the child +// * +// * @return the non-null child +// * +// * @throws java.lang.ArrayIndexOutOfBoundsException +// * if the index is out of bounds. +// */ +// public XMLElement getChildAtIndex(int index) throws ArrayIndexOutOfBoundsException { +// return (XMLElement) this.children.elementAt(index); +// } + + + /** + * Returns all of the children as an XMLElement array. + * When the path parameter is specified, then it will return all children that match that path. + * The path is a series of elements and sub-elements, separated by slashes. + * + * @param path element name or path/to/element + * @return array of child elements that match + * @author processing.org + * + * @webref + * @brief Returns all of the children as an XMLElement array. + * @see processing.xml.XMLElement#getChildCount() + * @see processing.xml.XMLElement#getChild(int) + */ + public XMLElement[] getChildren(String path) { + if (path.indexOf('/') != -1) { + return getChildrenRecursive(PApplet.split(path, '/'), 0); } - - - /** - * Returns the child at a specific index. - * - * @param index the index of the child - * - * @return the non-null child - * - * @throws java.lang.ArrayIndexOutOfBoundsException - * if the index is out of bounds. - */ - public XMLElement getChildAtIndex(int index) - throws ArrayIndexOutOfBoundsException { - return (XMLElement) this.children.elementAt(index); + // if it's a number, do an index instead + // (returns a single element array, since this will be a single match + if (Character.isDigit(path.charAt(0))) { + return new XMLElement[] { getChild(Integer.parseInt(path)) }; } + int childCount = getChildCount(); + XMLElement[] matches = new XMLElement[childCount]; + int matchCount = 0; + for (int i = 0; i < childCount; i++) { + XMLElement kid = getChild(i); + String kidName = kid.getName(); + if (kidName != null && kidName.equals(path)) { + matches[matchCount++] = kid; + } + } + return (XMLElement[]) PApplet.subset(matches, 0, matchCount); + } - /** - * Searches a child element. - * - * @param name the full name of the child to search for. - * - * @return the child element, or null if no such child was found. - */ -// public XMLElement getFirstChildNamed(String name) { -// Enumeration enum = this.children.elements(); -// while (enum.hasMoreElements()) { -// XMLElement child = (XMLElement) enum.nextElement(); -// String childName = child.getFullName(); -// if ((childName != null) && childName.equals(name)) { -// return child; -// } -// } -// return null; -// } + protected XMLElement[] getChildrenRecursive(String[] items, int offset) { + if (offset == items.length-1) { + return getChildren(items[offset]); + } + XMLElement[] matches = getChildren(items[offset]); + XMLElement[] outgoing = new XMLElement[0]; + for (int i = 0; i < matches.length; i++) { + XMLElement[] kidMatches = matches[i].getChildrenRecursive(items, offset+1); + outgoing = (XMLElement[]) PApplet.concat(outgoing, kidMatches); + } + return outgoing; + } - /** - * Searches a child element. - * - * @param name the name of the child to search for. - * @param namespace the namespace, which may be null. - * - * @return the child element, or null if no such child was found. - */ -// public XMLElement getFirstChildNamed(String name, -// String namespace) { -// Enumeration enum = this.children.elements(); -// while (enum.hasMoreElements()) { -// XMLElement child = (XMLElement) enum.nextElement(); -// String str = child.getName(); -// boolean found = (str != null) && (str.equals(name)); -// str = child.getNamespace(); -// if (str == null) { -// found &= (name == null); + /** + * Searches an attribute. + * + * @param fullName the non-null full name of the attribute. + * + * @return the attribute, or null if the attribute does not exist. + */ + private XMLAttribute findAttribute(String fullName) { + Enumeration en = this.attributes.elements(); + while (en.hasMoreElements()) { + XMLAttribute attr = (XMLAttribute) en.nextElement(); + if (attr.getName().equals(fullName)) { + return attr; + } + } + return null; + } + + +// /** +// * Searches an attribute. +// * +// * @param name the non-null short name of the attribute. +// * @param namespace the name space, which may be null. +// * +// * @return the attribute, or null if the attribute does not exist. +// */ +// private XMLAttribute findAttribute(String name, +// String namespace) { +// Enumeration en = this.attributes.elements(); +// while (en.hasMoreElements()) { +// XMLAttribute attr = (XMLAttribute) en.nextElement(); +// boolean found = attr.getName().equals(name); +// if (namespace == null) { +// found &= (attr.getNamespace() == null); // } else { -// found &= str.equals(namespace); -// } -// if (found) { -// return child; -// } -// } -// return null; -// } - - - /** - * Get any children that match this name or path. Similar to getChild(), - * but will grab multiple matches rather than only the first. - * @param name element name or path/to/element - * @return array of child elements that match - * @author processing.org - */ - public XMLElement[] getChildren(String name) { - if (name.indexOf('/') != -1) { - return getChildrenRecursive(PApplet.split(name, '/'), 0); - } - // if it's a number, do an index instead - // (returns a single element array, since this will be a single match - if (Character.isDigit(name.charAt(0))) { - return new XMLElement[] { getChild(Integer.parseInt(name)) }; - } - int childCount = getChildCount(); - XMLElement[] matches = new XMLElement[childCount]; - int matchCount = 0; - for (int i = 0; i < childCount; i++) { - XMLElement kid = getChild(i); - if (kid.getName().equals(name)) { - matches[matchCount++] = kid; - } - } - return (XMLElement[]) PApplet.subset(matches, 0, matchCount); - } - - - protected XMLElement[] getChildrenRecursive(String[] items, int offset) { - if (offset == items.length-1) { - return getChildren(items[offset]); - } - XMLElement[] matches = getChildren(items[offset]); - XMLElement[] outgoing = new XMLElement[0]; - for (int i = 0; i < matches.length; i++) { - XMLElement[] kidMatches = matches[i].getChildrenRecursive(items, offset+1); - outgoing = (XMLElement[]) PApplet.concat(outgoing, kidMatches); - } - return outgoing; - } - - - /** - * Returns a vector of all child elements named name. - * - * @param name the full name of the children to search for. - * - * @return the non-null vector of child elements. - */ -// public Vector getChildrenNamed(String name) { -// Vector result = new Vector(this.children.size()); -// Enumeration enum = this.children.elements(); -// while (enum.hasMoreElements()) { -// XMLElement child = (XMLElement) enum.nextElement(); -// String childName = child.getFullName(); -// if ((childName != null) && childName.equals(name)) { -// result.addElement(child); -// } -// } -// return result; -// } - - - /** - * Returns a vector of all child elements named name. - * - * @param name the name of the children to search for. - * @param namespace the namespace, which may be null. - * - * @return the non-null vector of child elements. - */ -// public Vector getChildrenNamed(String name, -// String namespace) { -// Vector result = new Vector(this.children.size()); -// Enumeration enum = this.children.elements(); -// while (enum.hasMoreElements()) { -// XMLElement child = (XMLElement) enum.nextElement(); -// String str = child.getName(); -// boolean found = (str != null) && (str.equals(name)); -// str = child.getNamespace(); -// if (str == null) { -// found &= (name == null); -// } else { -// found &= str.equals(namespace); +// found &= namespace.equals(attr.getNamespace()); // } // // if (found) { -// result.addElement(child); +// return attr; +// } +// } +// return null; +// } + + + /** + * Returns the number of attributes. + */ + public int getAttributeCount() { + return attributes.size(); + } + + + public String[] listAttributes() { + String[] outgoing = new String[attributes.size()]; + for (int i = 0; i < attributes.size(); i++) { + outgoing[i] = attributes.get(i).getName(); + } + return outgoing; + } + + + /** + * Returns the value of an attribute. + * @param name the non-null name of the attribute. + * @return the value, or null if the attribute does not exist. + */ + public String getAttribute(String name) { + return getAttribute(name, null); + } + + + /** + * Returns the value of an attribute. + * + * @param name the non-null full name of the attribute. + * @param defaultValue the default value of the attribute. + * + * @return the value, or defaultValue if the attribute does not exist. + */ + public String getAttribute(String name, String defaultValue) { + XMLAttribute attr = this.findAttribute(name); + if (attr == null) { + return defaultValue; + } else { + return attr.getValue(); + } + } + + +// /** +// * Returns the value of an attribute. +// * +// * @param name the non-null name of the attribute. +// * @param namespace the namespace URI, which may be null. +// * @param defaultValue the default value of the attribute. +// * +// * @return the value, or defaultValue if the attribute does not exist. +// * @deprecated namespace code is more trouble than it's worth +// */ +// public String getAttribute(String name, +// String namespace, +// String defaultValue) { +// XMLAttribute attr = this.findAttribute(name, namespace); +// if (attr == null) { +// return defaultValue; +// } else { +// return attr.getValue(); +// } +// } + + + /** + * @deprecated use getString() or getAttribute() + */ + public String getStringAttribute(String name) { + return getAttribute(name); + } + + + /** + * Returns a String attribute of the element. + * If the default parameter is used and the attribute doesn't exist, the default value is returned. + * When using the version of the method without the default parameter, if the attribute doesn't exist, the value 0 is returned. + * + * @webref + * @param name the name of the attribute + * @param default Value value returned if the attribute is not found + * + * @brief Returns a String attribute of the element. + * @deprecated use getString() or getAttribute() + */ + public String getStringAttribute(String name, String defaultValue) { + return getAttribute(name, defaultValue); + } + + +// /** +// * @deprecated namespace code is more trouble than it's worth +// */ +// public String getStringAttribute(String name, +// String namespace, +// String defaultValue) { +// return getAttribute(name, namespace, defaultValue); +// } + + + public String getString(String name) { + return getAttribute(name); + } + + + public String getString(String name, String defaultValue) { + return getAttribute(name, defaultValue); + } + + + /** + * Returns a boolean attribute of the element. + */ + public boolean getBoolean(String name) { + return getBoolean(name, false); + } + + + /** + * Returns a boolean attribute of the element. + * If the defaultValue parameter is used and the attribute doesn't exist, the defaultValue is returned. + * When using the version of the method without the defaultValue parameter, if the attribute doesn't exist, the value false is returned. + * + * @param name the name of the attribute + * @param defaultValue value returned if the attribute is not found + * + * @webref + * @brief Returns a boolean attribute of the element. + * @return the value, or defaultValue if the attribute does not exist. + */ + public boolean getBoolean(String name, boolean defaultValue) { + String value = this.getAttribute(name, Boolean.toString(defaultValue)); + return value.equals("1") || value.toLowerCase().equals("true"); + } + + +// /** +// * Returns the value of an attribute. +// * +// * @param name the non-null name of the attribute. +// * @param namespace the namespace URI, which may be null. +// * @param defaultValue the default value of the attribute. +// * +// * @return the value, or defaultValue if the attribute does not exist. +// */ +// public boolean getBooleanAttribute(String name, +// String namespace, +// boolean defaultValue) { +// String value = this.getAttribute(name, namespace, +// Boolean.toString(defaultValue)); +// return value.equals("1") || value.toLowerCase().equals("true"); +// } + + + /** + * @deprecated use getInt() instead + */ + public int getIntAttribute(String name) { + return getInt(name, 0); + } + + + /** + * @deprecated use getInt() instead + */ + public int getIntAttribute(String name, int defaultValue) { + return getInt(name, defaultValue); + } + + + /** + * Returns an integer attribute of the element. + */ + public int getInt(String name) { + return getInt(name, 0); + } + + + /** + * Returns an integer attribute of the element. + * If the default parameter is used and the attribute doesn't exist, the default value is returned. + * When using the version of the method without the default parameter, if the attribute doesn't exist, the value 0 is returned. + * + * @param name the name of the attribute + * @param defaultValue value returned if the attribute is not found + * + * @webref + * @brief Returns an integer attribute of the element. + * @return the value, or defaultValue if the attribute does not exist. + */ + public int getInt(String name, int defaultValue) { + String value = this.getAttribute(name, Integer.toString(defaultValue)); + return Integer.parseInt(value); + } + + +// /** +// * Returns the value of an attribute. +// * +// * @param name the non-null name of the attribute. +// * @param namespace the namespace URI, which may be null. +// * @param defaultValue the default value of the attribute. +// * +// * @return the value, or defaultValue if the attribute does not exist. +// * @deprecated namespace code is more trouble than it's worth +// */ +// public int getIntAttribute(String name, +// String namespace, +// int defaultValue) { +// String value = this.getAttribute(name, namespace, +// Integer.toString(defaultValue)); +// return Integer.parseInt(value); +// } + + + /** + * @deprecated use getFloat() instead + */ + public float getFloatAttribute(String name) { + return getFloat(name, 0); + } + + + /** + * @deprecated use getFloat() instead + */ + public float getFloatAttribute(String name, float defaultValue) { + return getFloat(name, 0); + } + + + /** + * Returns a float attribute of the element. + * If the default parameter is used and the attribute doesn't exist, the default value is returned. + * When using the version of the method without the default parameter, if the attribute doesn't exist, the value 0 is returned. + * + * @param name the name of the attribute + * @param defaultValue value returned if the attribute is not found + * + * @return the value, or defaultValue if the attribute does not exist. + * + * @webref + * @brief Returns a float attribute of the element. + */ + public float getFloat(String name, float defaultValue) { + String value = this.getAttribute(name, Float.toString(defaultValue)); + return Float.parseFloat(value); + } + + +// /** +// * Returns the value of an attribute. +// * +// * @param name the non-null name of the attribute. +// * @param namespace the namespace URI, which may be null. +// * @param defaultValue the default value of the attribute. +// * +// * @return the value, or defaultValue if the attribute does not exist. +// * @nowebref +// * @deprecated namespace code is more trouble than it's worth +// */ +// public float getFloatAttribute(String name, +// String namespace, +// float defaultValue) { +// String value = this.getAttribute(name, namespace, +// Float.toString(defaultValue)); +// return Float.parseFloat(value); +// } + + + public double getDouble(String name) { + return getDouble(name, 0); + } + + + /** + * Returns the value of an attribute. + * + * @param name the non-null full name of the attribute. + * @param defaultValue the default value of the attribute. + * + * @return the value, or defaultValue if the attribute does not exist. + */ + public double getDouble(String name, double defaultValue) { + String value = this.getAttribute(name, Double.toString(defaultValue)); + return Double.parseDouble(value); + } + + +// /** +// * Returns the value of an attribute. +// * +// * @param name the non-null name of the attribute. +// * @param namespace the namespace URI, which may be null. +// * @param defaultValue the default value of the attribute. +// * +// * @return the value, or defaultValue if the attribute does not exist. +// */ +// public double getDoubleAttribute(String name, +// String namespace, +// double defaultValue) { +// String value = this.getAttribute(name, namespace, +// Double.toString(defaultValue)); +// return Double.parseDouble(value); +// } + + +// /** +// * Returns the type of an attribute. +// * +// * @param name the non-null full name of the attribute. +// * +// * @return the type, or null if the attribute does not exist. +// */ +// public String getAttributeType(String name) { +// XMLAttribute attr = this.findAttribute(name); +// if (attr == null) { +// return null; +// } else { +// return attr.getType(); +// } +// } + + +// /** +// * Returns the namespace of an attribute. +// * +// * @param name the non-null full name of the attribute. +// * +// * @return the namespace, or null if there is none associated. +// */ +// public String getAttributeNamespace(String name) { +// XMLAttribute attr = this.findAttribute(name); +// if (attr == null) { +// return null; +// } else { +// return attr.getNamespace(); +// } +// } + + +// /** +// * Returns the type of an attribute. +// * +// * @param name the non-null name of the attribute. +// * @param namespace the namespace URI, which may be null. +// * +// * @return the type, or null if the attribute does not exist. +// */ +// public String getAttributeType(String name, +// String namespace) { +// XMLAttribute attr = this.findAttribute(name, namespace); +// if (attr == null) { +// return null; +// } else { +// return attr.getType(); +// } +// } + + +// /** +// * @deprecated use set() +// */ +// public void setAttribute(String name, String value) { +// set(name, value); +// } + + + /** + * Sets an attribute. + * + * @param name the non-null full name of the attribute. + * @param value the non-null value of the attribute. + */ + public void setString(String name, String value) { + XMLAttribute attr = this.findAttribute(name); + if (attr == null) { + attr = new XMLAttribute(name, name, null, value, "CDATA"); + this.attributes.addElement(attr); + } else { + attr.setValue(value); + } + } + + + public void setBoolean(String name, boolean value) { + setString(name, String.valueOf(value)); + } + + + public void setInt(String name, int value) { + setString(name, String.valueOf(value)); + } + + + public void setFloat(String name, float value) { + setString(name, String.valueOf(value)); + } + + + public void setDouble(String name, double value) { + setString(name, String.valueOf(value)); + } + + +// /** +// * Sets an attribute. +// * +// * @param fullName the non-null full name of the attribute. +// * @param namespace the namespace URI of the attribute, which may be null. +// * @param value the non-null value of the attribute. +// */ +// public void setAttribute(String fullName, +// String namespace, +// String value) { +// int index = fullName.indexOf(':'); +// String vorname = fullName.substring(index + 1); +// XMLAttribute attr = this.findAttribute(vorname, namespace); +// if (attr == null) { +// attr = new XMLAttribute(fullName, vorname, namespace, value, "CDATA"); +// this.attributes.addElement(attr); +// } else { +// attr.setValue(value); +// } +// } + + + /** + * Removes an attribute. Formerly removeAttribute(). + * + * @param name the non-null name of the attribute. + */ + public void remove(String name) { + for (int i = 0; i < this.attributes.size(); i++) { + XMLAttribute attr = (XMLAttribute) this.attributes.elementAt(i); + if (attr.getName().equals(name)) { + this.attributes.removeElementAt(i); + return; + } + } + } + + +// /** +// * Removes an attribute. +// * +// * @param name the non-null name of the attribute. +// * @param namespace the namespace URI of the attribute, which may be null. +// */ +// public void removeAttribute(String name, +// String namespace) { +// for (int i = 0; i < this.attributes.size(); i++) { +// XMLAttribute attr = (XMLAttribute) this.attributes.elementAt(i); +// boolean found = attr.getName().equals(name); +// if (namespace == null) { +// found &= (attr.getNamespace() == null); +// } else { +// found &= attr.getNamespace().equals(namespace); +// } +// +// if (found) { +// this.attributes.removeElementAt(i); +// return; +// } +// } +// } + + +// /** +// * Returns an enumeration of all attribute names. +// * +// * @return the non-null enumeration. +// */ +// public Enumeration enumerateAttributeNames() { +// Vector result = new Vector(); +// Enumeration en = this.attributes.elements(); +// while (en.hasMoreElements()) { +// XMLAttribute attr = (XMLAttribute) en.nextElement(); +// result.addElement(attr.getFullName()); +// } +// return result.elements(); +// } + + + /** + * Returns whether an attribute exists. + * + * @return true if the attribute exists. + */ + public boolean hasAttribute(String name) { + return this.findAttribute(name) != null; + } + + +// /** +// * Returns whether an attribute exists. +// * +// * @return true if the attribute exists. +// */ +// public boolean hasAttribute(String name, +// String namespace) { +// return this.findAttribute(name, namespace) != null; +// } + + +// /** +// * Returns all attributes as a Properties object. +// * +// * @return the non-null set. +// */ +// public Properties getAttributes() { +// Properties result = new Properties(); +// Enumeration en = this.attributes.elements(); +// while (en.hasMoreElements()) { +// XMLAttribute attr = (XMLAttribute) en.nextElement(); +// result.put(attr.getFullName(), attr.getValue()); +// } +// return result; +// } + + +// /** +// * Returns all attributes in a specific namespace as a Properties object. +// * +// * @param namespace the namespace URI of the attributes, which may be null. +// * +// * @return the non-null set. +// */ +// public Properties getAttributesInNamespace(String namespace) { +// Properties result = new Properties(); +// Enumeration en = this.attributes.elements(); +// while (en.hasMoreElements()) { +// XMLAttribute attr = (XMLAttribute) en.nextElement(); +// if (namespace == null) { +// if (attr.getNamespace() == null) { +// result.put(attr.getName(), attr.getValue()); +// } +// } else { +// if (namespace.equals(attr.getNamespace())) { +// result.put(attr.getName(), attr.getValue()); +// } // } // } // return result; // } - /** - * Searches an attribute. - * - * @param fullName the non-null full name of the attribute. - * - * @return the attribute, or null if the attribute does not exist. - */ - private XMLAttribute findAttribute(String fullName) { - Enumeration en = this.attributes.elements(); - while (en.hasMoreElements()) { - XMLAttribute attr = (XMLAttribute) en.nextElement(); - if (attr.getFullName().equals(fullName)) { - return attr; - } - } - return null; + /** + * Returns the system ID of the data where the element started. + * + * @return the system ID, or null if unknown. + * + * @see #getLineNr + */ + public String getSystemID() { + return this.systemID; + } + + + /** + * Returns the line number in the data where the element started. + * + * @return the line number, or NO_LINE if unknown. + * + * @see #NO_LINE + * @see #getSystemID + */ + public int getLine() { + return this.line; + } + + + /** + * Returns the content of an element. If there is no such content, null is returned. + * =advanced + * Return the #PCDATA content of the element. If the element has a + * combination of #PCDATA content and child elements, the #PCDATA + * sections can be retrieved as unnamed child objects. In this case, + * this method returns null. + * + * @webref + * @brief Returns the content of an element + * @return the content. + */ + public String getContent() { + return this.content; + } + + + /** + * Sets the #PCDATA content. It is an error to call this method with a + * non-null value if there are child objects. + * + * @param content the (possibly null) content. + */ + public void setContent(String content) { + this.content = content; + } + + + /** + * Returns true if the element equals another element. + * + * @param rawElement the element to compare to + */ + public boolean equals(Object object) { + if (!(object instanceof XMLElement)) { + return false; } + XMLElement rawElement = (XMLElement) object; - - /** - * Searches an attribute. - * - * @param name the non-null short name of the attribute. - * @param namespace the name space, which may be null. - * - * @return the attribute, or null if the attribute does not exist. - */ - private XMLAttribute findAttribute(String name, - String namespace) { - Enumeration en = this.attributes.elements(); - while (en.hasMoreElements()) { - XMLAttribute attr = (XMLAttribute) en.nextElement(); - boolean found = attr.getName().equals(name); - if (namespace == null) { - found &= (attr.getNamespace() == null); - } else { - found &= namespace.equals(attr.getNamespace()); - } - - if (found) { - return attr; - } - } - return null; + if (! this.name.equals(rawElement.getLocalName())) { + return false; } - - - /** - * Returns the number of attributes. - */ - public int getAttributeCount() { - return this.attributes.size(); + if (this.attributes.size() != rawElement.getAttributeCount()) { + return false; } - - - /** - * Returns the value of an attribute. - * - * @param name the non-null name of the attribute. - * - * @return the value, or null if the attribute does not exist. - */ - public String getAttribute(String name) { - return this.getAttribute(name, null); + Enumeration en = this.attributes.elements(); + while (en.hasMoreElements()) { + XMLAttribute attr = (XMLAttribute) en.nextElement(); + // if (! rawElement.hasAttribute(attr.getName(), attr.getNamespace())) { + if (!rawElement.hasAttribute(attr.getName())) { + return false; + } + // String value = rawElement.getAttribute(attr.getName(), + // attr.getNamespace(), + // null); + String value = rawElement.getAttribute(attr.getName(), null); + if (! attr.getValue().equals(value)) { + return false; + } + // String type = + // rawElement.getAttributeType(attr.getName(), attr.getNamespace()); + // if (!attr.getType().equals(type)) { + // return false; + // } } - - - /** - * Returns the value of an attribute. - * - * @param name the non-null full name of the attribute. - * @param defaultValue the default value of the attribute. - * - * @return the value, or defaultValue if the attribute does not exist. - */ - public String getAttribute(String name, - String defaultValue) { - XMLAttribute attr = this.findAttribute(name); - if (attr == null) { - return defaultValue; - } else { - return attr.getValue(); - } + if (this.children.size() != rawElement.getChildCount()) { + return false; } + for (int i = 0; i < this.children.size(); i++) { + XMLElement child1 = this.getChild(i); + XMLElement child2 = rawElement.getChild(i); - - /** - * Returns the value of an attribute. - * - * @param name the non-null name of the attribute. - * @param namespace the namespace URI, which may be null. - * @param defaultValue the default value of the attribute. - * - * @return the value, or defaultValue if the attribute does not exist. - */ - public String getAttribute(String name, - String namespace, - String defaultValue) { - XMLAttribute attr = this.findAttribute(name, namespace); - if (attr == null) { - return defaultValue; - } else { - return attr.getValue(); - } + if (!child1.equals(child2)) { + return false; + } } + return true; + } + +// /** +// * Returns true if the element equals another element. +// * +// * @param rawElement the element to compare to +// */ +// public boolean equals(Object rawElement) { +// if (!(rawElement instanceof XMLElement)) { +// return false; +// } +// try { +// return this.equalsXMLElement((XMLElement) rawElement); +// } catch (ClassCastException e) { +// return false; +// } +// } - public String getStringAttribute(String name) { - return getAttribute(name); +// /** +// * Returns true if the element equals another element. +// * +// * @param rawElement the element to compare to +// */ +// public boolean equalsXMLElement(XMLElement rawElement) { +// if (! this.name.equals(rawElement.getLocalName())) { +// return false; +// } +// if (this.attributes.size() != rawElement.getAttributeCount()) { +// return false; +// } +// Enumeration en = this.attributes.elements(); +// while (en.hasMoreElements()) { +// XMLAttribute attr = (XMLAttribute) en.nextElement(); +//// if (! rawElement.hasAttribute(attr.getName(), attr.getNamespace())) { +// if (!rawElement.hasAttribute(attr.getFullName())) { +// return false; +// } +//// String value = rawElement.getAttribute(attr.getName(), +//// attr.getNamespace(), +//// null); +// String value = rawElement.getAttribute(attr.getFullName(), null); +// if (! attr.getValue().equals(value)) { +// return false; +// } +//// String type = +//// rawElement.getAttributeType(attr.getName(), attr.getNamespace()); +//// if (!attr.getType().equals(type)) { +//// return false; +//// } +// } +// if (this.children.size() != rawElement.getChildCount()) { +// return false; +// } +// for (int i = 0; i < this.children.size(); i++) { +// XMLElement child1 = this.getChildAtIndex(i); +// XMLElement child2 = rawElement.getChildAtIndex(i); +// +// if (! child1.equalsXMLElement(child2)) { +// return false; +// } +// } +// return true; +// } + + + public String toString() { + return toString(true); + } + + + public String toString(boolean pretty) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + OutputStreamWriter osw = new OutputStreamWriter(baos); + XMLWriter writer = new XMLWriter(osw); + try { + writer.write(this, pretty); + } catch (IOException e) { + e.printStackTrace(); } + return baos.toString(); + } + - - public String getStringAttribute(String name, String defaultValue) { - return getAttribute(name, defaultValue); + private PApplet findSketch() { + if (sketch != null) { + return sketch; } - - - public String getStringAttribute(String name, - String namespace, - String defaultValue) { - return getAttribute(name, namespace, defaultValue); + if (parent != null) { + return parent.findSketch(); } + return null; + } - - public int getIntAttribute(String name) { - return getIntAttribute(name, 0); + + public boolean save(String filename) { + if (sketch == null) { + sketch = findSketch(); } - - - /** - * Returns the value of an attribute. - * - * @param name the non-null full name of the attribute. - * @param defaultValue the default value of the attribute. - * - * @return the value, or defaultValue if the attribute does not exist. - */ - public int getIntAttribute(String name, - int defaultValue) { - String value = this.getAttribute(name, Integer.toString(defaultValue)); - return Integer.parseInt(value); + if (sketch == null) { + System.err.println("save() can only be used on elements loaded by a sketch"); + throw new RuntimeException("no sketch found, use write(PrintWriter) instead."); } + return write(sketch.createWriter(filename)); + } - /** - * Returns the value of an attribute. - * - * @param name the non-null name of the attribute. - * @param namespace the namespace URI, which may be null. - * @param defaultValue the default value of the attribute. - * - * @return the value, or defaultValue if the attribute does not exist. - */ - public int getIntAttribute(String name, - String namespace, - int defaultValue) { - String value = this.getAttribute(name, namespace, - Integer.toString(defaultValue)); - return Integer.parseInt(value); - } - - - public float getFloatAttribute(String name) { - return getFloatAttribute(name, 0); - } - - - /** - * Returns the value of an attribute. - * - * @param name the non-null full name of the attribute. - * @param defaultValue the default value of the attribute. - * - * @return the value, or defaultValue if the attribute does not exist. - */ - public float getFloatAttribute(String name, - float defaultValue) { - String value = this.getAttribute(name, Float.toString(defaultValue)); - return Float.parseFloat(value); - } - - - /** - * Returns the value of an attribute. - * - * @param name the non-null name of the attribute. - * @param namespace the namespace URI, which may be null. - * @param defaultValue the default value of the attribute. - * - * @return the value, or defaultValue if the attribute does not exist. - */ - public float getFloatAttribute(String name, - String namespace, - float defaultValue) { - String value = this.getAttribute(name, namespace, - Float.toString(defaultValue)); - return Float.parseFloat(value); - } - - - public double getDoubleAttribute(String name) { - return getDoubleAttribute(name, 0); - } - - - /** - * Returns the value of an attribute. - * - * @param name the non-null full name of the attribute. - * @param defaultValue the default value of the attribute. - * - * @return the value, or defaultValue if the attribute does not exist. - */ - public double getDoubleAttribute(String name, - double defaultValue) { - String value = this.getAttribute(name, Double.toString(defaultValue)); - return Double.parseDouble(value); - } - - - /** - * Returns the value of an attribute. - * - * @param name the non-null name of the attribute. - * @param namespace the namespace URI, which may be null. - * @param defaultValue the default value of the attribute. - * - * @return the value, or defaultValue if the attribute does not exist. - */ - public double getDoubleAttribute(String name, - String namespace, - double defaultValue) { - String value = this.getAttribute(name, namespace, - Double.toString(defaultValue)); - return Double.parseDouble(value); - } - - - /** - * Returns the type of an attribute. - * - * @param name the non-null full name of the attribute. - * - * @return the type, or null if the attribute does not exist. - */ - public String getAttributeType(String name) { - XMLAttribute attr = this.findAttribute(name); - if (attr == null) { - return null; - } else { - return attr.getType(); - } - } - - - /** - * Returns the namespace of an attribute. - * - * @param name the non-null full name of the attribute. - * - * @return the namespace, or null if there is none associated. - */ - public String getAttributeNamespace(String name) { - XMLAttribute attr = this.findAttribute(name); - if (attr == null) { - return null; - } else { - return attr.getNamespace(); - } - } - - - /** - * Returns the type of an attribute. - * - * @param name the non-null name of the attribute. - * @param namespace the namespace URI, which may be null. - * - * @return the type, or null if the attribute does not exist. - */ - public String getAttributeType(String name, - String namespace) { - XMLAttribute attr = this.findAttribute(name, namespace); - if (attr == null) { - return null; - } else { - return attr.getType(); - } - } - - - /** - * Sets an attribute. - * - * @param name the non-null full name of the attribute. - * @param value the non-null value of the attribute. - */ - public void setAttribute(String name, - String value) { - XMLAttribute attr = this.findAttribute(name); - if (attr == null) { - attr = new XMLAttribute(name, name, null, value, "CDATA"); - this.attributes.addElement(attr); - } else { - attr.setValue(value); - } - } - - - /** - * Sets an attribute. - * - * @param fullName the non-null full name of the attribute. - * @param namespace the namespace URI of the attribute, which may be null. - * @param value the non-null value of the attribute. - */ - public void setAttribute(String fullName, - String namespace, - String value) { - int index = fullName.indexOf(':'); - String vorname = fullName.substring(index + 1); - XMLAttribute attr = this.findAttribute(vorname, namespace); - if (attr == null) { - attr = new XMLAttribute(fullName, vorname, namespace, value, "CDATA"); - this.attributes.addElement(attr); - } else { - attr.setValue(value); - } - } - - - /** - * Removes an attribute. - * - * @param name the non-null name of the attribute. - */ - public void removeAttribute(String name) { - for (int i = 0; i < this.attributes.size(); i++) { - XMLAttribute attr = (XMLAttribute) this.attributes.elementAt(i); - if (attr.getFullName().equals(name)) { - this.attributes.removeElementAt(i); - return; - } - } - } - - - /** - * Removes an attribute. - * - * @param name the non-null name of the attribute. - * @param namespace the namespace URI of the attribute, which may be null. - */ - public void removeAttribute(String name, - String namespace) { - for (int i = 0; i < this.attributes.size(); i++) { - XMLAttribute attr = (XMLAttribute) this.attributes.elementAt(i); - boolean found = attr.getName().equals(name); - if (namespace == null) { - found &= (attr.getNamespace() == null); - } else { - found &= attr.getNamespace().equals(namespace); - } - - if (found) { - this.attributes.removeElementAt(i); - return; - } - } - } - - - /** - * Returns an enumeration of all attribute names. - * - * @return the non-null enumeration. - */ - public Enumeration enumerateAttributeNames() { - Vector result = new Vector(); - Enumeration en = this.attributes.elements(); - while (en.hasMoreElements()) { - XMLAttribute attr = (XMLAttribute) en.nextElement(); - result.addElement(attr.getFullName()); - } - return result.elements(); - } - - - /** - * Returns whether an attribute exists. - * - * @return true if the attribute exists. - */ - public boolean hasAttribute(String name) { - return this.findAttribute(name) != null; - } - - - /** - * Returns whether an attribute exists. - * - * @return true if the attribute exists. - */ - public boolean hasAttribute(String name, - String namespace) { - return this.findAttribute(name, namespace) != null; - } - - - /** - * Returns all attributes as a Properties object. - * - * @return the non-null set. - */ - public Properties getAttributes() { - Properties result = new Properties(); - Enumeration en = this.attributes.elements(); - while (en.hasMoreElements()) { - XMLAttribute attr = (XMLAttribute) en.nextElement(); - result.put(attr.getFullName(), attr.getValue()); - } - return result; - } - - - /** - * Returns all attributes in a specific namespace as a Properties object. - * - * @param namespace the namespace URI of the attributes, which may be null. - * - * @return the non-null set. - */ - public Properties getAttributesInNamespace(String namespace) { - Properties result = new Properties(); - Enumeration en = this.attributes.elements(); - while (en.hasMoreElements()) { - XMLAttribute attr = (XMLAttribute) en.nextElement(); - if (namespace == null) { - if (attr.getNamespace() == null) { - result.put(attr.getName(), attr.getValue()); - } - } else { - if (namespace.equals(attr.getNamespace())) { - result.put(attr.getName(), attr.getValue()); - } - } - } - return result; - } - - - /** - * Returns the system ID of the data where the element started. - * - * @return the system ID, or null if unknown. - * - * @see #getLineNr - */ - public String getSystemID() { - return this.systemID; - } - - - /** - * Returns the line number in the data where the element started. - * - * @return the line number, or NO_LINE if unknown. - * - * @see #NO_LINE - * @see #getSystemID - */ - public int getLineNr() { - return this.lineNr; - } - - - /** - * Return the #PCDATA content of the element. If the element has a - * combination of #PCDATA content and child elements, the #PCDATA - * sections can be retrieved as unnamed child objects. In this case, - * this method returns null. - * - * @return the content. - */ - public String getContent() { - return this.content; - } - - - /** - * Sets the #PCDATA content. It is an error to call this method with a - * non-null value if there are child objects. - * - * @param content the (possibly null) content. - */ - public void setContent(String content) { - this.content = content; - } - - - /** - * Returns true if the element equals another element. - * - * @param rawElement the element to compare to - */ - public boolean equals(Object rawElement) { - try { - return this.equalsXMLElement((XMLElement) rawElement); - } catch (ClassCastException e) { - return false; - } - } - - - /** - * Returns true if the element equals another element. - * - * @param rawElement the element to compare to - */ - public boolean equalsXMLElement(XMLElement rawElement) { - if (! this.name.equals(rawElement.getLocalName())) { - return false; - } - if (this.attributes.size() != rawElement.getAttributeCount()) { - return false; - } - Enumeration en = this.attributes.elements(); - while (en.hasMoreElements()) { - XMLAttribute attr = (XMLAttribute) en.nextElement(); - if (! rawElement.hasAttribute(attr.getName(), attr.getNamespace())) { - return false; - } - String value = rawElement.getAttribute(attr.getName(), - attr.getNamespace(), - null); - if (! attr.getValue().equals(value)) { - return false; - } - String type = rawElement.getAttributeType(attr.getName(), - attr.getNamespace()); - if (! attr.getType().equals(type)) { - return false; - } - } - if (this.children.size() != rawElement.getChildCount()) { - return false; - } - for (int i = 0; i < this.children.size(); i++) { - XMLElement child1 = this.getChildAtIndex(i); - XMLElement child2 = rawElement.getChildAtIndex(i); - - if (! child1.equalsXMLElement(child2)) { - return false; - } - } - return true; - } - - - public String toString() { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - OutputStreamWriter osw = new OutputStreamWriter(baos); - XMLWriter writer = new XMLWriter(osw); - try { - writer.write(this, true, 2, true); - } catch (IOException e) { - e.printStackTrace(); - } - return baos.toString(); + public boolean write(PrintWriter writer) { + writer.println(XMLWriter.HEADER); + XMLWriter xmlw = new XMLWriter(writer); + try { + xmlw.write(this, true); + writer.flush(); + return true; + + } catch (IOException e) { + e.printStackTrace(); + return false; } + } } diff --git a/android/core/src/processing/xml/XMLWriter.java b/android/core/src/processing/xml/XMLWriter.java index 7925cf8e3..7f865ee2a 100644 --- a/android/core/src/processing/xml/XMLWriter.java +++ b/android/core/src/processing/xml/XMLWriter.java @@ -33,8 +33,7 @@ import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; -import java.util.Enumeration; -import java.util.Vector; +//import java.util.Enumeration; /** @@ -42,8 +41,9 @@ import java.util.Vector; * * @author Marc De Scheemaecker */ -public class XMLWriter -{ +public class XMLWriter { + static final int INDENT = 2; + static final String HEADER = ""; /** * Where to write the output to. @@ -96,7 +96,7 @@ public class XMLWriter public void write(XMLElement xml) throws IOException { - this.write(xml, false, 0, true); + this.write(xml, false, 0, INDENT, true); } @@ -107,11 +107,8 @@ public class XMLWriter * @param prettyPrint if spaces need to be inserted to make the output more * readable */ - public void write(XMLElement xml, - boolean prettyPrint) - throws IOException - { - this.write(xml, prettyPrint, 0, true); + public void write(XMLElement xml, boolean prettyPrint) throws IOException { + this.write(xml, prettyPrint, 0, INDENT, true); } @@ -123,12 +120,9 @@ public class XMLWriter * readable * @param indent how many spaces to indent the element. */ - public void write(XMLElement xml, - boolean prettyPrint, - int indent) - throws IOException - { - this.write(xml, prettyPrint, indent, true); + public void write(XMLElement xml, boolean prettyPrint, int initialIndent) + throws IOException { + this.write(xml, prettyPrint, initialIndent, INDENT, true); } @@ -138,16 +132,15 @@ public class XMLWriter * @param xml the non-null XML element to write. * @param prettyPrint if spaces need to be inserted to make the output more * readable - * @param indent how many spaces to indent the element. + * @param initialIndent how many spaces to indent the first element. */ public void write(XMLElement xml, - boolean prettyPrint, - int indent, - boolean collapseEmptyElements) - throws IOException - { + boolean prettyPrint, + int initialIndent, + int eachIndent, + boolean collapseEmptyElements) throws IOException { if (prettyPrint) { - for (int i = 0; i < indent; i++) { + for (int i = 0; i < initialIndent; i++) { this.writer.print(' '); } } @@ -164,45 +157,50 @@ public class XMLWriter } else { this.writer.print('<'); this.writer.print(xml.getName()); - Vector nsprefixes = new Vector(); +// Vector nsprefixes = new Vector(); - if (xml.getNamespace() != null) { - if (xml.getLocalName().equals(xml.getName())) { - this.writer.print(" xmlns=\"" + xml.getNamespace() + '"'); - } else { - String prefix = xml.getName(); - prefix = prefix.substring(0, prefix.indexOf(':')); - nsprefixes.addElement(prefix); - this.writer.print(" xmlns:" + prefix); - this.writer.print("=\"" + xml.getNamespace() + "\""); - } - } + // namespace was spewing all sorts of garbage into the xml doc. + // disabling this and looking for a better solution +// if (xml.getNamespace() != null) { +//// System.out.println("namespace is " + xml.getNamespace()); +//// System.out.println(" names are " + xml.getLocalName() + " " + xml.getName()); +// if (xml.getLocalName().equals(xml.getName())) { +// this.writer.print(" xmlns=\"" + xml.getNamespace() + '"'); +// } else { +// String prefix = xml.getName(); +// prefix = prefix.substring(0, prefix.indexOf(':')); +// nsprefixes.addElement(prefix); +// this.writer.print(" xmlns:" + prefix); +// this.writer.print("=\"" + xml.getNamespace() + "\""); +// } +// } - Enumeration en = xml.enumerateAttributeNames(); +// Enumeration en = xml.enumerateAttributeNames(); - while (en.hasMoreElements()) { - String key = (String) en.nextElement(); - int index = key.indexOf(':'); +// while (en.hasMoreElements()) { +// String key = (String) en.nextElement(); +// int index = key.indexOf(':'); +// +// if (index >= 0) { +// String namespace = xml.getAttributeNamespace(key); +// +// if (namespace != null) { +// String prefix = key.substring(0, index); +// +// if (!nsprefixes.contains(prefix)) { +// this.writer.print(" xmlns:" + prefix); +// this.writer.print("=\"" + namespace + '"'); +// nsprefixes.addElement(prefix); +// } +// } +// } +// } - if (index >= 0) { - String namespace = xml.getAttributeNamespace(key); +// en = xml.enumerateAttributeNames(); - if (namespace != null) { - String prefix = key.substring(0, index); - - if (! nsprefixes.contains(prefix)) { - this.writer.print(" xmlns:" + prefix); - this.writer.print("=\"" + namespace + '"'); - nsprefixes.addElement(prefix); - } - } - } - } - - en = xml.enumerateAttributeNames(); - - while (en.hasMoreElements()) { - String key = (String) en.nextElement(); +// while (en.hasMoreElements()) { +// String key = (String) en.nextElement(); + for (String key : xml.listAttributes()) { String value = xml.getAttribute(key, null); this.writer.print(" " + key + "=\""); this.writeEncoded(value); @@ -225,16 +223,16 @@ public class XMLWriter writer.println(); } - en = xml.enumerateChildren(); - - while (en.hasMoreElements()) { - XMLElement child = (XMLElement) en.nextElement(); - this.write(child, prettyPrint, indent + 4, - collapseEmptyElements); + int count = xml.getChildCount(); + for (int i = 0; i < count; i++) { + XMLElement child = xml.getChild(i); + this.write(child, prettyPrint, + initialIndent + eachIndent, eachIndent, + collapseEmptyElements); } if (prettyPrint) { - for (int i = 0; i < indent; i++) { + for (int i = 0; i < initialIndent; i++) { this.writer.print(' '); } } @@ -303,5 +301,4 @@ public class XMLWriter } } } - } diff --git a/android/todo.txt b/android/todo.txt index e54df3ec6..336f014d9 100644 --- a/android/todo.txt +++ b/android/todo.txt @@ -4,7 +4,7 @@ X right now would cause NumberFormatException X add notes to the wiki about the size() method X make sure sketchRenderer()/sketchWidth()/sketchHeight() are working on desktop o see about getting them documented in the reference -_ do a writeup of the size() method in the wiki +X do a writeup of the size() method in the wiki X size() command is currently ignored in Android X http://dev.processing.org/bugs/show_bug.cgi?id=1397 X http://code.google.com/p/processing/issues/detail?id=211 @@ -29,11 +29,7 @@ X http://code.google.com/p/processing/issues/detail?id=221 X change skewX/Y to shearX/Y _ need updated reference for this -not included -KEYCODE_VOLUME_DOWN -KEYCODE_VOLUME_UP -KEYCODE_CAMERA -KEYCODE_HOME +_ throw an error if a file in the 'data' dir ends with .gz create new keystore location: [ ] (browse) @@ -271,8 +267,6 @@ _ probably same as memory error above // jdf maybedone _ if hitting 'run' in p5, need to kill any sketch that's currently running -_ throw an error if a file in the 'data' dir ends with .gz - _ need to make data folder copy more efficient than just copying everything _ right now, first copies to src inside Build.java (which then copies to bin)