Author: thomas.diesler(a)jboss.com
Date: 2008-07-31 11:51:20 -0400 (Thu, 31 Jul 2008)
New Revision: 7982
Modified:
common/trunk/
common/trunk/src/main/java/org/jboss/wsf/common/DOMWriter.java
common/trunk/src/test/java/org/jboss/test/ws/common/utils/DOMWriterTestCase.java
Log:
Add ignoreWhitespace
Property changes on: common/trunk
___________________________________________________________________
Name: svn:ignore
- ant.properties
version.properties.md5
output*
thirdparty
target
+ ant.properties
version.properties.md5
output*
thirdparty
target
.settings
Modified: common/trunk/src/main/java/org/jboss/wsf/common/DOMWriter.java
===================================================================
--- common/trunk/src/main/java/org/jboss/wsf/common/DOMWriter.java 2008-07-31 15:30:01 UTC
(rev 7981)
+++ common/trunk/src/main/java/org/jboss/wsf/common/DOMWriter.java 2008-07-31 15:51:20 UTC
(rev 7982)
@@ -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/trunk/src/test/java/org/jboss/test/ws/common/utils/DOMWriterTestCase.java
===================================================================
---
common/trunk/src/test/java/org/jboss/test/ws/common/utils/DOMWriterTestCase.java 2008-07-31
15:30:01 UTC (rev 7981)
+++
common/trunk/src/test/java/org/jboss/test/ws/common/utils/DOMWriterTestCase.java 2008-07-31
15:51:20 UTC (rev 7982)
@@ -258,4 +258,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);
+ }
}
Show replies by date