[jboss-svn-commits] JBoss Common SVN: r2117 - common-core/trunk/src/main/java/org/jboss/util/xml
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Oct 10 12:27:47 EDT 2006
Author: thomas.diesler at jboss.com
Date: 2006-10-10 12:27:45 -0400 (Tue, 10 Oct 2006)
New Revision: 2117
Modified:
common-core/trunk/src/main/java/org/jboss/util/xml/DOMWriter.java
Log:
[JBWS-762] DOMUtils.parse skips peer comments on Document node
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 2006-10-10 16:26:27 UTC (rev 2116)
+++ common-core/trunk/src/main/java/org/jboss/util/xml/DOMWriter.java 2006-10-10 16:27:45 UTC (rev 2117)
@@ -53,9 +53,9 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
-package org.jboss.util.xml;
+package org.jboss.ws.utils;
-// $Id:DOMWriter.java 1085 2006-09-28 08:16:31Z thomas.diesler at jboss.com $
+// $Id$
import java.io.OutputStream;
import java.io.OutputStreamWriter;
@@ -78,7 +78,7 @@
*
* @author Andy Clark, IBM
* @author Thomas.Diesler at jboss.org
- * @version $Revision:1085 $
+ * @version $Revision$
*/
public class DOMWriter
{
@@ -98,12 +98,21 @@
private boolean wroteXMLDeclaration;
// The node that started the write
private Node rootNode;
-
+ // True if we want namespace completion
+ private boolean completeNamespaces = true;
+
public DOMWriter(Writer w)
{
this.out = new PrintWriter(w);
}
+ public DOMWriter(Writer w, String charsetName)
+ {
+ this.out = new PrintWriter(w);
+ this.charsetName = charsetName;
+ this.writeXMLDeclaration = true;
+ }
+
public DOMWriter(OutputStream stream)
{
try
@@ -157,6 +166,17 @@
return this;
}
+ /**
+ * Set wheter subelements should have their namespaces completed.
+ * Setting this to false may lead to invalid XML fragments.
+ * The default is true.
+ */
+ public DOMWriter setCompleteNamespaces(boolean complete)
+ {
+ this.completeNamespaces = complete;
+ return this;
+ }
+
public boolean isPrettyprint()
{
return prettyprint;
@@ -181,9 +201,9 @@
* Set wheter the XML declaration should be written.
* The default is false.
*/
- public DOMWriter setWriteXMLDeclaration(boolean writeXMLDeclaration)
+ public DOMWriter setWriteXMLDeclaration(boolean flag)
{
- this.writeXMLDeclaration = writeXMLDeclaration;
+ this.writeXMLDeclaration = flag;
return this;
}
@@ -210,7 +230,10 @@
if (charsetName != null)
out.print(" encoding='" + charsetName + "'");
- out.println("?>");
+ out.print("?>");
+ if (prettyprint)
+ out.println();
+
wroteXMLDeclaration = true;
}
@@ -232,7 +255,7 @@
break;
}
- // print element with attributes
+ // print element with attributes
case Node.ELEMENT_NODE:
{
Element element = (Element)node;
@@ -247,7 +270,7 @@
out.print('<');
out.print(nodeName);
-
+
Map nsMap = new HashMap();
String elPrefix = node.getPrefix();
if (elPrefix != null)
@@ -255,15 +278,15 @@
String nsURI = getNamespaceURI(elPrefix, element, rootNode);
nsMap.put(elPrefix, nsURI);
}
-
+
Attr attrs[] = sortAttributes(node.getAttributes());
for (int i = 0; i < attrs.length; i++)
{
Attr attr = attrs[i];
String atPrefix = attr.getPrefix();
String atName = attr.getNodeName();
- String atValue = normalize(attr.getNodeValue());
-
+ String atValue = normalize(attr.getNodeValue(), canonical);
+
if (atPrefix != null && (atPrefix.equals("xmlns") || atPrefix.equals("xml")) == false)
{
String nsURI = getNamespaceURI(atPrefix, element, rootNode);
@@ -272,17 +295,20 @@
out.print(" " + atName + "='" + atValue + "'");
}
-
+
// Add missing namespace declaration
- Iterator itPrefix = nsMap.keySet().iterator();
- while (itPrefix.hasNext())
+ if (completeNamespaces)
{
- String prefix = (String)itPrefix.next();
- String nsURI = (String)nsMap.get(prefix);
- if (nsURI == null)
+ Iterator itPrefix = nsMap.keySet().iterator();
+ while (itPrefix.hasNext())
{
- nsURI = getNamespaceURI(prefix, element, null);
- out.print(" xmlns:" + prefix + "='" + nsURI + "'");
+ String prefix = (String)itPrefix.next();
+ String nsURI = (String)nsMap.get(prefix);
+ if (nsURI == null)
+ {
+ nsURI = getNamespaceURI(prefix, element, null);
+ out.print(" xmlns:" + prefix + "='" + nsURI + "'");
+ }
}
}
@@ -338,7 +364,7 @@
{
if (canonical)
{
- out.print(normalize(node.getNodeValue()));
+ out.print(normalize(node.getNodeValue(), canonical));
}
else
{
@@ -352,7 +378,7 @@
// print text
case Node.TEXT_NODE:
{
- String text = normalize(node.getNodeValue());
+ String text = normalize(node.getNodeValue(), canonical);
if (prettyprint == false || text.trim().length() > 0)
out.print(text);
break;
@@ -492,7 +518,7 @@
}
/** Normalizes the given string. */
- private String normalize(String s)
+ public static String normalize(String s, boolean canonical)
{
StringBuffer str = new StringBuffer();
More information about the jboss-svn-commits
mailing list