Skip to content

Commit 66cdb6f

Browse files
committed
fix for XML problems in issue processing#1796
1 parent 9425a31 commit 66cdb6f

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

core/src/processing/data/XML.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,13 @@ public String format(int indent) {
10081008
return singleLine;
10091009
}
10101010

1011+
// Might just be whitespace, which won't be valid XML. And in that case,
1012+
// even adding the decl to the top seems dumb, so don't spew out a decl.
1013+
// https://github.com/processing/processing/issues/1796
1014+
if (singleLine.trim().length() == 0) {
1015+
return "";
1016+
}
1017+
10111018
// Since the indent is not -1, bring back the XML declaration
10121019
//transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
10131020

@@ -1016,8 +1023,13 @@ public String format(int indent) {
10161023
// DOMSource source = new DOMSource(node);
10171024
Source source = new StreamSource(new StringReader(singleLine));
10181025
transformer.transform(source, xmlOutput);
1019-
return decl + sep + stringWriter.toString();
1020-
// return xmlOutput.getWriter().toString();
1026+
String outgoing = stringWriter.toString();
1027+
if (!outgoing.startsWith(decl)) {
1028+
// Add the XML declaration to the top if it's not there already
1029+
return decl + sep + outgoing;
1030+
} else {
1031+
return outgoing;
1032+
}
10211033

10221034
} catch (Exception e) {
10231035
e.printStackTrace();

0 commit comments

Comments
 (0)