use of org.eclipse.persistence.internal.oxm.Root in project eclipselink by eclipse-ee4j.
the class JAXBUnmarshaller method createJAXBElementOrUnwrapIfRequired.
private Object createJAXBElementOrUnwrapIfRequired(Object value) {
if (value instanceof Root) {
JAXBElement jaxbElement = jaxbContext.createJAXBElementFromXMLRoot((Root) value, Object.class);
jaxbElement.setNil(((Root) value).isNil());
return jaxbElement;
} else if (value instanceof WrappedValue) {
return ((WrappedValue) value).getValue();
}
return value;
}
use of org.eclipse.persistence.internal.oxm.Root in project eclipselink by eclipse-ee4j.
the class JAXBMarshaller method createXMLRootFromJAXBElement.
/**
* Create an instance of XMLRoot populated from the contents of the provided
* JAXBElement. XMLRoot will be used to hold the contents of the JAXBElement
* while the marshal operation is performed by TopLink OXM. This will avoid
* adding any runtime dependencies to TopLink.
*/
private Root createXMLRootFromJAXBElement(JAXBElement elt) {
// create an XMLRoot to hand into the marshaller
Root xmlroot = new Root();
Object objectValue = elt.getValue();
xmlroot.setObject(objectValue);
QName qname = elt.getName();
xmlroot.setLocalName(qname.getLocalPart());
xmlroot.setNamespaceURI(qname.getNamespaceURI());
xmlroot.setDeclaredType(elt.getDeclaredType());
xmlroot.setNil(elt.isNil());
if (elt.getDeclaredType() == CoreClassConstants.ABYTE || elt.getDeclaredType() == CoreClassConstants.APBYTE || elt.getDeclaredType().getCanonicalName().equals("jakarta.activation.DataHandler") || elt.getDeclaredType().isEnum()) {
// need a binary data mapping so need to wrap
Class<?> generatedClass = getClassToGeneratedClasses().get(elt.getDeclaredType().getCanonicalName());
if (!elt.getDeclaredType().isEnum()) {
xmlroot.setSchemaType(Constants.BASE_64_BINARY_QNAME);
}
if (generatedClass != null && WrappedValue.class.isAssignableFrom(generatedClass)) {
ClassDescriptor desc = xmlMarshaller.getXMLContext().getSession(generatedClass).getDescriptor(generatedClass);
Object newObject = desc.getInstantiationPolicy().buildNewInstance();
((WrappedValue) newObject).setValue(objectValue);
xmlroot.setObject(newObject);
return xmlroot;
}
} else {
xmlroot.setSchemaType(org.eclipse.persistence.internal.oxm.XMLConversionManager.getDefaultJavaTypes().get(elt.getDeclaredType()));
}
if (elt instanceof WrappedValue) {
xmlroot.setObject(elt);
return xmlroot;
}
Map<QName, Class<?>> qNameToGeneratedClasses = jaxbContext.getQNameToGeneratedClasses();
if (qNameToGeneratedClasses != null) {
Class<?> theClass = qNameToGeneratedClasses.get(qname);
if (theClass != null && WrappedValue.class.isAssignableFrom(theClass)) {
ClassDescriptor desc = xmlMarshaller.getXMLContext().getSession(theClass).getDescriptor(theClass);
Object newObject = desc.getInstantiationPolicy().buildNewInstance();
((WrappedValue) newObject).setValue(objectValue);
xmlroot.setObject(newObject);
return xmlroot;
}
}
Class<?> generatedClass = null;
if (jaxbContext.getTypeMappingInfoToGeneratedType() != null) {
if (jaxbContext.getTypeToTypeMappingInfo() != null) {
if (elt.getDeclaredType() != null && elt.getDeclaredType().isArray()) {
TypeMappingInfo tmi = jaxbContext.getTypeToTypeMappingInfo().get(elt.getDeclaredType());
generatedClass = jaxbContext.getTypeMappingInfoToGeneratedType().get(tmi);
} else if (elt instanceof JAXBTypeElement) {
Type objectType = ((JAXBTypeElement) elt).getType();
TypeMappingInfo tmi = jaxbContext.getTypeToTypeMappingInfo().get(objectType);
generatedClass = jaxbContext.getTypeMappingInfoToGeneratedType().get(tmi);
}
}
} else {
if (elt.getDeclaredType() != null && elt.getDeclaredType().isArray()) {
if (jaxbContext.getArrayClassesToGeneratedClasses() != null) {
generatedClass = jaxbContext.getArrayClassesToGeneratedClasses().get(elt.getDeclaredType().getCanonicalName());
}
} else if (elt instanceof JAXBTypeElement) {
Type objectType = ((JAXBTypeElement) elt).getType();
generatedClass = jaxbContext.getCollectionClassesToGeneratedClasses().get(objectType);
}
}
if (generatedClass != null) {
ClassDescriptor desc = xmlMarshaller.getXMLContext().getSession(generatedClass).getDescriptor(generatedClass);
Object newObject = desc.getInstantiationPolicy().buildNewInstance();
((ManyValue) newObject).setItem(objectValue);
xmlroot.setObject(newObject);
}
return xmlroot;
}
use of org.eclipse.persistence.internal.oxm.Root in project eclipselink by eclipse-ee4j.
the class JAXBMarshaller method wrapObjectInXMLRoot.
private Root wrapObjectInXMLRoot(JAXBElement wrapperElement, Object value, TypeMappingInfo typeMappingInfo) {
Root xmlroot = new Root();
Object objectValue = value;
xmlroot.setObject(objectValue);
QName qname = wrapperElement.getName();
xmlroot.setLocalName(qname.getLocalPart());
xmlroot.setNamespaceURI(qname.getNamespaceURI());
xmlroot.setDeclaredType(wrapperElement.getDeclaredType());
if (typeMappingInfo != null) {
xmlroot.setSchemaType(typeMappingInfo.getSchemaType());
} else if (value != null) {
if (value.getClass() == CoreClassConstants.ABYTE || value.getClass() == CoreClassConstants.APBYTE || value.getClass().getCanonicalName().equals("jakarta.activation.DataHandler")) {
xmlroot.setSchemaType(Constants.BASE_64_BINARY_QNAME);
}
}
return xmlroot;
}
use of org.eclipse.persistence.internal.oxm.Root in project eclipselink by eclipse-ee4j.
the class JAXBElementRootConverter method convertObjectValueToDataValue.
@Override
public Object convertObjectValueToDataValue(Object objectValue, Session session, XMLMarshaller marshaller) {
if (null != nestedConverter) {
objectValue = nestedConverter.convertObjectValueToDataValue(objectValue, session, marshaller);
}
if (objectValue instanceof JAXBElement) {
ClassDescriptor desc = session.getDescriptor(objectValue);
if (desc == null || objectValue instanceof WrappedValue) {
JAXBElement element = (JAXBElement) objectValue;
Root root = new XMLRoot();
root.setLocalName(element.getName().getLocalPart());
root.setNamespaceURI(element.getName().getNamespaceURI());
root.setObject(element.getValue());
root.setDeclaredType(element.getDeclaredType());
root.setNil(element.isNil());
return root;
}
}
return objectValue;
}
use of org.eclipse.persistence.internal.oxm.Root in project eclipselink by eclipse-ee4j.
the class JAXBBinder method marshal.
@Override
public void marshal(Object obj, Object xmlNode) throws MarshalException {
if (null == obj || null == xmlNode) {
throw new IllegalArgumentException();
}
try {
if (obj instanceof JAXBElement) {
JAXBElement jaxbElem = (JAXBElement) obj;
Root xmlRoot = new Root();
xmlRoot.setObject(jaxbElem.getValue());
xmlRoot.setLocalName(jaxbElem.getName().getLocalPart());
xmlRoot.setNamespaceURI(jaxbElem.getName().getNamespaceURI());
xmlBinder.marshal(xmlRoot, (Node) xmlNode);
} else {
xmlBinder.marshal(obj, (Node) xmlNode);
}
} catch (Exception e) {
throw new MarshalException(e);
}
}
Aggregations