incorporating edits from the desktop version--new xml api

This commit is contained in:
benfry
2010-08-07 20:10:18 +00:00
parent fec4ae5c60
commit d9e37ab6b5
7 changed files with 1452 additions and 1321 deletions

View File

@@ -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);
// }
}

View File

@@ -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);

View File

@@ -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;
}

File diff suppressed because it is too large Load Diff

View File

@@ -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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
/**
* 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<String> nsprefixes = new Vector<String>();
// Vector<String> nsprefixes = new Vector<String>();
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
}
}
}
}