[jboss-svn-commits] JBoss Common SVN: r2891 - in common-core/trunk/src: test/java/org/jboss/test/util/test/xml and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Jul 31 12:03:02 EDT 2008


Author: thomas.diesler at jboss.com
Date: 2008-07-31 12:03:02 -0400 (Thu, 31 Jul 2008)
New Revision: 2891

Modified:
   common-core/trunk/src/main/java/org/jboss/util/xml/DOMWriter.java
   common-core/trunk/src/test/java/org/jboss/test/util/test/xml/DOMWriterTestCase.java
Log:
Add DomWriter.ignoreWhitespace

Modified: common-core/trunk/src/main/java/org/jboss/util/xml/DOMWriter.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/xml/DOMWriter.java	2008-07-31 14:57:42 UTC (rev 2890)
+++ common-core/trunk/src/main/java/org/jboss/util/xml/DOMWriter.java	2008-07-31 16:03:02 UTC (rev 2891)
@@ -90,6 +90,8 @@
    private boolean prettyprint;
    // True, if the XML declaration should be written
    private boolean writeXMLDeclaration;
+   // True, if whitespace should be ignored
+   private boolean ignoreWhitespace;
    // Explicit character set encoding
    private String charsetName;
    // indent for the pretty printer
@@ -168,7 +170,22 @@
       return this;
    }
 
+   public boolean isIgnoreWhitespace()
+   {
+      return ignoreWhitespace;
+   }
+
    /**
+    * Set whether whitespace should be ignored.
+    * The default is false.
+    */
+   public DOMWriter setIgnoreWhitespace(boolean ignoreWhitespace)
+   {
+      this.ignoreWhitespace = ignoreWhitespace;
+      return this;
+   }
+   
+   /**
     * Set wheter subelements should have their namespaces completed.
     * Setting this to false may lead to invalid XML fragments.
     * The default is true.
@@ -211,6 +228,9 @@
 
    public void print(Node node)
    {
+      if (prettyprint && ignoreWhitespace)
+         throw new IllegalStateException("Cannot pretty print and ignore whitespace");
+      
       rootNode = node;
       printInternal(node, false);
    }
@@ -412,8 +432,14 @@
          case Node.TEXT_NODE:
          {
             String text = normalize(node.getNodeValue(), canonical);
-            if (prettyprint == false || text.trim().length() > 0)
+            if (text.trim().length() > 0)
+            {
                out.print(text);
+            }
+            else if (prettyprint == false && ignoreWhitespace == false)
+            {
+               out.print(text);
+            }
             break;
          }
 
@@ -587,10 +613,6 @@
                break;
             }
             case '\r':
-            {
-               str.append("
");
-               break;
-            }
             case '\n':
             {
                if (canonical)

Modified: common-core/trunk/src/test/java/org/jboss/test/util/test/xml/DOMWriterTestCase.java
===================================================================
--- common-core/trunk/src/test/java/org/jboss/test/util/test/xml/DOMWriterTestCase.java	2008-07-31 14:57:42 UTC (rev 2890)
+++ common-core/trunk/src/test/java/org/jboss/test/util/test/xml/DOMWriterTestCase.java	2008-07-31 16:03:02 UTC (rev 2891)
@@ -260,4 +260,16 @@
       String xmlOut = DOMWriter.printNode(root, false);
       assertEquals(xmlIn, xmlOut);
    }
+ 
+   public void testWhiteSpaceRemove() throws Exception
+   {
+      String xmlIn = "<Hello> <Sub>World</Sub> </Hello>";
+      Element root = DOMUtils.parse(xmlIn);
+      root.normalize();
+      
+      StringWriter strwr = new StringWriter();
+      new DOMWriter(strwr).setIgnoreWhitespace(true).print(root);
+      String xmlOut = strwr.toString();
+      assertEquals("<Hello><Sub>World</Sub></Hello>", xmlOut);
+   }
 }




More information about the jboss-svn-commits mailing list