Fixed bug that XMLSerializer output wrong processing instructions.

This commit is contained in:
Harald Kuhr 2010-02-07 17:57:38 +01:00
parent 261c6f8038
commit 4b7a17ba30

View File

@ -51,13 +51,13 @@ public class XMLSerializer {
// Main problem: Sun's Java 5 does not have LS 3.0 support // Main problem: Sun's Java 5 does not have LS 3.0 support
// This class has no dependencies, which probably makes it more useful // This class has no dependencies, which probably makes it more useful
// TODO: Don't insert initial and ending line-break for text-nodes
// TODO: Support not inserting line-breaks, to preserve space
// TODO: Support line breaking (at configurable width) // TODO: Support line breaking (at configurable width)
// TODO: Support skipping XML declaration?
// TODO: Support standalone? // TODO: Support standalone?
// TODO: Support more than version 1.0? // TODO: Support more than version 1.0?
// TODO: Consider using IOException to communicate trouble, rather than RTE, // TODO: Consider using IOException to communicate trouble, rather than RTE,
// to be more compatible... // to be more compatible...
// TODO: Support not inserting line-breaks, to preserve space
// TODO: Idea: Create a SerializationContext that stores attributes on // TODO: Idea: Create a SerializationContext that stores attributes on
// serialization, to keep the serialization thread-safe // serialization, to keep the serialization thread-safe
@ -152,7 +152,7 @@ public class XMLSerializer {
writeComment(pOut, pNode, pContext); writeComment(pOut, pNode, pContext);
break; break;
case Node.PROCESSING_INSTRUCTION_NODE: case Node.PROCESSING_INSTRUCTION_NODE:
writeProcessingInstruction(pOut, pNode); writeProcessingInstruction(pOut, (ProcessingInstruction) pNode);
break; break;
case Node.ATTRIBUTE_NODE: case Node.ATTRIBUTE_NODE:
throw new IllegalArgumentException("Malformed input Document: Attribute nodes should only occur inside Element nodes"); throw new IllegalArgumentException("Malformed input Document: Attribute nodes should only occur inside Element nodes");
@ -167,9 +167,16 @@ public class XMLSerializer {
} }
} }
private void writeProcessingInstruction(final PrintWriter pOut, final Node pNode) { private void writeProcessingInstruction(final PrintWriter pOut, final ProcessingInstruction pNode) {
pOut.print("\n<?"); pOut.print("\n<?");
pOut.print(pNode.getNodeValue()); pOut.print(pNode.getTarget());
String value = pNode.getData();
if (value != null) {
pOut.print(" ");
pOut.print(value);
}
pOut.println("?>"); pOut.println("?>");
} }
@ -180,8 +187,11 @@ public class XMLSerializer {
pOut.print(maybeEscapeElementValue(value)); pOut.print(maybeEscapeElementValue(value));
} }
else if (!StringUtil.isEmpty(value)) { else if (!StringUtil.isEmpty(value)) {
indentToLevel(pOut, pContext); String escapedValue = maybeEscapeElementValue(value.trim());
pOut.println(maybeEscapeElementValue(value.trim())); //if (escapedValue.length() + (pContext.level * pContext.indent.length()) > 78) {
indentToLevel(pOut, pContext);
//}
pOut.println(escapedValue);
} }
} }
@ -219,7 +229,7 @@ public class XMLSerializer {
} }
String value = pNode.getNodeValue(); String value = pNode.getNodeValue();
validateCommenValue(value); validateCommentValue(value);
if (value.startsWith(" ")) { if (value.startsWith(" ")) {
pOut.print("<!--"); pOut.print("<!--");
@ -248,7 +258,7 @@ public class XMLSerializer {
int startEscape = needsEscapeElement(pValue); int startEscape = needsEscapeElement(pValue);
if (startEscape < 0) { if (startEscape < 0) {
// If no escpaing is needed, simply return original // If no escaping is needed, simply return original
return pValue; return pValue;
} }
else { else {
@ -374,6 +384,7 @@ public class XMLSerializer {
default: default:
} }
} }
return -1; return -1;
} }
@ -384,7 +395,7 @@ public class XMLSerializer {
return pValue; return pValue;
} }
private static String validateCommenValue(final String pValue) { private static String validateCommentValue(final String pValue) {
if (pValue.indexOf("--") >= 0) { if (pValue.indexOf("--") >= 0) {
throw new IllegalArgumentException("Malformed input document: Comment may not contain the string '--'"); throw new IllegalArgumentException("Malformed input document: Comment may not contain the string '--'");
} }
@ -407,6 +418,9 @@ public class XMLSerializer {
// TODO: Attributes should probably include namespaces, so that it works // TODO: Attributes should probably include namespaces, so that it works
// even if the document was created using attributes instead of namespaces... // even if the document was created using attributes instead of namespaces...
// In that case, prefix will be null...
// TODO: Don't insert duplicate/unnecessary namesspace declarations
// Handle namespace // Handle namespace
String namespace = pNode.getNamespaceURI(); String namespace = pNode.getNamespaceURI();
@ -444,6 +458,7 @@ public class XMLSerializer {
} }
} }
// TODO: Consider not indenting/newline if the first child is a text node
// Iterate children if any // Iterate children if any
if (pNode.hasChildNodes()) { if (pNode.hasChildNodes()) {
pOut.print(">"); pOut.print(">");
@ -452,11 +467,9 @@ public class XMLSerializer {
} }
NodeList children = pNode.getChildNodes(); NodeList children = pNode.getChildNodes();
//pContext.level++;
for (int i = 0; i < children.getLength(); i++) { for (int i = 0; i < children.getLength(); i++) {
writeNodeRecursive(pOut, children.item(i), pContext.push()); writeNodeRecursive(pOut, children.item(i), pContext.push());
} }
//pContext.level--;
if (!pContext.preserveSpace) { if (!pContext.preserveSpace) {
indentToLevel(pOut, pContext); indentToLevel(pOut, pContext);
@ -515,12 +528,15 @@ public class XMLSerializer {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true); factory.setNamespaceAware(true);
DocumentBuilder builder; DocumentBuilder builder;
try { try {
builder = factory.newDocumentBuilder(); builder = factory.newDocumentBuilder();
} }
catch (ParserConfigurationException e) { catch (ParserConfigurationException e) {
//noinspection ThrowableInstanceNeverThrown BOGUS
throw (IOException) new IOException(e.getMessage()).initCause(e); throw (IOException) new IOException(e.getMessage()).initCause(e);
} }
DOMImplementation dom = builder.getDOMImplementation(); DOMImplementation dom = builder.getDOMImplementation();
Document document = dom.createDocument("http://www.twelvemonkeys.com/xml/test", "test", dom.createDocumentType("test", null, null)); Document document = dom.createDocument("http://www.twelvemonkeys.com/xml/test", "test", dom.createDocumentType("test", null, null));