From 7b78f3e71cdde947293f427a0e25c35673ea4126 Mon Sep 17 00:00:00 2001 From: Michal Srb <msrb@redhat.com> Date: Wed, 8 Jan 2014 15:21:46 +0100 Subject: [PATCH] Add ability to disable HTML handling and act as a plain XMLWriter Patch by Alan Harder https://github.com/jenkinsci/dom4j/commit/44f214e7e4d4f741c2f721565605255a2ae8fbfa --- src/java/org/dom4j/io/HTMLWriter.java | 97 ++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 40 deletions(-) diff --git a/src/java/org/dom4j/io/HTMLWriter.java b/src/java/org/dom4j/io/HTMLWriter.java index fec556f..920dc4f 100644 --- a/src/java/org/dom4j/io/HTMLWriter.java +++ b/src/java/org/dom4j/io/HTMLWriter.java @@ -218,6 +218,8 @@ public class HTMLWriter extends XMLWriter { */ private HashSet omitElementCloseSet; + private boolean disabled = false; + public HTMLWriter(Writer writer) { super(writer, DEFAULT_HTML_FORMAT); } @@ -243,10 +245,24 @@ public class HTMLWriter extends XMLWriter { super(out, format); } + public boolean isEnabled() { + return !disabled; + } + + /** + * Enable/disable HTML handling. If set to false, behave as plain XMLWriter + * (useful if Content-Type is set to something other than text/html). + */ + public void setEnabled(boolean enabled) { + disabled = !enabled; + } + public void startCDATA() throws SAXException { + if (disabled) super.startCDATA(); } public void endCDATA() throws SAXException { + if (disabled) super.endCDATA(); } // Overloaded methods @@ -254,7 +270,7 @@ public class HTMLWriter extends XMLWriter { protected void writeCDATA(String text) throws IOException { // XXX: Should we escape entities? // writer.write( escapeElementEntities( text ) ); - if (getOutputFormat().isXHTML()) { + if (disabled || getOutputFormat().isXHTML()) { super.writeCDATA(text); } else { writer.write(text); @@ -264,40 +280,49 @@ public class HTMLWriter extends XMLWriter { } protected void writeEntity(Entity entity) throws IOException { - writer.write(entity.getText()); - lastOutputNodeType = Node.ENTITY_REFERENCE_NODE; + if (disabled) { + super.writeEntity(entity); + } else { + writer.write(entity.getText()); + lastOutputNodeType = Node.ENTITY_REFERENCE_NODE; + } } protected void writeDeclaration() throws IOException { + if (disabled) super.writeDeclaration(); } protected void writeString(String text) throws IOException { - /* - * DOM stores \n at the end of text nodes that are newlines. This is - * significant if we are in a PRE section. However, we only want to - * output the system line.separator, not \n. This is a little brittle, - * but this function appears to be called with these lineseparators as a - * separate TEXT_NODE. If we are in a preformatted section, output the - * right line.separator, otherwise ditch. If the single \n character is - * not the text, then do the super thing to output the text. - * - * Also, we store the last text that was not a \n since it may be used - * by writeElement in this class to line up preformatted tags. - */ - if (text.equals("\n")) { - if (!formatStack.empty()) { - super.writeString(lineSeparator); - } + if (disabled) { + super.writeString(text); + } else { + /* + * DOM stores \n at the end of text nodes that are newlines. This is + * significant if we are in a PRE section. However, we only want to + * output the system line.separator, not \n. This is a little brittle, + * but this function appears to be called with these lineseparators as a + * separate TEXT_NODE. If we are in a preformatted section, output the + * right line.separator, otherwise ditch. If the single \n character is + * not the text, then do the super thing to output the text. + * + * Also, we store the last text that was not a \n since it may be used + * by writeElement in this class to line up preformatted tags. + */ + if (text.equals("\n")) { + if (!formatStack.empty()) { + super.writeString(lineSeparator); + } - return; - } + return; + } - lastText = text; + lastText = text; - if (formatStack.empty()) { - super.writeString(text.trim()); - } else { - super.writeString(text); + if (formatStack.empty()) { + super.writeString(text.trim()); + } else { + super.writeString(text); + } } } @@ -312,16 +337,17 @@ public class HTMLWriter extends XMLWriter { * DOCUMENT ME! */ protected void writeClose(String qualifiedName) throws IOException { - if (!omitElementClose(qualifiedName)) { + if (disabled || !omitElementClose(qualifiedName)) { super.writeClose(qualifiedName); } } protected void writeEmptyElementClose(String qualifiedName) throws IOException { - if (getOutputFormat().isXHTML()) { - // xhtml, always check with format object whether to expand or not. - if (omitElementClose(qualifiedName)) { + if (disabled || !omitElementClose(qualifiedName)) { + super.writeEmptyElementClose(qualifiedName); + } else { + if (getOutputFormat().isXHTML()) { // it was a special omit tag, do it the XHTML way: "<br/>", // ignoring the expansion option, since <br></br> is OK XML, // but produces twice the linefeeds desired in the browser. @@ -329,17 +355,8 @@ public class HTMLWriter extends XMLWriter { // before the close slash. writer.write(" />"); } else { - super.writeEmptyElementClose(qualifiedName); - } - } else { - // html, not xhtml - if (omitElementClose(qualifiedName)) { // it was a special omit tag, do it the old html way: "<br>". writer.write(">"); - } else { - // it was NOT a special omit tag, check with format object - // whether to expand or not. - super.writeEmptyElementClose(qualifiedName); } } } @@ -764,7 +781,7 @@ public class HTMLWriter extends XMLWriter { // Allows us to the current state of the format in this struct on the // formatStack. - private class FormatState { + private static class FormatState { private boolean newlines = false; private boolean trimText = false; -- 1.8.3.1