mirror of
https://github.com/processing/processing4.git
synced 2026-02-13 02:20:45 +01:00
work on wrapping up XML changes
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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() });
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user