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 2012/09/25 21:16:35 UTC

svn commit: r1390064 - /cxf/branches/2.6.x-fixes/api/src/main/java/org/apache/cxf/staxutils/StaxUtils.java

Author: dkulp
Date: Tue Sep 25 19:16:35 2012
New Revision: 1390064

URL: http://svn.apache.org/viewvc?rev=1390064&view=rev
Log:
Merged revisions 1390058 via  git cherry-pick from
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1390058 | dkulp | 2012-09-25 15:12:07 -0400 (Tue, 25 Sep 2012) | 3 lines

  [CXF-4520] Make sure namespaces are written prior to attributes that may use them.
  Patch from Ivan/xuhaihong applied

........

Modified:
    cxf/branches/2.6.x-fixes/api/src/main/java/org/apache/cxf/staxutils/StaxUtils.java

Modified: cxf/branches/2.6.x-fixes/api/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.6.x-fixes/api/src/main/java/org/apache/cxf/staxutils/StaxUtils.java?rev=1390064&r1=1390063&r2=1390064&view=diff
==============================================================================
--- cxf/branches/2.6.x-fixes/api/src/main/java/org/apache/cxf/staxutils/StaxUtils.java (original)
+++ cxf/branches/2.6.x-fixes/api/src/main/java/org/apache/cxf/staxutils/StaxUtils.java Tue Sep 25 19:16:35 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 {