You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by bo...@apache.org on 2005/12/20 05:50:18 UTC

svn commit: r357908 - /ant/core/trunk/src/main/org/apache/tools/ant/util/DOMElementWriter.java

Author: bodewig
Date: Mon Dec 19 20:50:14 2005
New Revision: 357908

URL: http://svn.apache.org/viewcvs?rev=357908&view=rev
Log:
better deal with elements that don't have a namespace URI

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/util/DOMElementWriter.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/util/DOMElementWriter.java
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/main/org/apache/tools/ant/util/DOMElementWriter.java?rev=357908&r1=357907&r2=357908&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/util/DOMElementWriter.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/util/DOMElementWriter.java Mon Dec 19 20:50:14 2005
@@ -33,6 +33,7 @@
 
 /**
  * Writes a DOM tree to a given Writer.
+ * warning: this utility currently does not declare XML Namespaces.
  * <p>Utility class used by {@link org.apache.tools.ant.XmlLogger
  * XmlLogger} and
  * org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter
@@ -278,7 +279,8 @@
         // Write element
         out.write("<");
         if (namespacePolicy.qualifyElements) {
-            String prefix = (String) nsPrefixMap.get(element.getNamespaceURI());
+            String uri = getNamespaceURI(element);
+            String prefix = (String) nsPrefixMap.get(uri);
             if (prefix == null) {
                 if (nsPrefixMap.isEmpty()) {
                     // steal default namespace
@@ -286,8 +288,8 @@
                 } else {
                     prefix = NS + (nextPrefix++);
                 }
-                nsPrefixMap.put(element.getNamespaceURI(), prefix);
-                addNSDefinition(element, element.getNamespaceURI());
+                nsPrefixMap.put(uri, prefix);
+                addNSDefinition(element, uri);
             }
             if (!"".equals(prefix)) {
                 out.write(prefix);
@@ -302,12 +304,12 @@
             Attr attr = (Attr) attrs.item(i);
             out.write(" ");
             if (namespacePolicy.qualifyAttributes) {
-                String prefix =
-                    (String) nsPrefixMap.get(attr.getNamespaceURI());
+                String uri = getNamespaceURI(attr);
+                String prefix = (String) nsPrefixMap.get(uri);
                 if (prefix == null) {
                     prefix = NS + (nextPrefix++);
-                    nsPrefixMap.put(attr.getNamespaceURI(), prefix);
-                    addNSDefinition(element, attr.getNamespaceURI());
+                    nsPrefixMap.put(uri, prefix);
+                    addNSDefinition(element, uri);
                 }
                 out.write(prefix);
                 out.write(":");
@@ -370,10 +372,9 @@
 
         // Write element close
         out.write("</");
-        if (namespacePolicy.qualifyElements
-            || namespacePolicy.qualifyAttributes) {
-            String prefix =
-                (String) nsPrefixMap.get(element.getNamespaceURI());
+        if (namespacePolicy.qualifyElements) {
+            String uri = getNamespaceURI(element);
+            String prefix = (String) nsPrefixMap.get(uri);
             if (prefix != null && !"".equals(prefix)) {
                 out.write(prefix);
                 out.write(":");
@@ -547,5 +548,14 @@
             nsURIByElement.put(element, al);
         }
         al.add(uri);
+    }
+
+    private static String getNamespaceURI(Node n) {
+        String uri = n.getNamespaceURI();
+        if (uri == null) {
+            // FIXME: Is "No Namespace is Empty Namespace" really OK?
+            uri = "";
+        }
+        return uri;
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Re: svn commit: r357908 - /ant/core/trunk/src/main/org/apache/tools/ant/util/DOMElementWriter.java

Posted by Steve Loughran <st...@apache.org>.
Stefan Bodewig wrote:
> On Tue, 20 Dec 2005, <bo...@apache.org> wrote:
> 
> 
>>better deal with elements that don't have a namespace URI
> 
> 
> This is for the case where the task tells DOMElementWriter to qualify
> elements with namespaces but passes in a DOM tree with elements that
> don't have a namespace URI associated with them.
> 
> Is
> 
> 
>>+    private static String getNamespaceURI(Node n) {
>>+        String uri = n.getNamespaceURI();
>>+        if (uri == null) {
>>+            // FIXME: Is "No Namespace is Empty Namespace" really OK?
>>+            uri = "";
>>+        }
>>+        return uri;
>>     }
> 
> 

oh, do not ask me. I never understand DOM and its namespace stuff. I 
know that if you parse something without an ns, you cannot do 
Element.getLocalName(), you need to do something else like 
Element.getName(). Xom is more consistent, "" means local namespace. For 
dom it may be 'null' instead.

Think we will need some tests here to work out wtf is going on.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Re: svn commit: r357908 - /ant/core/trunk/src/main/org/apache/tools/ant/util/DOMElementWriter.java

Posted by Stefan Bodewig <bo...@apache.org>.
On Tue, 20 Dec 2005, <bo...@apache.org> wrote:

> better deal with elements that don't have a namespace URI

This is for the case where the task tells DOMElementWriter to qualify
elements with namespaces but passes in a DOM tree with elements that
don't have a namespace URI associated with them.

Is

> +    private static String getNamespaceURI(Node n) {
> +        String uri = n.getNamespaceURI();
> +        if (uri == null) {
> +            // FIXME: Is "No Namespace is Empty Namespace" really OK?
> +            uri = "";
> +        }
> +        return uri;
>      }

the correct thing to do?

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org