work on wrapping up XML changes

This commit is contained in:
benfry
2012-12-16 19:31:09 +00:00
parent 8c69aeac16
commit c31c40000e
10 changed files with 227 additions and 113 deletions
@@ -23,7 +23,6 @@ package processing.mode.android;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
@@ -201,7 +200,7 @@ public class Manifest {
// load the copy from the build location and start messing with it
XML mf = null;
try {
mf = new XML(new FileReader(file));
mf = new XML(file);
// package name, or default
String p = mf.getString("package").trim();
@@ -241,7 +240,7 @@ public class Manifest {
File manifestFile = getManifestFile();
if (manifestFile.exists()) {
try {
xml = new XML(new FileReader(manifestFile));
xml = new XML(manifestFile);
} catch (Exception e) {
e.printStackTrace();
System.err.println("Problem reading AndroidManifest.xml, creating a new version");
@@ -261,7 +260,7 @@ public class Manifest {
if (xml == null) {
writeBlankManifest(manifestFile);
try {
xml = new XML(new FileReader(manifestFile));
xml = new XML(manifestFile);
} catch (FileNotFoundException e) {
System.err.println("Could not read " + manifestFile.getAbsolutePath());
e.printStackTrace();
+42 -4
View File
@@ -5822,8 +5822,14 @@ public class PApplet extends Applet
* @see PApplet#loadTable(String)
*/
public XML loadXML(String filename) {
return loadXML(filename, null);
}
// version that uses 'options' though there are currently no supported options
public XML loadXML(String filename, String options) {
try {
return new XML(this, filename);
return new XML(createInput(filename), options);
} catch (Exception e) {
e.printStackTrace();
return null;
@@ -5831,6 +5837,31 @@ public class PApplet extends Applet
}
public XML parseXML(String xmlString) {
return parseXML(xmlString, null);
}
public XML parseXML(String xmlString, String options) {
try {
return XML.parse(xmlString, options);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean saveXML(XML xml, String filename) {
return saveXML(xml, filename, null);
}
public boolean saveXML(XML xml, String filename, String options) {
return xml.save(saveFile(filename), options);
}
public Table createTable() {
return new Table();
}
@@ -5869,17 +5900,19 @@ public class PApplet extends Applet
}
public void saveTable(Table table, String filename) {
saveTable(table, filename, null);
public boolean saveTable(Table table, String filename) {
return saveTable(table, filename, null);
}
public void saveTable(Table table, String filename, String options) {
public boolean saveTable(Table table, String filename, String options) {
try {
table.save(saveFile(filename), options);
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
@@ -10847,6 +10880,11 @@ public class PApplet extends Applet
}
public PShape loadShape(String filename, String options) {
return g.loadShape(filename, options);
}
/**
* @webref shape
* @see PShape
+5
View File
@@ -1604,6 +1604,11 @@ public class PGraphics extends PImage implements PConstants {
* @see PApplet#createShape()
*/
public PShape loadShape(String filename) {
return loadShape(filename, null);
}
public PShape loadShape(String filename, String options) {
showMissingWarning("loadShape");
return null;
}
@@ -1380,17 +1380,23 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ {
@Override
public PShape loadShape(String filename) {
return loadShape(filename, null);
}
@Override
public PShape loadShape(String filename, String options) {
String extension = PApplet.getExtension(filename);
PShapeSVG svg = null;
if (extension.equals("svg")) {
svg = new PShapeSVG(parent, filename);
svg = new PShapeSVG(parent.loadXML(filename));
} else if (extension.equals("svgz")) {
try {
InputStream input = new GZIPInputStream(parent.createInput(filename));
XML xml = new XML(PApplet.createReader(input));
XML xml = new XML(input, options);
svg = new PShapeSVG(xml);
} catch (Exception e) {
e.printStackTrace();
+8 -8
View File
@@ -159,14 +159,14 @@ public class PShapeSVG extends PShape {
String fillName; // id of another object
/**
* Initializes a new SVG Object with the given filename.
*/
public PShapeSVG(PApplet parent, String filename) {
// this will grab the root document, starting <svg ...>
// the xml version and initial comments are ignored
this(parent.loadXML(filename));
}
// /**
// * Initializes a new SVG Object with the given filename.
// */
// public PShapeSVG(PApplet parent, String filename) {
// // this will grab the root document, starting <svg ...>
// // the xml version and initial comments are ignored
// this(parent.loadXML(filename));
// }
/**
+24 -4
View File
@@ -119,6 +119,11 @@ public class Table {
}
public Table(InputStream input) throws IOException {
this(input, null);
}
/**
* Read the table from a stream. Possible options include:
* <ul>
@@ -479,7 +484,6 @@ public class Table {
// compiler) of an inner class by the runtime.
/** incomplete, do not use */
// public void parseInto(PApplet sketch, String fieldName) {
public void parseInto(Object enclosingObject, String fieldName) {
Class<?> target = null;
Object outgoing = null;
@@ -1043,14 +1047,23 @@ public class Table {
/**
* Set the titles (and if a second column is present) the data types for
* this table based on a file loaded separately.
* this table based on a file loaded separately. This will look for the
* title in column 0, and the type in column 1. Better yet, specify a
* column named "title" and another named "type" in the dictionary table
* to future-proof the code.
* @param dictionary
*/
public void setColumnTypes(Table dictionary) {
setColumnTitles(dictionary.getStringColumn(0));
int titleCol = 0;
int typeCol = 1;
if (dictionary.hasColumnTitles()) {
titleCol = dictionary.getColumnIndex("title", true);
typeCol = dictionary.getColumnIndex("type", true);
}
setColumnTitles(dictionary.getStringColumn(titleCol));
if (dictionary.getColumnCount() > 1) {
for (int i = 0; i < dictionary.getRowCount(); i++) {
setColumnType(i, dictionary.getString(i, 1));
setColumnType(i, dictionary.getString(i, typeCol));
}
}
}
@@ -1061,7 +1074,9 @@ public class Table {
/**
* Remove the first row from the data set, and use it as the column titles.
* Use loadTable("table.csv", "header") instead.
*/
@Deprecated
public String[] removeTitleRow() {
String[] titles = getStringRow(0);
removeRow(0);
@@ -1089,6 +1104,11 @@ public class Table {
}
public boolean hasColumnTitles() {
return columnTitles != null;
}
public String[] getColumnTitles() {
return columnTitles;
}
+9 -5
View File
@@ -43,9 +43,10 @@ public class TableODS extends Table {
*/
protected TableODS(InputStream input, String worksheet, boolean actual) {
try {
InputStreamReader isr = new InputStreamReader(input, "UTF-8");
BufferedReader reader = new BufferedReader(isr);
read(reader, worksheet, actual);
// InputStreamReader isr = new InputStreamReader(input, "UTF-8");
// BufferedReader reader = new BufferedReader(isr);
// read(reader, worksheet, actual);
read(input, worksheet, actual);
} catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
@@ -59,8 +60,11 @@ public class TableODS extends Table {
}
protected void read(BufferedReader reader, String worksheet, boolean actual) throws IOException, ParserConfigurationException, SAXException {
XML xml = new XML(reader);
// protected void read(BufferedReader reader, String worksheet, boolean actual) throws IOException, ParserConfigurationException, SAXException {
// XML xml = new XML(reader);
protected void read(InputStream input, String worksheet, boolean actual) throws IOException, ParserConfigurationException, SAXException {
XML xml = new XML(input);
// XML x = new XML(reader);
// PApplet.saveStrings(new File("/Users/fry/Desktop/namespacefix.xml"), new String[] { xml.toString() });
// PApplet.saveStrings(new File("/Users/fry/Desktop/newparser.xml"), new String[] { x.toString() });
+107 -78
View File
@@ -49,8 +49,8 @@ public class XML implements Serializable {
/** The internal representation, a DOM node. */
protected Node node;
/** Cached locally because it's used often. */
protected String name;
// /** Cached locally because it's used often. */
// protected String name;
/** The parent element. */
protected XML parent;
@@ -62,26 +62,40 @@ public class XML implements Serializable {
protected XML() { }
/**
* 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.
*
* @throws SAXException
* @throws ParserConfigurationException
* @throws IOException
*/
public XML(PApplet parent, String filename) throws IOException, ParserConfigurationException, SAXException {
this(parent.createReader(filename));
}
// /**
// * 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.
// *
// * @throws SAXException
// * @throws ParserConfigurationException
// * @throws IOException
// */
// public XML(PApplet parent, String filename) throws IOException, ParserConfigurationException, SAXException {
// this(parent.createReader(filename));
// }
public XML(File file) throws IOException, ParserConfigurationException, SAXException {
this(PApplet.createReader(file));
this(file, null);
}
public XML(Reader reader) throws IOException, ParserConfigurationException, SAXException {
public XML(File file, String options) throws IOException, ParserConfigurationException, SAXException {
this(PApplet.createReader(file), options);
}
public XML(InputStream input) throws IOException, ParserConfigurationException, SAXException {
this(input, null);
}
public XML(InputStream input, String options) throws IOException, ParserConfigurationException, SAXException {
this(PApplet.createReader(input), options);
}
protected XML(Reader reader, String options) throws IOException, ParserConfigurationException, SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Prevent 503 errors from www.w3.org
@@ -115,7 +129,8 @@ public class XML implements Serializable {
// Document document = builder.parse(dataPath("1_alt.html"));
Document document = builder.parse(new InputSource(reader));
node = document.getDocumentElement();
name = node.getNodeName();
// name = node.getNodeName();
// NodeList nodeList = document.getDocumentElement().getChildNodes();
// for (int i = 0; i < nodeList.getLength(); i++) {
// }
@@ -124,27 +139,20 @@ public class XML implements Serializable {
// TODO is there a more efficient way of doing this? wow.
// i.e. can we use one static document object for all PNodeXML objects?
public XML(String name) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
node = document.createElement(name);
this.name = name;
this.parent = null;
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
public XML(String name) throws ParserConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
node = document.createElement(name);
// this.name = name;
this.parent = null;
}
protected XML(XML parent, Node node) {
this.node = node;
this.parent = parent;
this.name = node.getNodeName();
// this.name = node.getNodeName();
}
@@ -155,30 +163,33 @@ public class XML implements Serializable {
* @brief Converts String content to an XML object
* @param data the content to be parsed as XML
* @return an XML object, or null
* @throws SAXException
* @throws ParserConfigurationException
* @throws IOException
* @see PApplet#loadXML(String)
*/
static public XML parse(String data) {
try {
return new XML(new StringReader(data));
} catch (Exception e) {
e.printStackTrace();
return null;
}
static public XML parse(String data) throws IOException, ParserConfigurationException, SAXException {
return XML.parse(data, null);
}
public boolean save(OutputStream output) {
static public XML parse(String data, String options) throws IOException, ParserConfigurationException, SAXException {
return new XML(new StringReader(data), null);
}
protected boolean save(OutputStream output) {
return save(PApplet.createWriter(output));
}
public boolean save(File file) {
public boolean save(File file, String options) {
return save(PApplet.createWriter(file));
}
public boolean save(PrintWriter output) {
output.print(format(2));
output.print(toString(2));
output.flush();
return true;
}
@@ -198,7 +209,7 @@ public class XML implements Serializable {
/**
* Internal function; not included in reference.
*/
protected Node getNode() {
protected Object getNative() {
return node;
}
@@ -212,7 +223,8 @@ public class XML implements Serializable {
* @return the name, or null if the element only contains #PCDATA.
*/
public String getName() {
return name;
// return name;
return node.getNodeName();
}
/**
@@ -222,7 +234,7 @@ public class XML implements Serializable {
public void setName(String newName) {
Document document = node.getOwnerDocument();
node = document.renameNode(node, null, newName);
name = node.getNodeName();
// name = node.getNodeName();
}
@@ -450,7 +462,7 @@ public class XML implements Serializable {
public XML addChild(XML child) {
Document document = node.getOwnerDocument();
Node newChild = document.importNode(child.getNode(), true);
Node newChild = document.importNode((Node) child.getNative(), true);
return appendChild(newChild);
}
@@ -476,36 +488,53 @@ public class XML implements Serializable {
}
/** Remove whitespace nodes. */
public void trim() {
//// public static boolean isWhitespace(XML xml) {
//// if (xml.node.getNodeType() != Node.TEXT_NODE)
//// return false;
//// Matcher m = whitespace.matcher(xml.node.getNodeValue());
//// return m.matches();
//// }
// trim(this);
// /** Remove whitespace nodes. */
// public void trim() {
////// public static boolean isWhitespace(XML xml) {
////// if (xml.node.getNodeType() != Node.TEXT_NODE)
////// return false;
////// Matcher m = whitespace.matcher(xml.node.getNodeValue());
////// return m.matches();
////// }
//// trim(this);
//// }
//
// checkChildren();
// int index = 0;
// for (int i = 0; i < children.length; i++) {
// if (i != index) {
// children[index] = children[i];
// }
// Node childNode = (Node) children[i].getNative();
// if (childNode.getNodeType() != Node.TEXT_NODE ||
// children[i].getContent().trim().length() > 0) {
// children[i].trim();
// index++;
// }
// }
// if (index != children.length) {
// children = (XML[]) PApplet.subset(children, 0, index);
// }
//
// // possibility, but would have to re-parse the object
//// helpdesk.objects.com.au/java/how-do-i-remove-whitespace-from-an-xml-document
//// TransformerFactory factory = TransformerFactory.newInstance();
//// Transformer transformer = factory.newTransformer(new StreamSource("strip-space.xsl"));
//// DOMSource source = new DOMSource(document);
//// StreamResult result = new StreamResult(System.out);
//// transformer.transform(source, result);
//
//// <xsl:stylesheet version="1.0"
//// xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
//// <xsl:output method="xml" omit-xml-declaration="yes"/>
//// <xsl:strip-space elements="*"/>
//// <xsl:template match="@*|node()">
//// <xsl:copy>
//// <xsl:apply-templates select="@*|node()"/>
//// </xsl:copy>
//// </xsl:template>
//// </xsl:stylesheet>
// }
//
//
// protected void trim() {
checkChildren();
int index = 0;
for (int i = 0; i < children.length; i++) {
if (i != index) {
children[index] = children[i];
}
Node childNode = children[i].getNode();
if (childNode.getNodeType() != Node.TEXT_NODE ||
children[i].getContent().trim().length() > 0) {
children[i].trim();
index++;
}
}
if (index != children.length) {
children = (XML[]) PApplet.subset(children, 0, index);
}
}
/**
@@ -727,7 +756,7 @@ public class XML implements Serializable {
}
public String format(int indent) {
public String toString(int indent) {
try {
DOMSource dumSource = new DOMSource(node);
// entities = doctype.getEntities()
@@ -777,6 +806,6 @@ public class XML implements Serializable {
@Override
/** Return the XML data as a single line, with no DOCTYPE declaration. */
public String toString() {
return format(-1);
return toString(-1);
}
}
+2 -2
View File
@@ -248,13 +248,13 @@ public class PGraphics2D extends PGraphicsOpenGL {
PShapeSVG svg = null;
if (extension.equals("svg")) {
svg = new PShapeSVG(pg.parent, filename);
svg = new PShapeSVG(pg.parent.loadXML(filename));
} else if (extension.equals("svgz")) {
try {
InputStream input =
new GZIPInputStream(pg.parent.createInput(filename));
XML xml = new XML(PApplet.createReader(input));
XML xml = new XML(input);
svg = new PShapeSVG(xml);
} catch (Exception e) {
e.printStackTrace();
+19 -6
View File
@@ -77,6 +77,9 @@ X saveTable("filename.tsv") or saveTable("filename.txt", "tsv")
X createTable() method in PApplet
X removed getUniqueXxxx() and some others, pending names
_ OutOfMemory in image()
_ http://code.google.com/p/processing/issues/detail?id=1353
_ shader syntax (Andres request)
_ might also need a define inside the shader to control what type it is
@@ -98,6 +101,8 @@ _ dictionary support
_ join tables together (static is kinda gross)
shape
_ major surgery to put loadShape() back into PApplet/PGraphics
_ then have the OpenGL or Java2D versions as cached objects
_ PShape s = createShape(); / s.beginShape(QUADS);
_ createShape() not yet implemented for Java2D
_ http://code.google.com/p/processing/issues/detail?id=1400
@@ -126,14 +131,25 @@ _ loadShape() needs to live in PApplet
_ make PShapeOpenGL a cache object
xml library
X removed 'name' field to avoid possibility of random errors
X confirmed that DOM "correct" version includes the text nodes
X new XML(name) also throws an ex, use createXML() or appendChild("name")
X remove XML.parse() from the reference (it throws an exception)
X use parseXML() instead
o do we need a trim() method for XML?
o use XSL transform to strip whitespace
o helpdesk.objects.com.au/java/how-do-i-remove-whitespace-from-an-xml-document
X messy, just not great
o isWhitespace() method for nodes? (that's the capitalization used in Character)
X doesn't help much
o get back to PhiLho once finished
_ beginning slash in getChild() threw an NPE
_ do we need a trim() method for XML?
_ isWhitespace() method for nodes? (that's the capitalization used in Character)
_ XML toString(0) means no indents or newlines
_ but no way to remove indents and still have newlines...
_ toString(-1)? a new method?
_ format(2), format(4)... format() -> default to 2 params
_ get back to PhiLho once finished
_ look into json and how it would work wrt XML
_ 1) we bring back getFloatAttribute() et al.,
_ and make getFloat() be equivalent to parseFloat(xml.getContent())
@@ -214,9 +230,6 @@ _ http://code.google.com/p/processing/issues/detail?id=1407
_ add "CGAffineTransformInvert: singular matrix" problem to the Wiki
_ http://code.google.com/p/processing/issues/detail?id=1363
_ OutOfMemory in image()
_ http://code.google.com/p/processing/issues/detail?id=1353
api to be fixed/removed
_ remove PImage.delete() and friends from PImage, Movie, etc.
_ delete()/dispose() being used in the movie