[XOM-interest] Small bug in Serializer relating to indentation and preserving whitespace

Asgeir Frimannsson asgeirf at gmail.com
Sat Oct 20 19:07:36 EDT 2007


On 10/21/07, Elliotte Harold <elharo at metalab.unc.edu> wrote:
>
> > Following up on this, here's an additional unit test that catches this
> > bug...
>
> That's quite helpful. I do prefer bug reports in the form of unit tests.
> Unfortunately the mailing list strips attachments. Could you just paste
> the relevant text into an e-mail and send it again? Thanks.
>

Here it is:

Index: SerializerTest.java
===================================================================
RCS file: /cvs/xom/src/nu/xom/tests/SerializerTest.java,v
retrieving revision 1.74
diff -u -r1.74 SerializerTest.java
--- SerializerTest.java    9 Feb 2006 15:12:26 -0000    1.74
+++ SerializerTest.java    20 Oct 2007 06:10:03 -0000
@@ -455,6 +455,29 @@

     }

+
+    public void testXMLSpacePreserveWithIndentingAndWhitespaceTextNode()
+      throws IOException {
+
+        root.addAttribute(
+          new Attribute(
+            "xml:space",
+            "http://www.w3.org/XML/1998/namespace",
+            "preserve"));
+        root.appendChild(new Element("sameline"));
+        root.appendChild(new Text("  "));
+        root.appendChild(new Element("sameline2"));
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        Serializer serializer = new Serializer(out, "UTF-8");
+        serializer.setIndent(4);
+        serializer.write(doc);
+        String result = out.toString("UTF-8");
+        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
+          + "<root xml:space=\"preserve\"><sameline/>
<sameline2/></root>\r\n",
+           result);
+
+    }
+

     public void testXMLSpaceUnspecifiedValueWithIndenting()
       throws IOException {


And suggested patch:

Index: Serializer.java
===================================================================
RCS file: /cvs/xom/src/nu/xom/Serializer.java,v
retrieving revision 1.82
diff -u -r1.82 Serializer.java
--- Serializer.java    22 Feb 2006 10:49:21 -0000    1.82
+++ Serializer.java    20 Oct 2007 05:53:01 -0000
@@ -792,7 +792,7 @@
             escaper.writeUncheckedMarkup("]]>");
         }
         // is this boundary whitespace we can ignore?
-        else if (isBoundaryWhitespace(text, value)) {
+        else if (isBoundaryWhitespace(text, value) &&
!escaper.isPreserveSpace()) {
             return; // without writing node
         }
         else {



cheers,
asgeir


More information about the XOM-interest mailing list