cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1166908 - in /cxf/branches/2.4.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
Date Thu, 08 Sep 2011 21:18:35 GMT
Author: sergeyb
Date: Thu Sep  8 21:18:34 2011
New Revision: 1166908

URL: http://svn.apache.org/viewvc?rev=1166908&view=rev
Log:
Merged revisions 1166907 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1166907 | sergeyb | 2011-09-08 22:14:12 +0100 (Thu, 08 Sep 2011) | 1 line
  
  [CXF-3795] Configuring JAXBElementProvider to optionally register a namespace to prefix
mapper
........

Modified:
    cxf/branches/2.4.x-fixes/   (props changed)
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
    svn:mergeinfo = /cxf/trunk:1166907

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=1166908&r1=1166907&r2=1166908&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
Thu Sep  8 21:18:34 2011
@@ -79,11 +79,15 @@ public class JAXBElementProvider extends
                                     Marshaller.JAXB_SCHEMA_LOCATION});
     
     private Map<String, Object> mProperties = Collections.emptyMap();
+    private Map<String, String> nsPrefixes = Collections.emptyMap();
     
     public JAXBElementProvider() {
         
     }
     
+    public void setNamespacePrefixes(Map<String, String> prefixes) {
+        nsPrefixes = prefixes;
+    }
     
     @Override
     protected boolean canBeReadAsJaxbElement(Class<?> type) {
@@ -320,16 +324,21 @@ public class JAXBElementProvider extends
         ms.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
         if (ns.length() > 0) {
             Map<String, String> map = Collections.singletonMap(ns, "ns1");
-            NamespaceMapper nsMapper = new NamespaceMapper(map);
-            try {
-                ms.setProperty("com.sun.xml.bind.namespacePrefixMapper", nsMapper);
-            } catch (PropertyException ex) {
-                ms.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", nsMapper);
-            }
+            map.putAll(nsPrefixes);
+            setNamespaceMapper(ms, map);
         }
         marshal(obj, cls, genericType, enc, os, mt, ms);
     }
     
+    protected static void setNamespaceMapper(Marshaller ms, Map<String, String> map)
throws Exception {
+        NamespaceMapper nsMapper = new NamespaceMapper(map);
+        try {
+            ms.setProperty("com.sun.xml.bind.namespacePrefixMapper", nsMapper);
+        } catch (PropertyException ex) {
+            ms.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", nsMapper);
+        }
+    }
+    
     protected void marshal(Object obj, Class<?> cls, Type genericType, 
                            String enc, OutputStream os, MediaType mt) throws Exception {
         obj = convertToJaxbElementIfNeeded(obj, cls, genericType);
@@ -338,6 +347,9 @@ public class JAXBElementProvider extends
         }
         
         Marshaller ms = createMarshaller(obj, cls, genericType, enc);
+        if (!nsPrefixes.isEmpty()) {
+            setNamespaceMapper(ms, nsPrefixes);
+        }
         addAttachmentMarshaller(ms);
         marshal(obj, cls, genericType, enc, os, mt, ms);
     }

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java?rev=1166908&r1=1166907&r2=1166908&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
Thu Sep  8 21:18:34 2011
@@ -323,6 +323,19 @@ public class JAXBElementProviderTest ext
     }
     
     @Test
+    public void testWriteWithCustomPrefixes() throws Exception {
+        JAXBElementProvider provider = new JAXBElementProvider();
+        provider.setNamespacePrefixes(
+            Collections.singletonMap("http://tags", "prefix"));
+        TagVO2 tag = new TagVO2("a", "b");
+        
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        provider.writeTo(tag, TagVO2.class, TagVO2.class,
+                       new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String,
Object>(), bos);
+        assertTrue(bos.toString().contains("prefix:thetag"));
+    }
+    
+    @Test
     public void testWriteWithoutXmlRootElement() throws Exception {
         doTestWriteWithoutXmlRootElement("SuperBook", false, false);
     }



Mime
View raw message