[jboss-svn-commits] JBoss Common SVN: r1933 - tags/JBossCommon-1.0.1/src/main/org/jboss/util/xml

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Aug 10 12:28:19 EDT 2006


Author: thomas.diesler at jboss.com
Date: 2006-08-10 12:28:17 -0400 (Thu, 10 Aug 2006)
New Revision: 1933

Modified:
   tags/JBossCommon-1.0.1/src/main/org/jboss/util/xml/DOMUtils.java
   tags/JBossCommon-1.0.1/src/main/org/jboss/util/xml/DOMWriter.java
Log:
Add DOMUtils.parse(InputSource)
DOMWriter adds additional nsDeclaration when writing a DOM subtree.
DOMWriter should always produce valid XML.



Modified: tags/JBossCommon-1.0.1/src/main/org/jboss/util/xml/DOMUtils.java
===================================================================
--- tags/JBossCommon-1.0.1/src/main/org/jboss/util/xml/DOMUtils.java	2006-08-10 16:19:33 UTC (rev 1932)
+++ tags/JBossCommon-1.0.1/src/main/org/jboss/util/xml/DOMUtils.java	2006-08-10 16:28:17 UTC (rev 1933)
@@ -21,14 +21,8 @@
  */
 package org.jboss.util.xml;
 
-import org.jboss.logging.Logger;
-import org.w3c.dom.*;
-import org.xml.sax.SAXException;
+// $Id$
 
-import javax.xml.namespace.QName;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -37,11 +31,26 @@
 import java.util.Iterator;
 import java.util.Map;
 
+import javax.xml.namespace.QName;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.jboss.logging.Logger;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
 /**
  * DOM2 utilites
  *
  * @author Thomas.Diesler at jboss.org
- * @version $Revision$
  */
 public final class DOMUtils
 {
@@ -111,6 +120,22 @@
         }
     }
 
+    /** Parse the given input source and return the root Element
+     */
+    public static Element parse(InputSource source) throws IOException
+    {
+        try
+        {
+            Document doc = getDocumentBuilder().parse(source);
+            Element root = doc.getDocumentElement();
+            return root;
+        }
+        catch (SAXException e)
+        {
+            throw new IOException(e.toString());
+        }
+    }
+
     /** Create an Element for a given name
      */
     public static Element createElement(String localPart)

Modified: tags/JBossCommon-1.0.1/src/main/org/jboss/util/xml/DOMWriter.java
===================================================================
--- tags/JBossCommon-1.0.1/src/main/org/jboss/util/xml/DOMWriter.java	2006-08-10 16:19:33 UTC (rev 1932)
+++ tags/JBossCommon-1.0.1/src/main/org/jboss/util/xml/DOMWriter.java	2006-08-10 16:28:17 UTC (rev 1933)
@@ -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