Search in sources :

Example 6 with XmlJavaTypeAdapter

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

the class WrapperClassGenerator method generatePackageInfo.

private void generatePackageInfo(String className, String ns, Class<?> clz) {
    ClassWriter cw = createClassWriter();
    String classFileName = periodToSlashes(className);
    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();
    }
    AnnotationVisitor av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlSchema;", true);
    av0.visit("namespace", ns);
    av0.visitEnum("elementFormDefault", 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, clz, cw.toByteArray());
}
Also used : XmlNsForm(javax.xml.bind.annotation.XmlNsForm) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) XmlJavaTypeAdapters(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters) Annotation(java.lang.annotation.Annotation) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 7 with XmlJavaTypeAdapter

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

the class AbstractJAXBProvider method getActualType.

protected Class<?> getActualType(Class<?> type, Type genericType, Annotation[] anns) {
    Class<?> theType = null;
    if (JAXBElement.class.isAssignableFrom(type)) {
        theType = InjectionUtils.getActualType(genericType);
    } else {
        theType = type;
    }
    XmlJavaTypeAdapter adapter = org.apache.cxf.jaxrs.utils.JAXBUtils.getAdapter(theType, anns);
    theType = org.apache.cxf.jaxrs.utils.JAXBUtils.getTypeFromAdapter(adapter, theType, false);
    return theType;
}
Also used : XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter)

Example 8 with XmlJavaTypeAdapter

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

the class JAXBElementProvider method marshalCollection.

protected void marshalCollection(Class<?> originalCls, Object collection, Type genericType, String enc, 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;
    QName qname = null;
    if (firstObj instanceof JAXBElement) {
        JAXBElement<?> el = (JAXBElement<?>) firstObj;
        qname = el.getName();
        actualClass = el.getDeclaredType();
    } else {
        qname = getCollectionWrapperQName(actualClass, genericType, firstObj, true);
    }
    if (qname == null) {
        String message = new org.apache.cxf.common.i18n.Message("NO_COLLECTION_ROOT", BUNDLE).toString();
        throw new WebApplicationException(Response.serverError().entity(message).build());
    }
    StringBuilder pi = new StringBuilder();
    pi.append(XML_PI_START + (enc == null ? StandardCharsets.UTF_8.name() : enc) + "\"?>");
    os.write(pi.toString().getBytes());
    String startTag = null;
    String endTag = null;
    if (qname.getNamespaceURI().length() > 0) {
        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() + ">";
    }
    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, enc, os, anns, m, qname.getNamespaceURI());
        while (it.hasNext()) {
            marshalCollectionMember(JAXBUtils.useAdapter(it.next(), adapter, true), actualClass, genericType, enc, os, anns, m, qname.getNamespaceURI());
        }
    }
    os.write(endTag.getBytes());
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) QName(javax.xml.namespace.QName) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) JAXBElement(javax.xml.bind.JAXBElement)

Example 9 with XmlJavaTypeAdapter

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

the class WrapperBeanFieldAnnotator method annotate.

public void annotate(final JavaAnnotatable field) {
    JavaField jField = null;
    if (field instanceof JavaField) {
        jField = (JavaField) field;
    } else {
        throw new RuntimeException("WrapperBeanFiledAnnotator expect JavaField as input");
    }
    String rawName = jField.getRawName();
    boolean hasEl = false;
    for (Annotation ann : jField.getJaxbAnnotaions()) {
        if (ann instanceof XmlMimeType) {
            JAnnotation mimeAnno = new JAnnotation(XmlMimeType.class);
            mimeAnno.addElement(new JAnnotationElement("value", ((XmlMimeType) ann).value()));
            jField.addAnnotation(mimeAnno);
        } else if (ann instanceof XmlJavaTypeAdapter) {
            JAnnotation jaxbAnn = new JAnnotation(XmlJavaTypeAdapter.class);
            jaxbAnn.addElement(new JAnnotationElement("value", ((XmlJavaTypeAdapter) ann).value()));
            jaxbAnn.addElement(new JAnnotationElement("type", ((XmlJavaTypeAdapter) ann).type()));
            jField.addAnnotation(jaxbAnn);
        } else if (ann instanceof XmlAttachmentRef) {
            JAnnotation jaxbAnn = new JAnnotation(XmlAttachmentRef.class);
            jField.addAnnotation(jaxbAnn);
        } else if (ann instanceof XmlList) {
            JAnnotation jaxbAnn = new JAnnotation(XmlList.class);
            jField.addAnnotation(jaxbAnn);
        } else if (ann instanceof XmlElement) {
            hasEl = true;
            XmlElement el = (XmlElement) ann;
            JAnnotation xmlElementAnnotation = new JAnnotation(XmlElement.class);
            xmlElementAnnotation.addElement(new JAnnotationElement("name", el.name()));
            if (!StringUtils.isEmpty(el.namespace())) {
                xmlElementAnnotation.addElement(new JAnnotationElement("namespace", el.namespace()));
            }
            if (el.nillable()) {
                xmlElementAnnotation.addElement(new JAnnotationElement("nillable", el.nillable(), true));
            }
            if (el.required()) {
                xmlElementAnnotation.addElement(new JAnnotationElement("required", el.required(), true));
            }
            if (!StringUtils.isEmpty(el.defaultValue())) {
                xmlElementAnnotation.addElement(new JAnnotationElement("defaultValue", el.defaultValue()));
            }
            jField.addAnnotation(xmlElementAnnotation);
        }
    }
    if (!hasEl) {
        JAnnotation xmlElementAnnotation = new JAnnotation(XmlElement.class);
        xmlElementAnnotation.addElement(new JAnnotationElement("name", rawName));
        if (!StringUtils.isEmpty(jField.getTargetNamespace())) {
            xmlElementAnnotation.addElement(new JAnnotationElement("namespace", jField.getTargetNamespace()));
        }
        jField.addAnnotation(xmlElementAnnotation);
    }
}
Also used : JavaField(org.apache.cxf.tools.common.model.JavaField) XmlMimeType(javax.xml.bind.annotation.XmlMimeType) JAnnotation(org.apache.cxf.tools.common.model.JAnnotation) XmlAttachmentRef(javax.xml.bind.annotation.XmlAttachmentRef) JAnnotationElement(org.apache.cxf.tools.common.model.JAnnotationElement) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) XmlElement(javax.xml.bind.annotation.XmlElement) JAnnotation(org.apache.cxf.tools.common.model.JAnnotation) Annotation(java.lang.annotation.Annotation) XmlList(javax.xml.bind.annotation.XmlList)

Example 10 with XmlJavaTypeAdapter

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

the class XmlJavaTypeAdapterAnnotator method annotate.

public void annotate(JavaAnnotatable jn) {
    JAnnotation jaxbAnnotation = new JAnnotation(XmlJavaTypeAdapter.class);
    jaxbAnnotation.addElement(new JAnnotationElement("value", adapter));
    if (jn instanceof JavaParameter) {
        JavaParameter jp = (JavaParameter) jn;
        jp.addAnnotation("XmlJavaTypeAdapter", jaxbAnnotation);
    } else if (jn instanceof JavaMethod) {
        JavaMethod jm = (JavaMethod) jn;
        jm.addAnnotation("XmlJavaTypeAdapter", jaxbAnnotation);
    } else {
        throw new RuntimeException("Annotation of " + jn.getClass() + " not supported.");
    }
    jf.addImport(XmlJavaTypeAdapter.class.getName());
    jf.addImport(adapter.getName());
}
Also used : JAnnotation(org.apache.cxf.tools.common.model.JAnnotation) JAnnotationElement(org.apache.cxf.tools.common.model.JAnnotationElement) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) JavaParameter(org.apache.cxf.tools.common.model.JavaParameter) JavaMethod(org.apache.cxf.tools.common.model.JavaMethod)

Aggregations

XmlJavaTypeAdapter (javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter)20 Annotation (java.lang.annotation.Annotation)6 XmlList (javax.xml.bind.annotation.XmlList)4 XmlAdapter (javax.xml.bind.annotation.adapters.XmlAdapter)4 ParameterizedType (java.lang.reflect.ParameterizedType)3 XmlAttachmentRef (javax.xml.bind.annotation.XmlAttachmentRef)3 QName (javax.xml.namespace.QName)3 Type (java.lang.reflect.Type)2 JAXBElement (javax.xml.bind.JAXBElement)2 XmlElement (javax.xml.bind.annotation.XmlElement)2 XmlElementWrapper (javax.xml.bind.annotation.XmlElementWrapper)2 XmlMimeType (javax.xml.bind.annotation.XmlMimeType)2 JAXBBeanInfo (org.apache.cxf.common.jaxb.JAXBBeanInfo)2 JAnnotation (org.apache.cxf.tools.common.model.JAnnotation)2 JAnnotationElement (org.apache.cxf.tools.common.model.JAnnotationElement)2 Field (java.lang.reflect.Field)1 GenericArrayType (java.lang.reflect.GenericArrayType)1 Method (java.lang.reflect.Method)1 WildcardType (java.lang.reflect.WildcardType)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1