Search in sources :

Example 26 with XmlJavaTypeAdapter

use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.

the class JAXBSchemaInitializer method begin.

@Override
public void begin(MessagePartInfo part) {
    // Check to see if the WSDL information has been filled in for us.
    if (part.getTypeQName() != null || part.getElementQName() != null) {
        checkForExistence(part);
        return;
    }
    Class<?> clazz = part.getTypeClass();
    if (clazz == null) {
        return;
    }
    boolean isFromWrapper = part.getMessageInfo().getOperation().isUnwrapped();
    boolean isList = false;
    if (clazz.isArray()) {
        if (isFromWrapper && !Byte.TYPE.equals(clazz.getComponentType())) {
            clazz = clazz.getComponentType();
        } else if (!isFromWrapper) {
            Annotation[] anns = (Annotation[]) part.getProperty("parameter.annotations");
            for (Annotation a : anns) {
                if (a instanceof XmlList) {
                    part.setProperty("honor.jaxb.annotations", Boolean.TRUE);
                    clazz = clazz.getComponentType();
                    isList = true;
                }
            }
        }
    }
    Annotation[] anns = (Annotation[]) part.getProperty("parameter.annotations");
    XmlJavaTypeAdapter jta = findFromTypeAdapter(context, clazz, anns);
    JAXBBeanInfo jtaBeanInfo = null;
    if (jta != null) {
        jtaBeanInfo = findFromTypeAdapter(context, jta.value());
    }
    JAXBBeanInfo beanInfo = getBeanInfo(clazz);
    if (jtaBeanInfo != beanInfo && jta != null) {
        beanInfo = jtaBeanInfo;
        if (anns == null) {
            anns = new Annotation[] { jta };
        } else {
            boolean found = false;
            for (Annotation t : anns) {
                if (t == jta) {
                    found = true;
                }
            }
            if (!found) {
                Annotation[] tmp = new Annotation[anns.length + 1];
                System.arraycopy(anns, 0, tmp, 0, anns.length);
                tmp[anns.length] = jta;
                anns = tmp;
            }
        }
        part.setProperty("parameter.annotations", anns);
        part.setProperty("honor.jaxb.annotations", Boolean.TRUE);
    }
    if (beanInfo == null) {
        if (Exception.class.isAssignableFrom(clazz)) {
            QName name = (QName) part.getMessageInfo().getProperty("elementName");
            part.setElementQName(name);
            buildExceptionType(part, clazz);
        }
        return;
    }
    boolean isElement = beanInfo.isElement() && !Boolean.TRUE.equals(part.getMessageInfo().getOperation().getProperty("operation.force.types"));
    boolean hasType = !beanInfo.getTypeNames().isEmpty();
    if (isElement && isFromWrapper && hasType) {
        // if there is both a Global element and a global type, AND we are in a wrapper,
        // make sure we use the type instead of a ref to the element to
        // match the rules for wrapped/unwrapped
        isElement = false;
    }
    part.setElement(isElement);
    if (isElement) {
        QName name = new QName(beanInfo.getElementNamespaceURI(null), beanInfo.getElementLocalName(null));
        XmlSchemaElement el = schemas.getElementByQName(name);
        if (el != null && el.getRef().getTarget() != null) {
            part.setTypeQName(el.getRef().getTargetQName());
        } else {
            part.setElementQName(name);
        }
        part.setXmlSchema(el);
    } else {
        QName typeName = getTypeName(beanInfo);
        if (typeName != null) {
            XmlSchemaType type = schemas.getTypeByQName(typeName);
            if (isList && type instanceof XmlSchemaSimpleType) {
                XmlSchemaSimpleType simpleType = new XmlSchemaSimpleType(type.getParent(), false);
                XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList();
                XmlSchemaSimpleType stype = (XmlSchemaSimpleType) type;
                list.setItemTypeName(stype.getQName());
                simpleType.setContent(list);
                part.setXmlSchema(simpleType);
                if (part.getConcreteName() == null) {
                    part.setConcreteName(new QName(null, part.getName().getLocalPart()));
                }
            } else {
                part.setTypeQName(typeName);
                part.setXmlSchema(type);
            }
        }
    }
}
Also used : XmlSchemaSimpleTypeList(org.apache.ws.commons.schema.XmlSchemaSimpleTypeList) QName(javax.xml.namespace.QName) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) JAXBBeanInfo(org.apache.cxf.common.jaxb.JAXBBeanInfo) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) Annotation(java.lang.annotation.Annotation) XmlList(javax.xml.bind.annotation.XmlList)

Example 27 with XmlJavaTypeAdapter

use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.

the class Utils method getMethodReturnType.

static Class<?> getMethodReturnType(Method m) {
    XmlJavaTypeAdapter adapter = getMethodXJTA(m);
    // adapter handle what gets populated
    if (adapter == null && m.getGenericReturnType() instanceof ParameterizedType && ((ParameterizedType) m.getGenericReturnType()).getActualTypeArguments().length < 2) {
        return null;
    }
    Class<?> adapterType = (Class<?>) getTypeFromXmlAdapter(adapter);
    return adapterType != null ? adapterType : m.getReturnType();
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter)

Example 28 with XmlJavaTypeAdapter

use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.

the class Utils method getFieldValue.

@SuppressWarnings({ "rawtypes", "unchecked" })
static Object getFieldValue(Field f, Object target) throws Exception {
    XmlJavaTypeAdapter adapterAnnotation = getFieldXJTA(f);
    XmlAdapter adapter = getXmlAdapter(adapterAnnotation);
    return adapter != null ? adapter.marshal(f.get(target)) : f.get(target);
}
Also used : XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) XmlAdapter(javax.xml.bind.annotation.adapters.XmlAdapter)

Example 29 with XmlJavaTypeAdapter

use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.

the class WrapperClassGenerator method generatePackageInfo.

// CHECKSTYLE:ON
private void generatePackageInfo(String className, String ns, Class<?> clz, Method method, InterfaceInfo interfaceInfo, boolean qualified) {
    ASMHelper.ClassWriter cw = helper.createClassWriter();
    String classFileName = StringUtils.periodToSlashes(className);
    OpcodesProxy opCodes = helper.getOpCodes();
    cw.visit(opCodes.V1_5, opCodes.ACC_ABSTRACT + opCodes.ACC_INTERFACE, classFileName, null, "java/lang/Object", null);
    boolean q = qualified;
    SchemaInfo si = interfaceInfo.getService().getSchema(ns);
    if (si != null) {
        q = si.isElementFormQualified();
    }
    ASMHelper.AnnotationVisitor av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlSchema;", true);
    av0.visit("namespace", ns);
    av0.visitEnum("elementFormDefault", helper.getClassCode(XmlNsForm.class), q ? "QUALIFIED" : "UNQUALIFIED");
    av0.visitEnd();
    if (clz.getPackage() != null && clz.getPackage().getAnnotations() != null) {
        for (Annotation ann : clz.getPackage().getAnnotations()) {
            if (ann instanceof XmlJavaTypeAdapters) {
                av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/adapters/XmlJavaTypeAdapters;", true);
                generateXmlJavaTypeAdapters(av0, (XmlJavaTypeAdapters) ann);
                av0.visitEnd();
            } else if (ann instanceof XmlJavaTypeAdapter) {
                av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/adapters/XmlJavaTypeAdapter;", true);
                generateXmlJavaTypeAdapter(av0, (XmlJavaTypeAdapter) ann);
                av0.visitEnd();
            }
        }
    }
    cw.visitEnd();
    loadClass(className, method.getDeclaringClass(), cw.toByteArray());
}
Also used : OpcodesProxy(org.apache.cxf.common.util.OpcodesProxy) XmlNsForm(javax.xml.bind.annotation.XmlNsForm) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) ASMHelper(org.apache.cxf.common.util.ASMHelper) XmlJavaTypeAdapters(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters) Annotation(java.lang.annotation.Annotation) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 30 with XmlJavaTypeAdapter

use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.

the class JSONProvider method marshalCollection.

protected void marshalCollection(Class<?> originalCls, Object collection, Type genericType, String encoding, OutputStream os, MediaType m, Annotation[] anns) throws Exception {
    Class<?> actualClass = InjectionUtils.getActualType(genericType);
    actualClass = getActualType(actualClass, genericType, anns);
    Collection<?> c = originalCls.isArray() ? Arrays.asList((Object[]) collection) : (Collection<?>) collection;
    Iterator<?> it = c.iterator();
    Object firstObj = it.hasNext() ? it.next() : null;
    final String startTag;
    final String endTag;
    if (!dropCollectionWrapperElement) {
        final QName qname;
        if (firstObj instanceof JAXBElement) {
            JAXBElement<?> el = (JAXBElement<?>) firstObj;
            qname = el.getName();
            actualClass = el.getDeclaredType();
        } else {
            qname = getCollectionWrapperQName(actualClass, genericType, firstObj, false);
        }
        String prefix = "";
        if (!ignoreNamespaces) {
            prefix = namespaceMap.get(qname.getNamespaceURI());
            if (prefix != null) {
                if (!prefix.isEmpty()) {
                    prefix += ".";
                }
            } else if (!qname.getNamespaceURI().isEmpty()) {
                prefix = "ns1.";
            }
        }
        prefix = (prefix == null) ? "" : prefix;
        startTag = "{\"" + prefix + qname.getLocalPart() + "\":[";
        endTag = "]}";
    } else if (serializeAsArray) {
        startTag = "[";
        endTag = "]";
    } else {
        startTag = "{";
        endTag = "}";
    }
    os.write(startTag.getBytes());
    if (firstObj != null) {
        XmlJavaTypeAdapter adapter = org.apache.cxf.jaxrs.utils.JAXBUtils.getAdapter(firstObj.getClass(), anns);
        marshalCollectionMember(JAXBUtils.useAdapter(firstObj, adapter, true), actualClass, genericType, encoding, os);
        while (it.hasNext()) {
            os.write(',');
            marshalCollectionMember(JAXBUtils.useAdapter(it.next(), adapter, true), actualClass, genericType, encoding, os);
        }
    }
    os.write(endTag.getBytes());
}
Also used : QName(javax.xml.namespace.QName) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) JAXBElement(javax.xml.bind.JAXBElement)

Aggregations

XmlJavaTypeAdapter (javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter)40 Annotation (java.lang.annotation.Annotation)10 XmlList (javax.xml.bind.annotation.XmlList)7 QName (javax.xml.namespace.QName)7 EnunciateException (com.webcohesion.enunciate.EnunciateException)5 DecoratedDeclaredType (com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType)5 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)5 DeclaredType (javax.lang.model.type.DeclaredType)5 JAXBElement (javax.xml.bind.JAXBElement)5 XmlElement (javax.xml.bind.annotation.XmlElement)5 XmlAdapter (javax.xml.bind.annotation.adapters.XmlAdapter)5 XmlJavaTypeAdapters (javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters)5 ParameterizedType (java.lang.reflect.ParameterizedType)4 Type (java.lang.reflect.Type)4 TypeElement (javax.lang.model.element.TypeElement)4 XmlAttachmentRef (javax.xml.bind.annotation.XmlAttachmentRef)4 JAXBBeanInfo (org.apache.cxf.common.jaxb.JAXBBeanInfo)4 DecoratedProcessingEnvironment (com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment)3 Method (java.lang.reflect.Method)3 TypeMirror (javax.lang.model.type.TypeMirror)3