commit 006903e397cae4eb28c11774ea5e194750eef38f (HEAD, openbis_development_branch) Author: Jakub Straszewski Date: Thu Sep 4 14:51:55 2014 +0200 Fix the problem when namespace declarations where not being inserted in right place. diff --git a/xoai-common/src/main/java/com/lyncode/xoai/xml/EchoElement.java b/xoai-common/src/main/java/com/lyncode/xoai/xml/EchoElement.java index 9d68732..90d8511 100644 --- a/xoai-common/src/main/java/com/lyncode/xoai/xml/EchoElement.java +++ b/xoai-common/src/main/java/com/lyncode/xoai/xml/EchoElement.java @@ -3,6 +3,7 @@ package com.lyncode.xoai.xml; import com.lyncode.xml.XmlWritable; import com.lyncode.xml.XmlWriter; import com.lyncode.xml.exceptions.XmlWriteException; +import com.sun.xml.internal.stream.events.NamespaceImpl; import org.codehaus.stax2.XMLInputFactory2; import javax.xml.namespace.QName; @@ -11,16 +12,14 @@ import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.Attribute; import javax.xml.stream.events.XMLEvent; +import javax.xml.stream.events.StartElement; import java.io.ByteArrayInputStream; -import java.util.ArrayList; import java.util.Iterator; -import java.util.List; public class EchoElement implements XmlWritable { private static XMLInputFactory factory = XMLInputFactory2.newFactory(); private String xmlString = null; - private List declaredPrefixes = new ArrayList(); public EchoElement(String xmlString) { this.xmlString = xmlString; @@ -34,9 +33,17 @@ public class EchoElement implements XmlWritable { XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { - QName name = event.asStartElement().getName(); + StartElement startElement = event.asStartElement(); + QName name = startElement.getName(); writer.writeStartElement(name.getPrefix(), name.getLocalPart(), name.getNamespaceURI()); - addNamespaceIfRequired(writer, name); + + @SuppressWarnings("unchecked") + Iterator name_it = startElement.getNamespaces(); + + while (name_it.hasNext()) { + NamespaceImpl namespace = name_it.next(); + writer.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI()); + } @SuppressWarnings("unchecked") Iterator it = event.asStartElement().getAttributes(); @@ -44,7 +51,6 @@ public class EchoElement implements XmlWritable { while (it.hasNext()) { Attribute attr = it.next(); QName attrName = attr.getName(); - addNamespaceIfRequired(writer, attrName); writer.writeAttribute(attrName.getPrefix(), attrName.getNamespaceURI(), attrName.getLocalPart(), attr.getValue()); } } else if (event.isEndElement()) { @@ -57,11 +63,4 @@ public class EchoElement implements XmlWritable { throw new XmlWriteException("Error trying to output '"+this.xmlString+"'", e); } } - - private void addNamespaceIfRequired(XmlWriter writer, QName name) throws XMLStreamException { - if (!declaredPrefixes.contains(name.getPrefix())) { - writer.writeNamespace(name.getPrefix(), name.getNamespaceURI()); - declaredPrefixes.add(name.getPrefix()); - } - } }