Search in sources :

Example 11 with XmlSchema

use of javax.xml.bind.annotation.XmlSchema 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

XmlSchema (javax.xml.bind.annotation.XmlSchema)11 XmlType (javax.xml.bind.annotation.XmlType)6 XmlRootElement (javax.xml.bind.annotation.XmlRootElement)5 QName (javax.xml.namespace.QName)5 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Source (javax.xml.transform.Source)1 SAXSource (javax.xml.transform.sax.SAXSource)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Schema (javax.xml.validation.Schema)1 SchemaFactory (javax.xml.validation.SchemaFactory)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1