mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
doing battle with XML and format()
This commit is contained in:
@@ -157,8 +157,6 @@ public class XML implements Serializable {
|
||||
|
||||
|
||||
/**
|
||||
* xxxxxxx
|
||||
*
|
||||
* @webref xml:method
|
||||
* @brief Converts String content to an XML object
|
||||
* @param data the content to be parsed as XML
|
||||
@@ -166,7 +164,6 @@ public class XML implements Serializable {
|
||||
* @throws SAXException
|
||||
* @throws ParserConfigurationException
|
||||
* @throws IOException
|
||||
* @see PApplet#loadXML(String)
|
||||
*/
|
||||
static public XML parse(String data) throws IOException, ParserConfigurationException, SAXException {
|
||||
return XML.parse(data, null);
|
||||
@@ -724,7 +721,6 @@ public class XML implements Serializable {
|
||||
*
|
||||
* @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) {
|
||||
@@ -764,24 +760,35 @@ public class XML implements Serializable {
|
||||
|
||||
/**
|
||||
* Format this XML data as a String.
|
||||
*
|
||||
* @webref xml:method
|
||||
* @brief Formats XML data as a String
|
||||
* @param indent -1 for a single line (and no declaration), >= 0 for indents and newlines
|
||||
* @return the content
|
||||
* @see XML#toString()
|
||||
*/
|
||||
public String format(int indent) {
|
||||
try {
|
||||
// entities = doctype.getEntities()
|
||||
boolean useIndentAmount = false;
|
||||
TransformerFactory factory = TransformerFactory.newInstance();
|
||||
if (indent != -1) {
|
||||
factory.setAttribute("indent-number", indent);
|
||||
try {
|
||||
factory.setAttribute("indent-number", indent);
|
||||
} catch (IllegalArgumentException e) {
|
||||
useIndentAmount = true;
|
||||
}
|
||||
}
|
||||
Transformer transformer = factory.newTransformer();
|
||||
|
||||
// Add the XML declaration at the top if this node is the root and we're
|
||||
// not writing to a single line (indent = -1 means single line).
|
||||
if (indent == -1 || parent != null) {
|
||||
if (indent == -1 || parent == null) {
|
||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||
} else {
|
||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
|
||||
}
|
||||
|
||||
// transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "sample.dtd");
|
||||
|
||||
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
|
||||
@@ -793,17 +800,20 @@ public class XML implements Serializable {
|
||||
// transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
|
||||
// "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd");
|
||||
|
||||
// For Android, because (at least 2.3.3) doesn't like indent-number
|
||||
if (useIndentAmount) {
|
||||
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
|
||||
}
|
||||
|
||||
// transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
|
||||
// transformer.setOutputProperty(OutputKeys.ENCODING,"UTF8");
|
||||
transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
|
||||
// transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS
|
||||
// indent by default, but sometimes this needs to be turned off
|
||||
if (indent != 0) {
|
||||
//transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
} else {
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "no");
|
||||
}
|
||||
|
||||
// Always indent, otherwise the XML declaration will just be jammed
|
||||
// onto the first line with the XML code as well.
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
|
||||
// Properties p = transformer.getOutputProperties();
|
||||
// for (Object key : p.keySet()) {
|
||||
// System.out.println(key + " -> " + p.get(key));
|
||||
@@ -818,25 +828,45 @@ public class XML implements Serializable {
|
||||
// please contribute. I've wasted too much of my Sunday on it. But at
|
||||
// least the Giants are getting blown out by the Falcons.
|
||||
|
||||
final String decl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
|
||||
|
||||
// needed for splitting or when adding decl below?
|
||||
//System.getProperty("line.separator")
|
||||
|
||||
StringWriter tempWriter = new StringWriter();
|
||||
StreamResult tempResult = new StreamResult(tempWriter);
|
||||
transformer.transform(new DOMSource(node), tempResult);
|
||||
String[] tempLines = PApplet.split(tempWriter.toString(), '\n');
|
||||
if (tempLines[0].startsWith("<?xml")) {
|
||||
PApplet.println(tempLines);
|
||||
if (tempLines[0].startsWith(decl)) {
|
||||
// Remove XML declaration from the top before slamming into one line
|
||||
tempLines = PApplet.subset(tempLines, 1);
|
||||
int declEnd = tempLines[0].indexOf("?>") + 2;
|
||||
//if (tempLines[0].length() == decl.length()) {
|
||||
if (tempLines[0].length() == declEnd) {
|
||||
// If it's all the XML declaration, remove it
|
||||
PApplet.println("removing first line");
|
||||
tempLines = PApplet.subset(tempLines, 1);
|
||||
} else {
|
||||
PApplet.println("removing part of first line");
|
||||
// If the first node has been moved to this line, be more careful
|
||||
//tempLines[0] = tempLines[0].substring(decl.length());
|
||||
tempLines[0] = tempLines[0].substring(declEnd);
|
||||
}
|
||||
}
|
||||
String singleLine = PApplet.join(PApplet.trim(tempLines), "");
|
||||
if (indent == -1) {
|
||||
return singleLine;
|
||||
}
|
||||
|
||||
// Since the indent is not -1, bring back the XML declaration
|
||||
//transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
|
||||
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
StreamResult xmlOutput = new StreamResult(stringWriter);
|
||||
// DOMSource source = new DOMSource(node);
|
||||
Source source = new StreamSource(new StringReader(singleLine));
|
||||
transformer.transform(source, xmlOutput);
|
||||
return stringWriter.toString();
|
||||
return decl + "\n" + stringWriter.toString();
|
||||
// return xmlOutput.getWriter().toString();
|
||||
|
||||
} catch (Exception e) {
|
||||
@@ -850,6 +880,11 @@ public class XML implements Serializable {
|
||||
* Return the XML document formatted with two spaces for indents.
|
||||
* Chosen to do this since it's the most common case (e.g. with println()).
|
||||
* Same as format(2). Use the format() function for more options.
|
||||
*
|
||||
* @webref xml:method
|
||||
* @brief Gets XML data as a String using default formatting
|
||||
* @return the content
|
||||
* @see XML#format(int)
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
@@ -770,19 +770,25 @@ public class XML implements Serializable {
|
||||
public String format(int indent) {
|
||||
try {
|
||||
// entities = doctype.getEntities()
|
||||
boolean useIndentAmount = false;
|
||||
TransformerFactory factory = TransformerFactory.newInstance();
|
||||
if (indent != -1) {
|
||||
factory.setAttribute("indent-number", indent);
|
||||
try {
|
||||
factory.setAttribute("indent-number", indent);
|
||||
} catch (IllegalArgumentException e) {
|
||||
useIndentAmount = true;
|
||||
}
|
||||
}
|
||||
Transformer transformer = factory.newTransformer();
|
||||
|
||||
// Add the XML declaration at the top if this node is the root and we're
|
||||
// not writing to a single line (indent = -1 means single line).
|
||||
if (indent == -1 || parent != null) {
|
||||
if (indent == -1 || parent == null) {
|
||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||
} else {
|
||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
|
||||
}
|
||||
|
||||
// transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "sample.dtd");
|
||||
|
||||
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
|
||||
@@ -794,17 +800,20 @@ public class XML implements Serializable {
|
||||
// transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
|
||||
// "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd");
|
||||
|
||||
// For Android, because (at least 2.3.3) doesn't like indent-number
|
||||
if (useIndentAmount) {
|
||||
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
|
||||
}
|
||||
|
||||
// transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
|
||||
// transformer.setOutputProperty(OutputKeys.ENCODING,"UTF8");
|
||||
transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
|
||||
// transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS
|
||||
// indent by default, but sometimes this needs to be turned off
|
||||
if (indent != 0) {
|
||||
//transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
} else {
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "no");
|
||||
}
|
||||
|
||||
// Always indent, otherwise the XML declaration will just be jammed
|
||||
// onto the first line with the XML code as well.
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
|
||||
// Properties p = transformer.getOutputProperties();
|
||||
// for (Object key : p.keySet()) {
|
||||
// System.out.println(key + " -> " + p.get(key));
|
||||
@@ -819,25 +828,45 @@ public class XML implements Serializable {
|
||||
// please contribute. I've wasted too much of my Sunday on it. But at
|
||||
// least the Giants are getting blown out by the Falcons.
|
||||
|
||||
final String decl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
|
||||
|
||||
// needed for splitting or when adding decl below?
|
||||
//System.getProperty("line.separator")
|
||||
|
||||
StringWriter tempWriter = new StringWriter();
|
||||
StreamResult tempResult = new StreamResult(tempWriter);
|
||||
transformer.transform(new DOMSource(node), tempResult);
|
||||
String[] tempLines = PApplet.split(tempWriter.toString(), '\n');
|
||||
if (tempLines[0].startsWith("<?xml")) {
|
||||
PApplet.println(tempLines);
|
||||
if (tempLines[0].startsWith(decl)) {
|
||||
// Remove XML declaration from the top before slamming into one line
|
||||
tempLines = PApplet.subset(tempLines, 1);
|
||||
int declEnd = tempLines[0].indexOf("?>") + 2;
|
||||
//if (tempLines[0].length() == decl.length()) {
|
||||
if (tempLines[0].length() == declEnd) {
|
||||
// If it's all the XML declaration, remove it
|
||||
PApplet.println("removing first line");
|
||||
tempLines = PApplet.subset(tempLines, 1);
|
||||
} else {
|
||||
PApplet.println("removing part of first line");
|
||||
// If the first node has been moved to this line, be more careful
|
||||
//tempLines[0] = tempLines[0].substring(decl.length());
|
||||
tempLines[0] = tempLines[0].substring(declEnd);
|
||||
}
|
||||
}
|
||||
String singleLine = PApplet.join(PApplet.trim(tempLines), "");
|
||||
if (indent == -1) {
|
||||
return singleLine;
|
||||
}
|
||||
|
||||
// Since the indent is not -1, bring back the XML declaration
|
||||
//transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
|
||||
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
StreamResult xmlOutput = new StreamResult(stringWriter);
|
||||
// DOMSource source = new DOMSource(node);
|
||||
Source source = new StreamSource(new StringReader(singleLine));
|
||||
transformer.transform(source, xmlOutput);
|
||||
return stringWriter.toString();
|
||||
return decl + "\n" + stringWriter.toString();
|
||||
// return xmlOutput.getWriter().toString();
|
||||
|
||||
} catch (Exception e) {
|
||||
|
||||
+2
-2
@@ -107,8 +107,8 @@ o also shorter than getCount() or getLength()
|
||||
o why not HashMap and ArrayList for JSON?
|
||||
o could enable loadHash() and loadArray() functions
|
||||
X because the types coming back out would always have to be cast
|
||||
_ add loadDict() and loadList() for JSON?
|
||||
_ Dict and List could be interfaces?
|
||||
o add loadDict() and loadList() for JSON?
|
||||
o Dict and List could be interfaces?
|
||||
_ JSONObject.has(key) vs XML.hasAttribute(attr) vs HashMap.containsKey()
|
||||
_ and how it should be handled with hash/dict
|
||||
_ right now using hasKey().. in JSONObject
|
||||
|
||||
@@ -44,6 +44,8 @@ X remove separate launch of QT movie creator
|
||||
|
||||
manindra
|
||||
M bug that was causing the Debugger to point to wrong break point line numbers
|
||||
M 'Debug' button does not re-run the project when it is already running.
|
||||
M http://code.google.com/p/processing/issues/detail?id=1504
|
||||
|
||||
earlier
|
||||
X include debug mode as 'experimental'?
|
||||
@@ -61,11 +63,12 @@ X removed in 2.0b7, because it has bugs and is no longer compatible
|
||||
o also can have case of opening two of same sketch at once
|
||||
o if sketch was open, then restart by dragging the .pde to p5.app
|
||||
|
||||
_ crashes when selecting "Show Sketch Folder" with Windows 7
|
||||
_ http://code.google.com/p/processing/issues/detail?id=1473
|
||||
|
||||
https://processing-js.lighthouseapp.com/
|
||||
|
||||
_ add Iterator as an import?
|
||||
_ for others like collections and Date, show warning and option to add?
|
||||
_ along with warning that it's not supported
|
||||
|
||||
_ remove sketch.properties when moving back to the default?
|
||||
_ or can we not do this, because the next mode needs this?
|
||||
|
||||
@@ -862,6 +865,9 @@ _ avoid some confusion for when describing the libraries folder to users
|
||||
|
||||
DIST / Windows
|
||||
|
||||
_ crash when selecting "Show Sketch Folder" with Windows 7
|
||||
_ might be a java.awt.Desktop problem
|
||||
_ http://code.google.com/p/processing/issues/detail?id=1473
|
||||
_ does launching p5 from inside the .zip folder cause it to quit immediately?
|
||||
_ fix line endings in revisions.txt for windows
|
||||
_ exe instead of bat to make exported apps run in 64-bit
|
||||
|
||||
Reference in New Issue
Block a user