[jboss-svn-commits] JBoss Common SVN: r2116 - common-old/branches/Branch_1_0/src/main/org/jboss/util/xml
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Oct 10 12:26:29 EDT 2006
Author: thomas.diesler at jboss.com
Date: 2006-10-10 12:26:27 -0400 (Tue, 10 Oct 2006)
New Revision: 2116
Modified:
common-old/branches/Branch_1_0/src/main/org/jboss/util/xml/DOMWriter.java
Log:
[JBWS-762] DOMUtils.parse skips peer comments on Document node
Modified: common-old/branches/Branch_1_0/src/main/org/jboss/util/xml/DOMWriter.java
===================================================================
--- common-old/branches/Branch_1_0/src/main/org/jboss/util/xml/DOMWriter.java 2006-10-10 12:16:40 UTC (rev 2115)
+++ common-old/branches/Branch_1_0/src/main/org/jboss/util/xml/DOMWriter.java 2006-10-10 16:26:27 UTC (rev 2116)
@@ -53,7 +53,7 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
-package org.jboss.util.xml;
+package org.jboss.ws.utils;
// $Id$
@@ -78,6 +78,7 @@
*
* @author Andy Clark, IBM
* @author Thomas.Diesler at jboss.org
+ * @version $Revision$
*/
public class DOMWriter
{
@@ -97,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
@@ -156,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;
@@ -180,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;
}
@@ -209,7 +230,10 @@
if (charsetName != null)
out.print(" encoding='" + charsetName + "'");
- out.println("?>");
+ out.print("?>");
+ if (prettyprint)
+ out.println();
+
wroteXMLDeclaration = true;
}
@@ -231,7 +255,7 @@
break;
}
- // print element with attributes
+ // print element with attributes
case Node.ELEMENT_NODE:
{
Element element = (Element)node;
@@ -246,7 +270,7 @@
out.print('<');
out.print(nodeName);
-
+
Map nsMap = new HashMap();
String elPrefix = node.getPrefix();
if (elPrefix != null)
@@ -254,34 +278,37 @@
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);
nsMap.put(atPrefix, nsURI);
}
-
+
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 + "'");
+ }
}
}
@@ -337,7 +364,7 @@
{
if (canonical)
{
- out.print(normalize(node.getNodeValue()));
+ out.print(normalize(node.getNodeValue(), canonical));
}
else
{
@@ -351,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;
@@ -491,7 +518,7 @@
}
/** Normalizes the given string. */
- private String normalize(String s)
+ public static String normalize(String s, boolean canonical)
{
StringBuffer str = new StringBuffer();
@@ -541,4 +568,4 @@
}
return (str.toString());
}
-}
+}
\ No newline at end of file
More information about the jboss-svn-commits
mailing list