You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2007/10/31 20:08:56 UTC

svn commit: r590790 - in /incubator/cxf/trunk/common/common/src: main/java/org/apache/cxf/staxutils/ test/java/org/apache/cxf/staxutils/

Author: dkulp
Date: Wed Oct 31 12:08:56 2007
New Revision: 590790

URL: http://svn.apache.org/viewvc?rev=590790&view=rev
Log:
More fixes for default namespaces in StaxUtils.

Modified:
    incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
    incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/W3CDOMStreamWriter.java
    incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/W3CNamespaceContext.java
    incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java

Modified: incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java?rev=590790&r1=590789&r2=590790&view=diff
==============================================================================
--- incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java (original)
+++ incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java Wed Oct 31 12:08:56 2007
@@ -438,6 +438,9 @@
 
         if (ns == null || ns.length() == 0) {
             writer.writeStartElement(localName);
+            if (StringUtils.isEmpty(decUri)) {
+                declareNamespace = false;
+            }
         } else {
 //            System.out.println("Calling writeStartElement for local name : " 
 //            + localName + " URI: " + ns + " Prefix :" + prefix);

Modified: incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/W3CDOMStreamWriter.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/W3CDOMStreamWriter.java?rev=590790&r1=590789&r2=590790&view=diff
==============================================================================
--- incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/W3CDOMStreamWriter.java (original)
+++ incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/W3CDOMStreamWriter.java Wed Oct 31 12:08:56 2007
@@ -38,7 +38,7 @@
     private Stack<Element> stack = new Stack<Element>();
     private Document document;
     private Element currentNode;
-    private NamespaceContext context;
+    private NamespaceContext context = new W3CNamespaceContext();
     private Map properties = Collections.EMPTY_MAP;
 
     public W3CDOMStreamWriter() throws ParserConfigurationException {
@@ -59,10 +59,7 @@
         this.document = e.getOwnerDocument();
         
         currentNode = e;
-
-        W3CNamespaceContext newContext = new W3CNamespaceContext();
-        newContext.setElement(currentNode);
-        this.context = newContext;
+        ((W3CNamespaceContext)context).setElement(e);
     }
 
     public void setProperties(Map properties) {
@@ -84,11 +81,10 @@
         } else {
             document.appendChild(element);
         }
-
-        W3CNamespaceContext newContext = new W3CNamespaceContext();
-        newContext.setElement(element);
-        this.context = newContext;
-
+        if (!(context instanceof W3CNamespaceContext)) {
+            context = new W3CNamespaceContext();
+        }
+        ((W3CNamespaceContext)context).setElement(element);
         currentNode = element;
     }
 

Modified: incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/W3CNamespaceContext.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/W3CNamespaceContext.java?rev=590790&r1=590789&r2=590790&view=diff
==============================================================================
--- incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/W3CNamespaceContext.java (original)
+++ incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/W3CNamespaceContext.java Wed Oct 31 12:08:56 2007
@@ -45,6 +45,10 @@
     }
 
     private String getNamespaceURI(Element e, String name) {
+        if (e == null) {
+            return null;
+        }
+
         Attr attr = e.getAttributeNode(name);
         if (attr == null) {
             Node n = e.getParentNode();

Modified: incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java?rev=590790&r1=590789&r2=590790&view=diff
==============================================================================
--- incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java (original)
+++ incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java Wed Oct 31 12:08:56 2007
@@ -32,6 +32,7 @@
 
 import org.xml.sax.InputSource;
 
+import org.apache.cxf.helpers.XMLUtils;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -178,10 +179,28 @@
         String testString = "<ns1:a xmlns:ns1=\"http://www.apache.org/\"><s1 xmlns=\"\">"
             + "abc</s1><s2 xmlns=\"\">def</s2></ns1:a>";
         
-        StringReader reader = new StringReader(testString);
+        cycleString(testString);
+        
+        testString = "<a xmlns=\"http://www.apache.org/\"><s1 xmlns=\"\">"
+            + "abc</s1><s2 xmlns=\"\">def</s2></a>";
+        cycleString(testString);
+
+        testString = "<a xmlns=\"http://www.apache.org/\"><s1 xmlns=\"\">"
+            + "abc</s1><s2>def</s2></a>";
+        cycleString(testString);
+        
+        testString = "<ns1:a xmlns:ns1=\"http://www.apache.org/\"><s1>"
+            + "abc</s1><s2 xmlns=\"\">def</s2></ns1:a>";
+        
+        cycleString(testString);
+    }
+    
+    private void cycleString(String s) throws Exception {
+        StringReader reader = new StringReader(s);
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         dbf.setNamespaceAware(true);
         Document doc = dbf.newDocumentBuilder().parse(new InputSource(reader));
+        String orig = XMLUtils.toString(doc.getDocumentElement());
         
         StringWriter sw = new StringWriter();
         XMLStreamWriter swriter = StaxUtils.createXMLStreamWriter(sw);
@@ -191,8 +210,12 @@
         swriter.close();
         
         String output = sw.toString();
-        assertEquals(testString, output);
-
+        assertEquals(s, output);
+        
+        W3CDOMStreamWriter domwriter = new W3CDOMStreamWriter();
+        StaxUtils.writeDocument(doc, domwriter, false, true);
+        output = XMLUtils.toString(domwriter.getDocument().getDocumentElement());
+        assertEquals(orig, output);
     }
 
 }