cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1089065 - in /cxf/trunk: common/common/src/main/java/org/apache/cxf/jaxb/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/packageinfo/ rt/frontend/jaxrs/src/t...
Date Tue, 05 Apr 2011 14:46:14 GMT
Author: sergeyb
Date: Tue Apr  5 14:46:14 2011
New Revision: 1089065

URL: http://svn.apache.org/viewvc?rev=1089065&view=rev
Log:
Using the actual class to get the package as opposed to Package.getPackage

Modified:
    cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/JAXBUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/packageinfo/Book2NoRootElement.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java

Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/JAXBUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/JAXBUtils.java?rev=1089065&r1=1089064&r2=1089065&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/JAXBUtils.java (original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/JAXBUtils.java Tue Apr  5 14:46:14
2011
@@ -778,7 +778,7 @@ public final class JAXBUtils {
     }
     
     public static String getPackageNamespace(Class<?> cls) {
-        Package p = Package.getPackage(PackageUtils.getPackageName(cls));
+        Package p = cls.getPackage();
         if (p != null) {
             javax.xml.bind.annotation.XmlSchema schemaAnn = 
                 p.getAnnotation(javax.xml.bind.annotation.XmlSchema.class);

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java?rev=1089065&r1=1089064&r2=1089065&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
Tue Apr  5 14:46:14 2011
@@ -27,6 +27,7 @@ import java.lang.annotation.Annotation;
 import java.lang.reflect.Array;
 import java.lang.reflect.Type;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -49,6 +50,7 @@ import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
 import javax.xml.bind.annotation.XmlAnyElement;
 import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlType;
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 import javax.xml.namespace.QName;
@@ -59,9 +61,6 @@ import javax.xml.validation.Schema;
 import org.apache.cxf.common.i18n.BundleUtils;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.PackageUtils;
-import org.apache.cxf.common.util.ReflectionInvokationHandler;
-import org.apache.cxf.jaxb.JAXBBeanInfo;
-import org.apache.cxf.jaxb.JAXBContextProxy;
 import org.apache.cxf.jaxb.JAXBUtils;
 import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
@@ -86,7 +85,7 @@ public abstract class AbstractJAXBProvid
     
     protected Set<Class<?>> collectionContextClasses = new HashSet<Class<?>>();
     
-    protected Map<String, String> jaxbElementClassMap;
+    protected Map<String, String> jaxbElementClassMap = Collections.emptyMap();
     protected boolean unmarshalAsJaxbElement;
     protected boolean marshalAsJaxbElement;
     
@@ -105,7 +104,7 @@ public abstract class AbstractJAXBProvid
     private Schema schema;
     private String collectionWrapperName;
     private Map<String, String> collectionWrapperMap;
-    private List<String> jaxbElementClassNames;
+    private List<String> jaxbElementClassNames = Collections.emptyList();
     private Map<String, Object> cProperties;
     private Map<String, Object> uProperties;
     
@@ -182,27 +181,27 @@ public abstract class AbstractJAXBProvid
         return null;
     }
     
+    protected static boolean isXmlRoot(Class<?> cls) {
+        return cls.getAnnotation(XmlRootElement.class) != null;
+    }
+    
     @SuppressWarnings("unchecked")
     protected Object convertToJaxbElementIfNeeded(Object obj, Class<?> cls, Type genericType)

         throws Exception {
         
-        QName name = null;
-        if (jaxbElementClassNames != null && jaxbElementClassNames.contains(cls.getName())

-            || jaxbElementClassMap != null && jaxbElementClassMap.containsKey(cls.getName()))
{
-            if (jaxbElementClassMap != null) {
-                name = JAXRSUtils.convertStringToQName(jaxbElementClassMap.get(cls.getName()));
-            } else {
-                name = getJaxbQName(cls, genericType, obj, false);
-            }
-        }
-        if (name == null && marshalAsJaxbElement) {
-            name = new QName(getPackageNamespace(cls),
-                             cls.getSimpleName());
+        boolean asJaxbElement = jaxbElementClassNames.contains(cls.getName());
+        if (!asJaxbElement && isXmlRoot(cls)) {
+            return obj;
         }
-        if (name != null) {
-            return new JAXBElement(name, cls, null, obj);
+        
+        QName name = null;
+        String expandedName = jaxbElementClassMap.get(cls.getName());
+        if (expandedName != null) {
+            name = JAXRSUtils.convertStringToQName(expandedName);
+        } else if (marshalAsJaxbElement || asJaxbElement) {
+            name = getJaxbQName(cls, genericType, obj, false);
         }
-        return obj;
+        return name != null ? new JAXBElement(name, cls, null, obj) : obj;
     }
     
     public void setCollectionWrapperName(String wName) {
@@ -269,32 +268,23 @@ public abstract class AbstractJAXBProvid
         }
         
         XmlRootElement root = cls.getAnnotation(XmlRootElement.class);
-        QName qname = null;
         if (root != null) {
-            String namespace = getNamespace(root.namespace());
-            if ("".equals(namespace)) {
-                namespace = getPackageNamespace(cls);
-            }
-            String name = getLocalName(root.name(), cls.getSimpleName(), pluralName);
-            return new QName(namespace, name);
+            return getQNameFromNamespaceAndName(root.namespace(), root.name(), cls, pluralName);
+        } else if (cls.getAnnotation(XmlType.class) != null) {
+            XmlType xmlType = cls.getAnnotation(XmlType.class);
+            return getQNameFromNamespaceAndName(xmlType.namespace(), xmlType.name(), cls,
pluralName);
         } else {
-            JAXBContext context = getJAXBContext(cls, type);
-            JAXBContextProxy proxy = ReflectionInvokationHandler.createProxyWrapper(context,
-                                                                                    JAXBContextProxy.class);
-            JAXBBeanInfo info = JAXBUtils.getBeanInfo(proxy, cls);
-            if (info != null) {
-                try {
-                    Object instance = object == null ? cls.newInstance() : object;
-                    String name = getLocalName(info.getElementLocalName(instance), cls.getSimpleName(),

-                                               pluralName);
-                    String namespace = getNamespace(info.getElementNamespaceURI(instance));
-                    return new QName(namespace, name);
-                } catch (Exception ex) {
-                    // ignore
-                }
-            }
+            return new QName(getPackageNamespace(cls), cls.getSimpleName());
+        }
+    }
+    
+    private QName getQNameFromNamespaceAndName(String ns, String localName, Class<?>
cls, boolean plural) {
+        String name = getLocalName(localName, cls.getSimpleName() , plural);
+        String namespace = getNamespace(ns);
+        if ("".equals(namespace)) {
+            namespace = getPackageNamespace(cls);
         }
-        return qname;
+        return new QName(namespace, name);
     }
     
     private String getLocalName(String name, String clsName, boolean pluralName) {
@@ -427,7 +417,7 @@ public abstract class AbstractJAXBProvid
             || isSkipJaxbChecks()) {
             return true;
         }
-        return type.getAnnotation(XmlRootElement.class) != null
+        return isXmlRoot(type)
             || JAXBElement.class.isAssignableFrom(type)
             || objectFactoryOrIndexAvailable(type)
             || (type != genericType && objectFactoryForType(genericType))

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/packageinfo/Book2NoRootElement.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/packageinfo/Book2NoRootElement.java?rev=1089065&r1=1089064&r2=1089065&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/packageinfo/Book2NoRootElement.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/packageinfo/Book2NoRootElement.java
Tue Apr  5 14:46:14 2011
@@ -18,7 +18,9 @@
  */
 package org.apache.cxf.jaxrs.fortest.jaxb.packageinfo;
 
+import javax.xml.bind.annotation.XmlType;
 
+@XmlType(name = "book2")
 public class Book2NoRootElement {
 
     private int id;

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=1089065&r1=1089064&r2=1089065&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
Tue Apr  5 14:46:14 2011
@@ -65,6 +65,7 @@ import org.w3c.dom.Node;
 import org.xml.sax.ContentHandler;
 
 import org.apache.cxf.jaxrs.ext.xml.XMLSource;
+import org.apache.cxf.jaxrs.fortest.jaxb.packageinfo.Book2;
 import org.apache.cxf.jaxrs.fortest.jaxb.packageinfo.Book2NoRootElement;
 import org.apache.cxf.jaxrs.impl.MetadataMap;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
@@ -380,6 +381,8 @@ public class JAXBElementProviderTest ext
                          Book2NoRootElement.class,
                          new Annotation[0], MediaType.TEXT_XML_TYPE, 
                          new MetadataMap<String, Object>(), bos);
+        assertTrue(bos.toString().contains("book2"));
+        assertTrue(bos.toString().contains("http://superbooks"));
         provider.setUnmarshallAsJaxbElement(true);
         
         ByteArrayInputStream is = new ByteArrayInputStream(bos.toByteArray());
@@ -391,6 +394,28 @@ public class JAXBElementProviderTest ext
         assertEquals(book2.getId(), book.getId());
     }
     
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testWriteWithXmlRootElementAndPackageInfo() throws Exception {
+        JAXBElementProvider provider = new JAXBElementProvider();
+        Book2 book = new Book2(333);
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        provider.writeTo(book, Book2.class, 
+                         Book2.class,
+                         new Annotation[0], MediaType.TEXT_XML_TYPE, 
+                         new MetadataMap<String, Object>(), bos);
+        assertTrue(bos.toString().contains("thebook2"));
+        assertTrue(bos.toString().contains("http://superbooks"));
+        
+        ByteArrayInputStream is = new ByteArrayInputStream(bos.toByteArray());
+        Book2 book2 = 
+            (Book2)provider.readFrom(
+                       (Class)Book2.class, 
+                       Book2.class,
+                       new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String,
String>(), is);
+        assertEquals(book2.getId(), book.getId());
+    }
+    
     @Test
     @SuppressWarnings("unchecked")
     public void testWriteWithoutXmlRootElementObjectFactory() throws Exception {



Mime
View raw message