[dom4j] Add ability to disable HTML handling

Michal Srb msrb at fedoraproject.org
Thu Jan 9 15:16:00 UTC 2014


commit 80fb25286dce1981cedd06fe60c5d865d2d717d0
Author: Michal Srb <msrb at redhat.com>
Date:   Thu Jan 9 13:28:31 2014 +0100

    Add ability to disable HTML handling
    
    This is needed by stapler web framework.
    
    The patch shouldn't change default behavior of the dom4j library and thus it
    shouldn't affect any of the projects currently depending on it.
    New behavior needs to be explicitly enabled first.
    
    The patch was created by Alan Harder.
    https://github.com/jenkinsci/dom4j/commit/44f214e7e4d4f741c2f721565605255a2ae8fbfa
    http://sourceforge.net/p/dom4j/patches/30/

 ....6.1-Add-ability-to-disable-HTML-handling.patch |  186 ++++++++++++++++++++
 dom4j.spec                                         |   10 +-
 2 files changed, 194 insertions(+), 2 deletions(-)
---
diff --git a/dom4j-1.6.1-Add-ability-to-disable-HTML-handling.patch b/dom4j-1.6.1-Add-ability-to-disable-HTML-handling.patch
new file mode 100644
index 0000000..6313a3f
--- /dev/null
+++ b/dom4j-1.6.1-Add-ability-to-disable-HTML-handling.patch
@@ -0,0 +1,186 @@
+From 7b78f3e71cdde947293f427a0e25c35673ea4126 Mon Sep 17 00:00:00 2001
+From: Michal Srb <msrb at 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
+
diff --git a/dom4j.spec b/dom4j.spec
index 9699f25..edb99aa 100644
--- a/dom4j.spec
+++ b/dom4j.spec
@@ -31,7 +31,7 @@
 Summary:        Open Source XML framework for Java
 Name:           dom4j
 Version:        1.6.1
-Release:        19%{?dist}
+Release:        20%{?dist}
 Epoch:          0
 License:        BSD
 URL:            http://www.dom4j.org/
@@ -45,6 +45,8 @@ Patch0:         dom4j-1.6.1-build_xml.patch
 # See https://bugzilla.redhat.com/show_bug.cgi?id=976180
 Patch1:         dom4j-1.6.1-Remove-references-to-ConcurrentReaderHashMap.patch
 Patch2:         dom4j-1.6.1-Port-to-JAXP-1.4.patch
+# Needed by stapler web framework
+Patch3:         dom4j-1.6.1-Add-ability-to-disable-HTML-handling.patch
 BuildRequires:  jpackage-utils >= 0:1.6
 BuildRequires:  ant >= 0:1.6
 #BuildRequires:  junit
@@ -116,6 +118,7 @@ sed -e '/unjar/d' -e 's|,cookbook/\*\*,|,|' build.xml.orig > build.xml
 %patch0 -b .sav
 %patch1 -p1
 %patch2 -p1
+%patch3 -p1
 
 %build
 pushd lib
@@ -184,7 +187,7 @@ install -m 755 run.sh $RPM_BUILD_ROOT%{_datadir}/%{name}-%{version}
 # POM and depmap
 install -d -m 755 $RPM_BUILD_ROOT%{_mavenpomdir}
 install -p -m 644 %{SOURCE2} $RPM_BUILD_ROOT%{_mavenpomdir}/JPP-%{name}.pom
-%add_maven_depmap
+%add_maven_depmap -a "org.jvnet.hudson.dom4j:dom4j"
 
 %files
 %dir %{_docdir}/%{name}
@@ -206,6 +209,9 @@ install -p -m 644 %{SOURCE2} $RPM_BUILD_ROOT%{_mavenpomdir}/JPP-%{name}.pom
 %{_datadir}/%{name}-%{version}
 
 %changelog
+* Fri Dec 06 2013 Michal Srb <msrb at redhat.com> - 0:1.6.1-20
+- Add ability to disable HTML handling
+
 * Wed Oct 16 2013 Michal Srb <msrb at redhat.com> - 0:1.6.1-19
 - Port to JAXP 1.4
 


More information about the scm-commits mailing list