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