[jboss-svn-commits] JBoss Common SVN: r1947 - branches/Branch_4_0/src/main/org/jboss/util/xml
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri Aug 11 18:34:15 EDT 2006
Author: thomas.diesler at jboss.com
Date: 2006-08-11 18:34:12 -0400 (Fri, 11 Aug 2006)
New Revision: 1947
Modified:
branches/Branch_4_0/src/main/org/jboss/util/xml/DOMWriter.java
Log:
ensure valid xml fragments with comlete ns declarations
when writing non root elements
Modified: branches/Branch_4_0/src/main/org/jboss/util/xml/DOMWriter.java
===================================================================
--- branches/Branch_4_0/src/main/org/jboss/util/xml/DOMWriter.java 2006-08-11 16:45:30 UTC (rev 1946)
+++ branches/Branch_4_0/src/main/org/jboss/util/xml/DOMWriter.java 2006-08-11 22:34:12 UTC (rev 1947)
@@ -65,6 +65,7 @@
import java.io.Writer;
import org.w3c.dom.Attr;
+import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@@ -74,7 +75,6 @@
*
* @author Andy Clark, IBM
* @author Thomas.Diesler at jboss.org
- * @version $Revision$
*/
public class DOMWriter
{
@@ -92,7 +92,9 @@
private int prettyIndent;
// True, if the XML declaration has been written
private boolean wroteXMLDeclaration;
-
+ // The node that started the write
+ private Node rootNode;
+
public DOMWriter(Writer w)
{
this.out = new PrintWriter(w);
@@ -183,6 +185,7 @@
public void print(Node node)
{
+ rootNode = node;
printInternal(node, false);
}
@@ -210,6 +213,7 @@
int type = node.getNodeType();
boolean hasChildNodes = node.getChildNodes().getLength() > 0;
+ String nodeName = node.getNodeName();
switch (type)
{
// print document
@@ -224,9 +228,10 @@
break;
}
- // print element with attributes
+ // print element with attributes
case Node.ELEMENT_NODE:
{
+ Element element = (Element)node;
if (prettyprint)
{
for (int i = 0; i < prettyIndent; i++)
@@ -237,17 +242,30 @@
}
out.print('<');
- out.print(node.getNodeName());
+ out.print(nodeName);
+
+ String prefix = node.getPrefix();
+ String nsURI = (prefix != null ? getNamespaceURI(prefix, element, rootNode) : null);
+
Attr attrs[] = sortAttributes(node.getAttributes());
for (int i = 0; i < attrs.length; i++)
{
Attr attr = attrs[i];
- out.print(' ');
- out.print(attr.getNodeName());
- out.print("='");
- out.print(normalize(attr.getNodeValue()));
- out.print("'");
+ String attrName = attr.getNodeName();
+ String attrValue = normalize(attr.getNodeValue());
+
+ if (prefix != null && attrName.equals("xmlns:" + prefix))
+ nsURI = attrValue;
+
+ out.print(" " + attrName + "='" + attrValue + "'");
}
+
+ // Add missing namespace declaration
+ if (prefix != null && nsURI == null)
+ {
+ nsURI = getNamespaceURI(prefix, element, null);
+ out.print(" xmlns:" + prefix + "='" + nsURI + "'");
+ }
if (hasChildNodes)
{
@@ -290,7 +308,7 @@
else
{
out.print('&');
- out.print(node.getNodeName());
+ out.print(nodeName);
out.print(';');
}
break;
@@ -325,7 +343,7 @@
case Node.PROCESSING_INSTRUCTION_NODE:
{
out.print("<?");
- out.print(node.getNodeName());
+ out.print(nodeName);
String data = node.getNodeValue();
if (data != null && data.length() > 0)
{
@@ -381,7 +399,7 @@
}
out.print("</");
- out.print(node.getNodeName());
+ out.print(nodeName);
out.print('>');
}
@@ -393,6 +411,16 @@
out.flush();
}
+ private String getNamespaceURI(String prefix, Element element, Node stopNode)
+ {
+ Node parent = element.getParentNode();
+ String nsURI = element.getAttribute("xmlns:" + prefix);
+ if (nsURI.length() == 0 && element != stopNode && parent instanceof Element)
+ return getNamespaceURI(prefix, (Element)parent, stopNode);
+
+ return (nsURI.length() > 0 ? nsURI : null);
+ }
+
private boolean isEndMarkerIndented(Node node)
{
if (prettyprint)
More information about the jboss-svn-commits
mailing list