Author: dkulp
Date: Sat Jan 8 03:37:39 2011
New Revision: 1056610
URL: http://svn.apache.org/viewvc?rev=1056610&view=rev
Log:
[CXF-3193] Allow CachingXmlEventWriter ns cache to have multiple
prefixes for a namespace.
Modified:
cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/CachingXmlEventWriter.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=1056610&r1=1056609&r2=1056610&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
Sat Jan 8 03:37:39 2011
@@ -233,35 +233,44 @@ public class CachingXmlEventWriter imple
parent = p;
}
public void addNs(String pfx, String ns) {
- map.put(ns, pfx);
+ map.put(pfx, ns);
}
public String getNamespaceURI(String prefix) {
- for (Map.Entry<String, String> e : map.entrySet()) {
- if (e.getValue().equals(prefix)) {
- return e.getKey();
- }
- }
- if (parent != null) {
+ String ret = map.get(prefix);
+ if (ret == null && parent != null) {
return parent.getNamespaceURI(prefix);
}
- return null;
+ return ret;
}
public String getPrefix(String namespaceURI) {
- String ret = map.get(namespaceURI);
- if (ret == null && parent != null) {
+ for (Map.Entry<String, String> e : map.entrySet()) {
+ if (e.getValue().equals(namespaceURI)) {
+ return e.getKey();
+ }
+ }
+ if (parent != null) {
return parent.getPrefix(namespaceURI);
}
- return ret;
+ return null;
}
public Iterator getPrefixes(String namespaceURI) {
- String pfx = getPrefix(namespaceURI);
- if (pfx == null) {
- return Collections.emptyList().iterator();
+ List<String> l = new ArrayList<String>();
+ for (Map.Entry<String, String> e : map.entrySet()) {
+ if (e.getValue().equals(namespaceURI)) {
+ l.add(e.getKey());
+ }
+ }
+ if (l.isEmpty()) {
+ String pfx = getPrefix(namespaceURI);
+ if (pfx == null) {
+ return Collections.emptyList().iterator();
+ }
+ return Collections.singleton(pfx).iterator();
}
- return Collections.singleton(pfx).iterator();
+ return l.iterator();
}
}
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=1056610&r1=1056609&r2=1056610&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
Sat Jan 8 03:37:39 2011
@@ -23,9 +23,9 @@ import java.io.*;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
+import javax.xml.stream.events.XMLEvent;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
@@ -281,19 +281,32 @@ public class StaxUtilsTest extends Asser
@Test
public void testDefaultPrefix() throws Exception {
- try {
- String soapMessage = "./resources/AddRequest.xml";
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- XMLStreamReader reader =
StaxUtils.createXMLStreamReader(getTestStream(soapMessage));
- XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(baos);
- StaxSource staxSource = new StaxSource(reader);
- StaxUtils.copy(staxSource, writer);
- writer.flush();
- baos.flush();
- } catch (XMLStreamException e) {
- fail("shouldn't catch this exception");
+ String soapMessage = "./resources/AddRequest.xml";
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ XMLStreamReader reader =
StaxUtils.createXMLStreamReader(getTestStream(soapMessage));
+ XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(baos);
+ StaxSource staxSource = new StaxSource(reader);
+ StaxUtils.copy(staxSource, writer);
+ writer.flush();
+ baos.flush();
+ }
+
+ @Test
+ public void testCXF3193() throws Exception {
+ String testString = "<a:elem1 xmlns:a=\"test\" xmlns:b=\"test\"
a:attr1=\"value\"/>";
+ CachingXmlEventWriter writer = new CachingXmlEventWriter();
+ StaxUtils.copy(StaxUtils.createXMLStreamReader(new
StringReader(testString)),
+ writer);
+ StringWriter swriter = new StringWriter();
+ XMLStreamWriter xwriter = StaxUtils.createXMLStreamWriter(swriter);
+ for (XMLEvent event : writer.getEvents()) {
+ StaxUtils.writeEvent(event, xwriter);
}
-
+ xwriter.flush();
- }
+ String s = swriter.toString();
+ int idx = s.indexOf("xmlns:a");
+ idx = s.indexOf("xmlns:a", idx + 1);
+ assertEquals(-1, idx);
+ }
}