Author: sergeyb Date: Fri Dec 2 22:13:29 2011 New Revision: 1209728 URL: http://svn.apache.org/viewvc?rev=1209728&view=rev Log: [CXF-3946] Using a mutable map to keep the namespace-prefix pairs when dealing with collections
Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=1209728&r1=1209727&r2=1209728&view=diff ============================================================================== --- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java (original) +++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java Fri Dec 2 22:13:29 2011 @@ -27,6 +27,7 @@ import java.lang.reflect.Type; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -279,15 +280,20 @@ public class JAXBElementProvider extends .entity(message).build()); } - StringBuilder prefix = new StringBuilder(); - prefix.append(XML_PI_START + (enc == null ? "UTF-8" : enc) + "\"?>"); - os.write(prefix.toString().getBytes()); + StringBuilder pi = new StringBuilder(); + pi.append(XML_PI_START + (enc == null ? "UTF-8" : enc) + "\"?>"); + os.write(pi.toString().getBytes()); String startTag = null; String endTag = null; + if (qname.getNamespaceURI().length() > 0) { - startTag = "<ns1:" + qname.getLocalPart() + " xmlns:ns1=\"" + qname.getNamespaceURI() - + "\">"; - endTag = "</ns1:" + qname.getLocalPart() + ">"; + String prefix = nsPrefixes.get(qname.getNamespaceURI()); + if (prefix == null) { + prefix = "ns1"; + } + startTag = "<" + prefix + ":" + qname.getLocalPart() + " xmlns:" + prefix + "=\"" + + qname.getNamespaceURI() + "\">"; + endTag = "</" + prefix + ":" + qname.getLocalPart() + ">"; } else { startTag = "<" + qname.getLocalPart() + ">"; endTag = "</" + qname.getLocalPart() + ">"; @@ -329,7 +335,11 @@ public class JAXBElementProvider extends Marshaller ms = createMarshaller(obj, cls, genericType, enc); ms.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); if (ns.length() > 0) { - Map<String, String> map = Collections.singletonMap(ns, "ns1"); + Map<String, String> map = new HashMap<String, String>(); + // set the default just in case + if (!nsPrefixes.containsKey(ns)) { + map.put(ns, "ns1"); + } map.putAll(nsPrefixes); setNamespaceMapper(ms, map); } Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java?rev=1209728&r1=1209727&r2=1209728&view=diff ============================================================================== --- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java (original) +++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java Fri Dec 2 22:13:29 2011 @@ -80,6 +80,7 @@ import org.apache.cxf.jaxrs.resources.Su import org.apache.cxf.jaxrs.resources.TagVO; import org.apache.cxf.jaxrs.resources.TagVO2; import org.apache.cxf.jaxrs.resources.Tags; +import org.apache.cxf.jaxrs.utils.ParameterizedCollectionType; import org.apache.cxf.jaxrs.utils.ResourceUtils; import org.junit.Assert; @@ -336,6 +337,20 @@ public class JAXBElementProviderTest ext } @Test + public void testWriteCollectionWithCustomPrefixes() throws Exception { + JAXBElementProvider provider = new JAXBElementProvider(); + provider.setNamespacePrefixes( + Collections.singletonMap("http://tags", "prefix")); + TagVO2 tag = new TagVO2("a", "b"); + List<TagVO2> tags = Collections.singletonList(tag); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + provider.writeTo(tags, List.class, new ParameterizedCollectionType<TagVO2>(TagVO2.class), + new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos); + assertTrue(bos.toString().contains("prefix:thetag")); + assertFalse(bos.toString().contains("ns1:thetag")); + } + + @Test public void testWriteWithoutXmlRootElement() throws Exception { doTestWriteWithoutXmlRootElement("SuperBook", false, false); }