Author: dkulp Date: Tue Sep 25 19:32:14 2012 New Revision: 1390092 URL: http://svn.apache.org/viewvc?rev=1390092&view=rev Log: Merged revisions 1390080 via git cherry-pick from https://svn.apache.org/repos/asf/cxf/branches/2.5.x-fixes
........ r1390080 | dkulp | 2012-09-25 15:24:13 -0400 (Tue, 25 Sep 2012) | 19 lines Merged revisions 1390064 via git cherry-pick from https://svn.apache.org/repos/asf/cxf/branches/2.6.x-fixes ........ r1390064 | dkulp | 2012-09-25 15:16:35 -0400 (Tue, 25 Sep 2012) | 11 lines 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.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java Modified: cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java?rev=1390092&r1=1390091&r2=1390092&view=diff ============================================================================== --- cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java (original) +++ cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java Tue Sep 25 19:32:14 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; @@ -751,10 +753,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(); @@ -789,7 +789,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); @@ -819,6 +819,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 {
