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;
}
Aggregations