cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r825221 - in /cxf/trunk/common/common/src: main/java/org/apache/cxf/staxutils/CachingXmlEventWriter.java main/java/org/apache/cxf/staxutils/StaxUtils.java test/java/org/apache/cxf/staxutils/StaxUtilsTest.java
Date Wed, 14 Oct 2009 19:02:38 GMT
Author: dkulp
Date: Wed Oct 14 19:02:38 2009
New Revision: 825221

URL: http://svn.apache.org/viewvc?rev=825221&view=rev
Log:
[CXF-2468] Make the StaxUtils.copy stuff repare attribute namespaces as
well as element namespaces

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

Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/CachingXmlEventWriter.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/CachingXmlEventWriter.java?rev=825221&r1=825220&r2=825221&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/CachingXmlEventWriter.java
(original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/CachingXmlEventWriter.java
Wed Oct 14 19:02:38 2009
@@ -270,6 +270,9 @@
 
         public Iterator getPrefixes(String namespaceURI) {
             String pfx = getPrefix(namespaceURI);
+            if (pfx == null) {
+                return Collections.emptyList().iterator();
+            }
             return Collections.singleton(pfx).iterator();
         }
         

Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java?rev=825221&r1=825220&r2=825221&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java (original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java Wed Oct
14 19:02:38 2009
@@ -68,6 +68,7 @@
 import org.apache.cxf.common.classloader.ClassLoaderUtils;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.helpers.DOMUtils;
 import org.apache.cxf.helpers.XMLUtils;
 
@@ -449,9 +450,16 @@
 //        System.out.println("STAXUTILS:writeStartElement : node name : " + local +  " namespace
URI" + uri);
         boolean writeElementNS = false;
         if (uri != null) {
-            String boundPrefix = writer.getPrefix(uri);
-            if (boundPrefix == null || !prefix.equals(boundPrefix)) {
-                writeElementNS = true;
+            writeElementNS = true;
+            Iterator<String> it = CastUtils.cast(writer.getNamespaceContext().getPrefixes(uri));
+            while (it != null && it.hasNext()) {
+                String s = it.next();
+                if (s == null) {
+                    s = "";
+                }
+                if (s.equals(prefix)) {
+                    writeElementNS = false;
+                }
             }
         }
 
@@ -509,6 +517,20 @@
                 writer.writeAttribute(reader.getAttributeNamespace(i), reader.getAttributeLocalName(i),
                                       reader.getAttributeValue(i));
             } else {
+                Iterator<String> it = CastUtils.cast(writer.getNamespaceContext().getPrefixes(ns));
+                boolean writeNs = true;
+                while (it != null && it.hasNext()) {
+                    String s = it.next();
+                    if (s == null) {
+                        s = "";
+                    }
+                    if (s.equals(nsPrefix)) {
+                        writeNs = false;
+                    }
+                }
+                if (writeNs) {
+                    writer.writeNamespace(nsPrefix, ns);
+                }
                 writer.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i),
reader
                     .getAttributeLocalName(i), reader.getAttributeValue(i));
             }

Modified: cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java?rev=825221&r1=825220&r2=825221&view=diff
==============================================================================
--- cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java (original)
+++ cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java Wed
Oct 14 19:02:38 2009
@@ -28,7 +28,9 @@
 import javax.xml.transform.Source;
 import javax.xml.transform.dom.DOMSource;
 
+import org.w3c.dom.Attr;
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 
 import org.xml.sax.InputSource;
 
@@ -111,6 +113,22 @@
         assertEquals(input, output);
     }
     
+    @Test
+    public void testCXF2468() throws Exception {
+        Document doc = XMLUtils.newDocument();
+        doc.appendChild(doc.createElementNS("http://blah.org/", "blah"));
+        Element foo = doc.createElementNS("http://blah.org/", "foo");
+        Attr attr = doc.createAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:nil");
+        attr.setValue("true");
+        foo.setAttributeNodeNS(attr);
+        doc.getDocumentElement().appendChild(foo);
+        XMLStreamReader sreader = StaxUtils.createXMLStreamReader(doc);
+        StringWriter sw = new StringWriter();
+        XMLStreamWriter swriter = StaxUtils.createXMLStreamWriter(sw);
+        StaxUtils.copy(sreader, swriter, true);
+        swriter.flush();
+        assertTrue("No xsi namespace: " + sw.toString(), sw.toString().contains("XMLSchema-instance"));
+    }
     
     @Test
     public void testNonNamespaceAwareParser() throws Exception {



Mime
View raw message