Search in sources :

Example 11 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;
    String startTag = null;
    String endTag = null;
    if (!dropCollectionWrapperElement) {
        QName qname = null;
        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.length() > 0) {
                    prefix += ".";
                }
            } else if (qname.getNamespaceURI().length() > 0) {
                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(",".getBytes());
            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)

Example 12 with XmlJavaTypeAdapter

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

the class WrapperClassGenerator method addJAXBAnnotations.

private boolean addJAXBAnnotations(FieldVisitor fv, List<Annotation> jaxbAnnos, String name) {
    AnnotationVisitor av0;
    boolean addedEl = false;
    for (Annotation ann : jaxbAnnos) {
        if (ann instanceof XmlMimeType) {
            av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlMimeType;", true);
            av0.visit("value", ((XmlMimeType) ann).value());
            av0.visitEnd();
        } else if (ann instanceof XmlJavaTypeAdapter) {
            av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/adapters/XmlJavaTypeAdapter;", true);
            generateXmlJavaTypeAdapter(av0, (XmlJavaTypeAdapter) ann);
            av0.visitEnd();
        } else if (ann instanceof XmlAttachmentRef) {
            av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlAttachmentRef;", true);
            av0.visitEnd();
        } else if (ann instanceof XmlList) {
            av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlList;", true);
            av0.visitEnd();
        } else if (ann instanceof XmlElement) {
            addedEl = true;
            XmlElement el = (XmlElement) ann;
            av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlElement;", true);
            if ("##default".equals(el.name())) {
                av0.visit("name", name);
            } else {
                av0.visit("name", el.name());
            }
            av0.visit("nillable", el.nillable());
            av0.visit("required", el.required());
            av0.visit("namespace", el.namespace());
            av0.visit("defaultValue", el.defaultValue());
            if (el.type() != XmlElement.DEFAULT.class) {
                av0.visit("type", el.type());
            }
            av0.visitEnd();
        } else if (ann instanceof XmlElementWrapper) {
            XmlElementWrapper el = (XmlElementWrapper) ann;
            av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlElementWrapper;", true);
            av0.visit("name", el.name());
            av0.visit("nillable", el.nillable());
            av0.visit("required", el.required());
            av0.visit("namespace", el.namespace());
            av0.visitEnd();
        }
    }
    return addedEl;
}
Also used : XmlMimeType(javax.xml.bind.annotation.XmlMimeType) XmlAttachmentRef(javax.xml.bind.annotation.XmlAttachmentRef) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) XmlElement(javax.xml.bind.annotation.XmlElement) Annotation(java.lang.annotation.Annotation) XmlElementWrapper(javax.xml.bind.annotation.XmlElementWrapper) XmlList(javax.xml.bind.annotation.XmlList)

Example 13 with XmlJavaTypeAdapter

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

the class WrapperClassGenerator method generateXmlJavaTypeAdapters.

private void generateXmlJavaTypeAdapters(AnnotationVisitor av, XmlJavaTypeAdapters adapters) {
    AnnotationVisitor av1 = av.visitArray("value");
    for (XmlJavaTypeAdapter adapter : adapters.value()) {
        AnnotationVisitor av2 = av1.visitAnnotation(null, "Ljavax/xml/bind/annotation/adapters/XmlJavaTypeAdapter;");
        generateXmlJavaTypeAdapter(av2, adapter);
        av2.visitEnd();
    }
    av1.visitEnd();
}
Also used : XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter)

Example 14 with XmlJavaTypeAdapter

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

the class JAXBUtils method getTypeFromAdapter.

public static Class<?> getTypeFromAdapter(XmlJavaTypeAdapter adapter, Class<?> theType, boolean boundType) {
    if (adapter != null) {
        if (adapter.type() != XmlJavaTypeAdapter.DEFAULT.class) {
            theType = adapter.type();
        } else {
            Type topAdapterType = adapter.value().getGenericSuperclass();
            Class<?> superClass = adapter.value().getSuperclass();
            while (superClass != null) {
                Class<?> nextSuperClass = superClass.getSuperclass();
                if (nextSuperClass != null && !Object.class.equals(nextSuperClass)) {
                    topAdapterType = superClass.getGenericSuperclass();
                }
                superClass = nextSuperClass;
            }
            Type[] types = InjectionUtils.getActualTypes(topAdapterType);
            if (types != null && types.length == 2) {
                int index = boundType ? 1 : 0;
                theType = InjectionUtils.getActualType(types[index]);
            }
        }
    }
    return theType;
}
Also used : Type(java.lang.reflect.Type) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter)

Example 15 with XmlJavaTypeAdapter

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

the class JAXBUtils method getAdapter.

public static XmlJavaTypeAdapter getAdapter(Class<?> objectClass, Annotation[] anns) {
    XmlJavaTypeAdapter typeAdapter = AnnotationUtils.getAnnotation(anns, XmlJavaTypeAdapter.class);
    if (typeAdapter == null) {
        typeAdapter = objectClass.getAnnotation(XmlJavaTypeAdapter.class);
        if (typeAdapter == null) {
            // lets just try the 1st interface for now
            Class<?>[] interfaces = objectClass.getInterfaces();
            typeAdapter = interfaces.length > 0 ? interfaces[0].getAnnotation(XmlJavaTypeAdapter.class) : null;
        }
    }
    return typeAdapter;
}
Also used : XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter)

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