// element of the object being marshalled to - need to create a
// JAXBElement from the returned XMLRoot object
if (obj instanceof XMLRoot) {
XMLRoot xmlRoot = ((XMLRoot)obj);
QName qname = new QName(xmlRoot.getNamespaceURI(), xmlRoot.getLocalName());
return new JAXBElement(qname, xmlRoot.getObject().getClass(), xmlRoot.getObject());
}
// at this point, the default root element of the object being marshalled
// to == the root element - here we need to create a JAXBElement
// instance using information from the returned object
org.eclipse.persistence.sessions.Session sess = xmlUnmarshaller.getXMLContext().getSession(obj);
XMLDescriptor desc = (XMLDescriptor) sess.getClassDescriptor(obj);
// here we are assuming that if we've gotten this far, there
// must be a default root element set on the descriptor. if
// this is incorrect, we need to check for null and throw an
// exception
String rootName = desc.getDefaultRootElement();
if (rootName == null) {
// TODO: we should probably throw an exception at this point
return new JAXBElement(new QName(""), obj.getClass(), obj);
}
String rootNamespaceUri = null;
int idx = rootName.indexOf(":");
if (idx != -1) {
rootNamespaceUri = desc.getNamespaceResolver().resolveNamespacePrefix(rootName.substring(0, idx));
rootName = rootName.substring(idx + 1);
}
QName qname;
if (rootNamespaceUri == null) {
qname = new QName(rootName);
} else {
qname = new QName(rootNamespaceUri, rootName);
}
return new JAXBElement(qname, obj.getClass(), obj);
}