Search in sources :

Example 31 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement in project play-cookbook by spinscale.

the class XmlEnhancerTest method testThingEntity.

@Test
public void testThingEntity() {
    XmlRootElement xmlRootElem = Thing.class.getAnnotation(XmlRootElement.class);
    assertNotNull(xmlRootElem);
    assertEquals("thing", xmlRootElem.name());
    XmlAccessorType anno = Thing.class.getAnnotation(XmlAccessorType.class);
    assertNotNull(anno);
    assertEquals(XmlAccessType.FIELD, anno.value());
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) XmlAccessorType(javax.xml.bind.annotation.XmlAccessorType) UnitTest(play.test.UnitTest) Test(org.junit.Test)

Example 32 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement in project winery by eclipse.

the class Util method getJAXBElement.

public static <T> JAXBElement<T> getJAXBElement(Class<T> clazz, T obj) {
    String namespace = null;
    String localName = null;
    XmlRootElement xmlRootElement = clazz.getAnnotation(XmlRootElement.class);
    if (xmlRootElement != null) {
        namespace = xmlRootElement.namespace();
        localName = xmlRootElement.name();
        if ("##default".equals(namespace)) {
            XmlSchema xmlSchema = clazz.getPackage().getAnnotation(XmlSchema.class);
            if (xmlSchema != null) {
                namespace = xmlSchema.namespace();
            } else {
                // trigger default handling
                namespace = null;
            }
        }
    }
    if (namespace == null) {
        // fallback non-specified namespaces
        namespace = Namespaces.TOSCA_NAMESPACE;
    }
    if (localName == null) {
        localName = Util.getLocalName(clazz);
    }
    QName qname = new QName(namespace, localName);
    return new JAXBElement<T>(qname, clazz, obj);
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) XmlSchema(javax.xml.bind.annotation.XmlSchema) QName(javax.xml.namespace.QName) JAXBElement(javax.xml.bind.JAXBElement)

Example 33 with XmlRootElement

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

the class JAXBExtensionHelper method addExtensions.

public static void addExtensions(Bus b, ExtensionRegistry registry, Class<?> parentType, Class<?> cls, String namespace, ClassLoader loader) throws JAXBException {
    JAXBExtensionHelper helper = new JAXBExtensionHelper(b, cls, namespace);
    ExtensionClassCreator extensionClassCreator = b.getExtension(ExtensionClassCreator.class);
    boolean found = false;
    Class<?> extCls = cls;
    try {
        Class<?> objectFactory = Class.forName(PackageUtils.getPackageName(cls) + ".ObjectFactory", true, loader);
        Method[] methods = ReflectionUtil.getDeclaredMethods(objectFactory);
        for (Method method : methods) {
            if (method.getParameterTypes().length == 1 && method.getParameterTypes()[0].equals(cls)) {
                XmlElementDecl elementDecl = method.getAnnotation(XmlElementDecl.class);
                if (null != elementDecl) {
                    String name = elementDecl.name();
                    String ns = namespace != null ? namespace : elementDecl.namespace();
                    if (namespace != null) {
                        helper.setJaxbNamespace(elementDecl.namespace());
                    }
                    QName elementType = new QName(ns, name);
                    if (!ExtensibilityElement.class.isAssignableFrom(extCls)) {
                        extCls = extensionClassCreator.createExtensionClass(cls, elementType, loader);
                        helper.setExtensionClass(extCls);
                    }
                    registry.registerDeserializer(parentType, elementType, helper);
                    registry.registerSerializer(parentType, elementType, helper);
                    registry.mapExtensionTypes(parentType, elementType, extCls);
                    found = true;
                }
            }
        }
    } catch (ClassNotFoundException ex) {
    // ignore
    }
    if (!found) {
        // not in object factory or no object factory, try other annotations
        XmlRootElement elAnnot = cls.getAnnotation(XmlRootElement.class);
        if (elAnnot != null) {
            String name = elAnnot.name();
            String ns = elAnnot.namespace();
            if (StringUtils.isEmpty(ns) || "##default".equals(ns)) {
                XmlSchema schema = null;
                if (cls.getPackage() != null) {
                    schema = cls.getPackage().getAnnotation(XmlSchema.class);
                }
                if (schema != null) {
                    ns = schema.namespace();
                }
            }
            if (!StringUtils.isEmpty(ns) && !StringUtils.isEmpty(name)) {
                if (namespace != null) {
                    helper.setJaxbNamespace(ns);
                    ns = namespace;
                }
                QName elementType = new QName(ns, name);
                if (!ExtensibilityElement.class.isAssignableFrom(extCls)) {
                    extCls = extensionClassCreator.createExtensionClass(cls, elementType, loader);
                    helper.setExtensionClass(extCls);
                }
                registry.registerDeserializer(parentType, elementType, helper);
                registry.registerSerializer(parentType, elementType, helper);
                registry.mapExtensionTypes(parentType, elementType, extCls);
                found = true;
            }
        }
    }
    if (!found) {
        LOG.log(Level.WARNING, "EXTENSION_NOT_REGISTERED", new Object[] { cls.getName(), parentType.getName() });
    }
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) QName(javax.xml.namespace.QName) XmlElementDecl(javax.xml.bind.annotation.XmlElementDecl) Method(java.lang.reflect.Method) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement) XmlSchema(javax.xml.bind.annotation.XmlSchema)

Example 34 with XmlRootElement

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

the class NameInfo method getRootElementName.

private QName getRootElementName(Class<?> klass) {
    if (klass == null) {
        return null;
    }
    QName rName = null;
    XmlRootElement reAnnot = klass.getAnnotation(XmlRootElement.class);
    if (reAnnot != null) {
        rName = new QName(reAnnot.namespace(), reAnnot.name());
    }
    return rName;
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) QName(javax.xml.namespace.QName)

Example 35 with XmlRootElement

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

the class JacksonUtil method getElementName.

/**
 * Makes a best efforts guess at the name and namespace associated with a JAXB object.  Note that
 * typically only classes with an {@link XmlRootElement} annotation have an authoritative associated
 * {@code QName}
 * @param obj - a JAXB object
 * @return best guess {@code QName} associated with the {@code obj} argument
 */
private static org.dom4j.QName getElementName(Object obj) {
    if (obj == null)
        return null;
    String ns = null;
    String elementName = null;
    XmlRootElement root = obj.getClass().getAnnotation(XmlRootElement.class);
    if (root != null) {
        elementName = root.name();
        if (!root.namespace().equals(JaxbInfo.DEFAULT_MARKER))
            ns = root.namespace();
    }
    if (Strings.isNullOrEmpty(ns)) {
        // Didn't get a valid namespace from XmlRootElement - see if there is one associated with the package
        XmlSchema schem = obj.getClass().getPackage().getAnnotation(XmlSchema.class);
        if (schem != null) {
            ns = schem.namespace();
        }
    }
    if (elementName == null) {
        // Didn't get a valid element name from XmlRootElement - use the last part of the class name
        elementName = obj.getClass().getName();
        if (elementName != null) {
            int pos = elementName.lastIndexOf('.');
            if (pos > 0) {
                elementName = elementName.substring(pos + 1);
            }
        }
    }
    org.dom4j.QName qn = null;
    if (elementName != null) {
        org.dom4j.Namespace dom4jNS = org.dom4j.Namespace.get("", ns);
        qn = new org.dom4j.QName(elementName, dom4jNS);
    }
    return qn;
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) XmlSchema(javax.xml.bind.annotation.XmlSchema)

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