Search in sources :

Example 1 with Root

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;
}
Also used : Root(org.eclipse.persistence.internal.oxm.Root) JAXBElement(jakarta.xml.bind.JAXBElement) WrappedValue(org.eclipse.persistence.internal.jaxb.WrappedValue)

Example 2 with Root

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;
}
Also used : Type(java.lang.reflect.Type) MediaType(org.eclipse.persistence.oxm.MediaType) Root(org.eclipse.persistence.internal.oxm.Root) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) QName(javax.xml.namespace.QName) ManyValue(org.eclipse.persistence.internal.jaxb.many.ManyValue) WrappedValue(org.eclipse.persistence.internal.jaxb.WrappedValue)

Example 3 with Root

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;
}
Also used : Root(org.eclipse.persistence.internal.oxm.Root) QName(javax.xml.namespace.QName)

Example 4 with Root

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;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Root(org.eclipse.persistence.internal.oxm.Root) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) JAXBElement(jakarta.xml.bind.JAXBElement)

Example 5 with Root

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);
    }
}
Also used : MarshalException(jakarta.xml.bind.MarshalException) Root(org.eclipse.persistence.internal.oxm.Root) JAXBElement(jakarta.xml.bind.JAXBElement) UnmarshalException(jakarta.xml.bind.UnmarshalException) PropertyException(jakarta.xml.bind.PropertyException) MarshalException(jakarta.xml.bind.MarshalException) JAXBException(jakarta.xml.bind.JAXBException)

Aggregations

Root (org.eclipse.persistence.internal.oxm.Root)22 QName (javax.xml.namespace.QName)6 JAXBElement (jakarta.xml.bind.JAXBElement)5 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)5 CoreAbstractSession (org.eclipse.persistence.internal.core.sessions.CoreAbstractSession)4 WrappedValue (org.eclipse.persistence.internal.jaxb.WrappedValue)4 Node (org.w3c.dom.Node)4 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)3 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)3 XMLObjectBuilder (org.eclipse.persistence.internal.oxm.XMLObjectBuilder)3 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)3 XMLPlatformException (org.eclipse.persistence.platform.xml.XMLPlatformException)3 Document (org.w3c.dom.Document)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ManyValue (org.eclipse.persistence.internal.jaxb.many.ManyValue)2 XMLRoot (org.eclipse.persistence.oxm.XMLRoot)2 DOMRecord (org.eclipse.persistence.oxm.record.DOMRecord)2 MarshalRecord (org.eclipse.persistence.oxm.record.MarshalRecord)2 Element (org.w3c.dom.Element)2