cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r1390058 - /cxf/trunk/api/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
Date Tue, 25 Sep 2012 19:12:08 GMT
Author: dkulp
Date: Tue Sep 25 19:12:07 2012
New Revision: 1390058

URL: http://svn.apache.org/viewvc?rev=1390058&view=rev
Log:
[CXF-4520] Make sure namespaces are written prior to attributes that may use them.
Patch from Ivan/xuhaihong applied

Modified:
    cxf/trunk/api/src/main/java/org/apache/cxf/staxutils/StaxUtils.java

Modified: cxf/trunk/api/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/staxutils/StaxUtils.java?rev=1390058&r1=1390057&r2=1390058&view=diff
==============================================================================
--- cxf/trunk/api/src/main/java/org/apache/cxf/staxutils/StaxUtils.java (original)
+++ cxf/trunk/api/src/main/java/org/apache/cxf/staxutils/StaxUtils.java Tue Sep 25 19:12:07
2012
@@ -25,7 +25,10 @@ import java.io.Reader;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.net.URL;
+import java.util.Collections;
 import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.Stack;
 import java.util.concurrent.ArrayBlockingQueue;
 import java.util.concurrent.BlockingQueue;
@@ -70,7 +73,6 @@ import org.w3c.dom.Node;
 import org.w3c.dom.ProcessingInstruction;
 import org.w3c.dom.Text;
 import org.w3c.dom.UserDataHandler;
-
 import org.xml.sax.InputSource;
 import org.xml.sax.XMLReader;
 
@@ -787,10 +789,8 @@ public final class StaxUtils {
         } else {
             writer.writeStartElement(prefix, localName, ns);
         }
-
-        NamedNodeMap attrs = e.getAttributes();
-        for (int i = 0; i < attrs.getLength(); i++) {
-            Node attr = attrs.item(i);
+        
+        for (Node attr : sortElementAttributes(e.getAttributes())) {          
 
             String name = attr.getLocalName();
             String attrPrefix = attr.getPrefix();
@@ -825,7 +825,7 @@ public final class StaxUtils {
                     } else if (attrPrefix.length() == 0) {
                         writer.writeAttribute(attns, name, value);
                     } else {
-                        if (repairing && DOMUtils.getNamespace(e, attrPrefix) ==
null) {
+                        if (repairing && writer.getNamespaceContext().getNamespaceURI(attrPrefix)
== null) {
                             writer.writeNamespace(attrPrefix, attns);
                         }
                         writer.writeAttribute(attrPrefix, attns, name, value);
@@ -855,6 +855,27 @@ public final class StaxUtils {
         }
     }
 
+    private static List<Node> sortElementAttributes(NamedNodeMap attrs) {
+        if (attrs.getLength() == 0) {
+            return Collections.<Node> emptyList();
+        }
+        List<Node> sortedAttrs = new LinkedList<Node>();
+        for (int i = 0; i < attrs.getLength(); i++) {
+            Node attr = attrs.item(i);
+            String name = attr.getLocalName();          
+            if (name == null) {
+                name = attr.getNodeName();
+            }
+            if ("xmlns".equals(attr.getPrefix()) || "xmlns".equals(name)) {
+                sortedAttrs.add(0, attr);
+            } else {
+                sortedAttrs.add(attr);
+            }
+        }
+
+        return sortedAttrs;
+    }
+
     public static void writeNode(Node n, XMLStreamWriter writer, boolean repairing) 
         throws XMLStreamException {
         



Mime
View raw message