Search in sources :

Example 36 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement in project zm-mailbox by Zimbra.

the class JaxbInfo method gatherInfo.

private void gatherInfo() {
    XmlAccessorType accessorType;
    rootElementName = null;
    XmlAccessType accessType = null;
    XmlRootElement rootE = jaxbClass.getAnnotation(XmlRootElement.class);
    if (rootE != null) {
        rootElementName = rootE.name();
    }
    xmlType = jaxbClass.getAnnotation(XmlType.class);
    accessorType = jaxbClass.getAnnotation(XmlAccessorType.class);
    if (accessorType == null) {
        Package pkg = jaxbClass.getPackage();
        accessorType = pkg.getAnnotation(XmlAccessorType.class);
    }
    if (accessorType != null) {
        accessType = accessorType.value();
    }
    if (accessType == null) {
        // Default value for JAXB
        accessType = XmlAccessType.PUBLIC_MEMBER;
    }
    Field[] fields = jaxbClass.getDeclaredFields();
    for (Field field : fields) {
        XmlTransient xmlTransient = field.getAnnotation(XmlTransient.class);
        if (xmlTransient != null) {
            continue;
        }
        Annotation[] fAnnots = field.getAnnotations();
        if ((fAnnots == null) || (fAnnots.length == 0)) {
            boolean autoFields = (accessType.equals(XmlAccessType.PUBLIC_MEMBER) || accessType.equals(XmlAccessType.FIELD));
            if (!autoFields) {
                continue;
            }
        }
        processFieldRelatedAnnotations(fAnnots, field.getName(), field.getGenericType());
    }
    Method[] methods = jaxbClass.getDeclaredMethods();
    for (Method method : methods) {
        XmlTransient xmlTransient = method.getAnnotation(XmlTransient.class);
        if (xmlTransient != null) {
            continue;
        }
        if (!isGetterOrSetter(method)) {
            continue;
        }
        Annotation[] mAnnots = method.getAnnotations();
        if ((mAnnots == null) || (mAnnots.length == 0)) {
            boolean autoGettersSetters = (accessType.equals(XmlAccessType.PUBLIC_MEMBER) || accessType.equals(XmlAccessType.PROPERTY));
            if (!autoGettersSetters) {
                continue;
            }
        }
        processFieldRelatedAnnotations(mAnnots, guessFieldNameFromGetterOrSetter(method.getName()), method.getGenericReturnType());
    }
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) Method(java.lang.reflect.Method) XmlTransient(javax.xml.bind.annotation.XmlTransient) Annotation(java.lang.annotation.Annotation) XmlType(javax.xml.bind.annotation.XmlType) Field(java.lang.reflect.Field) XmlAccessorType(javax.xml.bind.annotation.XmlAccessorType) XmlAccessType(javax.xml.bind.annotation.XmlAccessType)

Example 37 with XmlRootElement

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

the class WadlGenerator method getJaxbQName.

private QName getJaxbQName(JAXBContextProxy jaxbProxy, Class<?> type, Map<Class<?>, QName> clsMap) {
    XmlRootElement root = type.getAnnotation(XmlRootElement.class);
    if (root != null) {
        return getJaxbQName(root.name(), root.namespace(), type, clsMap);
    }
    try {
        JAXBBeanInfo jaxbInfo = jaxbProxy == null ? null : JAXBUtils.getBeanInfo(jaxbProxy, type);
        if (jaxbInfo == null) {
            return null;
        }
        Object instance = type.newInstance();
        return getQNameFromParts(jaxbInfo.getElementLocalName(instance), jaxbInfo.getElementNamespaceURI(instance), type, clsMap);
    } catch (Exception ex) {
    // ignore
    }
    return null;
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) JAXBBeanInfo(org.apache.cxf.common.jaxb.JAXBBeanInfo) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 38 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement in project jbpm by kiegroup.

the class JaxbTaskSerializationTest method uniqueRootElementTest.

@Test
public void uniqueRootElementTest() throws Exception {
    Set<String> idSet = new HashSet<String>();
    HashMap<String, Class> idClassMap = new HashMap<String, Class>();
    for (Class<?> jaxbClass : reflections.getTypesAnnotatedWith(XmlRootElement.class)) {
        XmlRootElement rootElemAnno = jaxbClass.getAnnotation(XmlRootElement.class);
        String id = rootElemAnno.name();
        if ("##default".equals(id)) {
            continue;
        }
        String otherClass = (idClassMap.get(id) == null ? "null" : idClassMap.get(id).getName());
        assertTrue("ID '" + id + "' used in both " + jaxbClass.getName() + " and " + otherClass, idSet.add(id));
        idClassMap.put(id, jaxbClass);
        String className = jaxbClass.getSimpleName();
        if (!className.endsWith("Command")) {
            continue;
        }
        String idName = id.replace("-", "");
        assertEquals("XML root element name should match class name in " + jaxbClass.getName() + "!", className.toLowerCase(), idName.toLowerCase());
    }
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 39 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement in project wso2-synapse by wso2.

the class JsonXMLRootProvider method getXmlRootElementName.

/**
 * Calculate root element name for an <code>@XmlRootElement</code>-annotated type.
 *
 * @param type
 * @return element name
 */
protected QName getXmlRootElementName(Class<?> type) {
    XmlRootElement xmlRootElement = type.getAnnotation(XmlRootElement.class);
    if (xmlRootElement == null) {
        return null;
    }
    String localName;
    if ("##default".equals(xmlRootElement.name())) {
        localName = Character.toLowerCase(type.getSimpleName().charAt(0)) + type.getSimpleName().substring(1);
    } else {
        localName = xmlRootElement.name();
    }
    XmlSchema xmlSchema = type.getPackage().getAnnotation(XmlSchema.class);
    String namespaceURI = getNamespaceURI(xmlRootElement, xmlSchema);
    return new QName(namespaceURI, localName, getPrefix(namespaceURI, xmlSchema));
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) XmlSchema(javax.xml.bind.annotation.XmlSchema) QName(javax.xml.namespace.QName)

Aggregations

XmlRootElement (javax.xml.bind.annotation.XmlRootElement)39 XmlSchema (javax.xml.bind.annotation.XmlSchema)11 XmlType (javax.xml.bind.annotation.XmlType)9 QName (javax.xml.namespace.QName)9 LinkedHashSet (java.util.LinkedHashSet)6 TreeSet (java.util.TreeSet)6 TypeElement (javax.lang.model.element.TypeElement)6 AnnotationProcessorHelper.findTypeElement (org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement)6 Method (java.lang.reflect.Method)5 TypeMirror (javax.lang.model.type.TypeMirror)5 Elements (javax.lang.model.util.Elements)5 XmlElements (javax.xml.bind.annotation.XmlElements)5 Metadata (org.apache.camel.spi.Metadata)5 HashSet (java.util.HashSet)4 Test (org.junit.Test)4 XmlAccessorType (javax.xml.bind.annotation.XmlAccessorType)3 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)2 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)2 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)2 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)2