cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r1509891 - in /cxf/trunk: api/src/main/java/org/apache/cxf/common/jaxb/ rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/
Date Fri, 02 Aug 2013 22:18:48 GMT
Author: dkulp
Date: Fri Aug  2 22:18:47 2013
New Revision: 1509891

URL: http://svn.apache.org/r1509891
Log:
[CXF-5166] If not eclipse or sun, use the SchemaCollection to try and determine the schema
types

Added:
    cxf/trunk/api/src/main/java/org/apache/cxf/common/jaxb/SchemaCollectionContextProxy.java
Modified:
    cxf/trunk/api/src/main/java/org/apache/cxf/common/jaxb/JAXBUtils.java
    cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java
    cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java

Modified: cxf/trunk/api/src/main/java/org/apache/cxf/common/jaxb/JAXBUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/common/jaxb/JAXBUtils.java?rev=1509891&r1=1509890&r2=1509891&view=diff
==============================================================================
--- cxf/trunk/api/src/main/java/org/apache/cxf/common/jaxb/JAXBUtils.java (original)
+++ cxf/trunk/api/src/main/java/org/apache/cxf/common/jaxb/JAXBUtils.java Fri Aug  2 22:18:47
2013
@@ -83,6 +83,7 @@ import org.apache.cxf.common.util.Reflec
 import org.apache.cxf.common.util.ReflectionUtil;
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.common.util.SystemPropertyAction;
+import org.apache.cxf.common.xmlschema.SchemaCollection;
 import org.apache.cxf.helpers.JavaUtils;
 
 
@@ -1144,6 +1145,11 @@ public final class JAXBUtils {
     }
     
     public static JAXBContextProxy createJAXBContextProxy(final JAXBContext ctx) {
+        return createJAXBContextProxy(ctx, null, null);
+    }
+    public static JAXBContextProxy createJAXBContextProxy(final JAXBContext ctx,
+                                                          final SchemaCollection collection,
+                                                          final String defaultNs) {
         if (ctx.getClass().getName().contains("eclipse")) {
             final org.eclipse.persistence.jaxb.JAXBContext c = (org.eclipse.persistence.jaxb.JAXBContext)ctx;
             return new JAXBContextProxy() {
@@ -1184,7 +1190,11 @@ public final class JAXBUtils {
                 }
             };
         }
-        return ReflectionInvokationHandler.createProxyWrapper(ctx, JAXBContextProxy.class);
+        if (ctx.getClass().getName().contains("com.sun.")
+            || collection == null) {
+            return ReflectionInvokationHandler.createProxyWrapper(ctx, JAXBContextProxy.class);
+        }
+        return new SchemaCollectionContextProxy(ctx, collection, defaultNs);
     }
     public static JAXBBeanInfo getBeanInfo(JAXBContextProxy context, Class<?> cls)
{
         Object o = context.getBeanInfo(cls);

Added: cxf/trunk/api/src/main/java/org/apache/cxf/common/jaxb/SchemaCollectionContextProxy.java
URL: http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/common/jaxb/SchemaCollectionContextProxy.java?rev=1509891&view=auto
==============================================================================
--- cxf/trunk/api/src/main/java/org/apache/cxf/common/jaxb/SchemaCollectionContextProxy.java
(added)
+++ cxf/trunk/api/src/main/java/org/apache/cxf/common/jaxb/SchemaCollectionContextProxy.java
Fri Aug  2 22:18:47 2013
@@ -0,0 +1,240 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.common.jaxb;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.net.URI;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.activation.DataHandler;
+import javax.activation.DataSource;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlSchema;
+import javax.xml.bind.annotation.XmlType;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.transform.Source;
+
+import org.w3c.dom.Document;
+
+import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.common.xmlschema.SchemaCollection;
+import org.apache.ws.commons.schema.XmlSchemaElement;
+import org.apache.ws.commons.schema.XmlSchemaType;
+import org.apache.ws.commons.schema.constants.Constants;
+
+/**
+ * 
+ */
+public class SchemaCollectionContextProxy implements JAXBContextProxy {
+    private static final Map<Class<?>, QName> TYPE_MAP = new HashMap<Class<?>,
QName>();
+    
+    final JAXBContext context;
+    final SchemaCollection schemas;
+    final String defaultNamespace;
+    
+    
+    static {
+        defaultRegister(BigDecimal.class, Constants.XSD_DECIMAL);
+        defaultRegister(BigInteger.class, Constants.XSD_INTEGER);
+        defaultRegister(Boolean.class, Constants.XSD_BOOLEAN);
+        defaultRegister(Calendar.class, Constants.XSD_DATETIME);
+        defaultRegister(Date.class, Constants.XSD_DATETIME);
+        defaultRegister(Float.class, Constants.XSD_FLOAT);
+        defaultRegister(Double.class, Constants.XSD_DOUBLE);
+        defaultRegister(Integer.class, Constants.XSD_INT);
+        defaultRegister(Long.class, Constants.XSD_LONG);
+        defaultRegister(Object.class, Constants.XSD_ANYTYPE);
+        defaultRegister(Byte.class, Constants.XSD_BYTE);
+        defaultRegister(Short.class, Constants.XSD_SHORT);
+        defaultRegister(Source.class, Constants.XSD_ANYTYPE);
+        defaultRegister(String.class, Constants.XSD_STRING);
+        defaultRegister(Time.class, Constants.XSD_TIME);
+        defaultRegister(Timestamp.class, Constants.XSD_DATETIME);
+        defaultRegister(URI.class, Constants.XSD_ANYURI);
+        defaultRegister(XMLStreamReader.class, Constants.XSD_ANYTYPE);
+        
+        defaultRegister(boolean.class, Constants.XSD_BOOLEAN);
+        defaultRegister(Date.class, Constants.XSD_DATETIME);
+        defaultRegister(Float.class, Constants.XSD_FLOAT);
+        defaultRegister(Double.class, Constants.XSD_DOUBLE);
+        defaultRegister(Integer.class, Constants.XSD_INT);
+        defaultRegister(Long.class, Constants.XSD_LONG);
+        defaultRegister(Object.class, Constants.XSD_ANYTYPE);
+        defaultRegister(Byte.class, Constants.XSD_BYTE);
+        defaultRegister(Short.class, Constants.XSD_SHORT);
+        defaultRegister(Source.class, Constants.XSD_ANYTYPE);
+        defaultRegister(String.class, Constants.XSD_STRING);
+        defaultRegister(Time.class, Constants.XSD_TIME);
+        defaultRegister(Timestamp.class, Constants.XSD_DATETIME);
+        defaultRegister(URI.class, Constants.XSD_ANYURI);
+        defaultRegister(XMLStreamReader.class, Constants.XSD_ANYTYPE);
+        
+        defaultRegister(boolean.class, Constants.XSD_BOOLEAN);
+        defaultRegister(byte[].class, Constants.XSD_BASE64);
+        defaultRegister(double.class, Constants.XSD_DOUBLE);
+        defaultRegister(float.class, Constants.XSD_FLOAT);
+        defaultRegister(int.class, Constants.XSD_INT);
+        defaultRegister(short.class, Constants.XSD_SHORT);
+        defaultRegister(byte.class, Constants.XSD_BYTE);
+        defaultRegister(long.class, Constants.XSD_LONG);
+
+        defaultRegister(java.sql.Date.class, Constants.XSD_DATETIME);
+        defaultRegister(java.sql.Date.class, Constants.XSD_DATE);
+        defaultRegister(Number.class, Constants.XSD_DECIMAL);
+
+        defaultRegister(DataSource.class, Constants.XSD_BASE64);
+        defaultRegister(DataHandler.class, Constants.XSD_BASE64);
+        defaultRegister(Document.class, Constants.XSD_ANYTYPE);
+    }
+    
+    public SchemaCollectionContextProxy(JAXBContext ctx, SchemaCollection c, String defaultNs)
{
+        schemas = c;
+        context = ctx;
+        defaultNamespace = defaultNs;
+    }
+
+    private static void defaultRegister(Class<?> cls, QName name) {
+        TYPE_MAP.put(cls, name);
+    }
+
+    public Object getBeanInfo(Class<?> cls) {
+        XmlRootElement xre = cls.getAnnotation(XmlRootElement.class);
+        String name = xre == null ? "##default" : xre.name();
+        String namespace = xre == null ? "##default" : xre.namespace();
+        if ("##default".equals(name)) {
+            name = java.beans.Introspector.decapitalize(cls.getSimpleName());
+        }
+        if ("##default".equals(namespace) && cls.getPackage() != null) {
+            XmlSchema sc = cls.getPackage().getAnnotation(XmlSchema.class);
+            if (sc != null) {
+                namespace = sc.namespace();
+            }
+        }        
+        if ("##default".equals(namespace) || StringUtils.isEmpty(namespace)) {
+            namespace = JAXBUtils.getPackageNamespace(cls);
+            if (namespace == null) {
+                namespace = defaultNamespace;
+            }
+        }
+        final QName qname = new QName(namespace, name);
+        final XmlSchemaElement el = schemas.getElementByQName(qname);
+        XmlSchemaType type = null;
+        if (el != null) {
+            type = el.getSchemaType();
+        }
+        if (type == null) {
+            type = schemas.getTypeByQName(getTypeQName(cls, namespace));
+            if (type == null) {
+                type = schemas.getTypeByQName(qname);
+            }
+        }
+        if (type == null) {
+            type = mapToSchemaType(cls, namespace);
+            /*
+            if (type == null) {
+                type = mapToSchemaType(cls, namespace);
+            }
+            */
+        }
+        if (el == null && type == null) {
+            return null;
+        }
+        final QName typeName = type == null ? null : type.getQName();
+        
+        JAXBBeanInfo bi = new JAXBBeanInfo() {
+            public boolean isElement() {
+                return el == null ? false : true;
+            }
+            public Collection<QName> getTypeNames() {
+                return Collections.singletonList(typeName);
+            }
+            public String getElementNamespaceURI(Object object) {
+                return qname.getNamespaceURI();
+            }
+            public String getElementLocalName(Object object) {
+                return qname.getLocalPart();
+            }
+        };
+        return bi;
+    }
+
+    private QName getTypeQName(Class<?> cls, String namespace) {
+        QName qn = TYPE_MAP.get(cls);
+        if (qn != null) {
+            return qn;
+        }
+        XmlType xtype = cls.getAnnotation(XmlType.class);
+        String tn = xtype == null ? "##default" : xtype.name();
+        String tns = xtype == null ? "##default" : xtype.namespace();
+        if ("##default".equals(tn)) {
+            tn = java.beans.Introspector.decapitalize(cls.getSimpleName());
+        }
+        if ("##default".equals(tns) || StringUtils.isEmpty(tns)) {
+            tns = JAXBUtils.getPackageNamespace(cls);
+        }
+        if ("##default".equals(tns) || StringUtils.isEmpty(tns)) {
+            tns = namespace;
+        }
+        return new QName(tns, tn);
+    }
+    private XmlSchemaType mapToSchemaType(Class<?> cls, String namespace) {
+        QName qn = getTypeQName(cls, namespace);
+        XmlSchemaType type = null;
+        if (qn != null) {
+            type = schemas.getTypeByQName(qn);
+        }
+        if (type == null && cls.isArray()) {
+            Class<?> compType = cls.getComponentType();
+            int count = 1;
+            while (compType.isArray()) {
+                compType = compType.getComponentType();
+                count++;
+            }
+            QName aqn = getTypeQName(compType, namespace);
+            if (aqn != null) {
+                while (count > 0) {
+                    aqn = new QName(aqn.getNamespaceURI(), aqn.getLocalPart() + "Array");
+                    count--;
+                }
+                type = schemas.getTypeByQName(aqn);
+                if (type == null) {
+                    type = schemas.getTypeByQName(new QName("http://jaxb.dev.java.net/array",
aqn.getLocalPart()));
+                }
+            }
+        }
+        /*
+        if (type == null) {
+            System.out.println("HELP: " + cls.getName());
+        }
+        */
+        return type;
+    }
+
+}

Modified: cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java?rev=1509891&r1=1509890&r2=1509891&view=diff
==============================================================================
--- cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java (original)
+++ cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java Fri
Aug  2 22:18:47 2013
@@ -398,7 +398,7 @@ public class JAXBDataBinding extends Abs
             }
 
             JAXBSchemaInitializer schemaInit = new JAXBSchemaInitializer(serviceInfo, col,
riContext,
-                                                                         this.qualifiedSchemas);
+                                                                         this.qualifiedSchemas,
tns);
             schemaInit.walk();
             if (cachedContextAndSchemas != null && !schemasFromCache) {
                 cachedContextAndSchemas.setSchemas(schemas);
@@ -409,13 +409,13 @@ public class JAXBDataBinding extends Abs
     private void justCheckForJAXBAnnotations(ServiceInfo serviceInfo) {
         for (MessageInfo mi: serviceInfo.getMessages().values()) {
             for (MessagePartInfo mpi : mi.getMessageParts()) {
-                checkForJAXBAnnotations(mpi);
+                checkForJAXBAnnotations(mpi, serviceInfo.getXmlSchemaCollection(), serviceInfo.getTargetNamespace());
             }
         }
     }
-    private void checkForJAXBAnnotations(MessagePartInfo mpi) {
+    private void checkForJAXBAnnotations(MessagePartInfo mpi, SchemaCollection schemaCollection,
String ns) {
         Annotation[] anns = (Annotation[])mpi.getProperty("parameter.annotations");
-        JAXBContextProxy ctx = JAXBUtils.createJAXBContextProxy(context);
+        JAXBContextProxy ctx = JAXBUtils.createJAXBContextProxy(context, schemaCollection,
ns);
         XmlJavaTypeAdapter jta = JAXBSchemaInitializer.findFromTypeAdapter(ctx, mpi.getTypeClass(),
anns);
         if (jta != null) {
             JAXBBeanInfo jtaBeanInfo = JAXBSchemaInitializer.findFromTypeAdapter(ctx, jta.value());

Modified: cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java?rev=1509891&r1=1509890&r2=1509891&view=diff
==============================================================================
--- cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java
(original)
+++ cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java
Fri Aug  2 22:18:47 2013
@@ -83,10 +83,11 @@ class JAXBSchemaInitializer extends Serv
     public JAXBSchemaInitializer(ServiceInfo serviceInfo,
                                  SchemaCollection col,
                                  JAXBContext context,
-                                 boolean q) {
+                                 boolean q,
+                                 String defaultNs) {
         super(serviceInfo);
         schemas = col;
-        this.context = JAXBUtils.createJAXBContextProxy(context);
+        this.context = JAXBUtils.createJAXBContextProxy(context, serviceInfo.getXmlSchemaCollection(),
defaultNs);
         this.qualifiedSchemas = q;
     }
 



Mime
View raw message