Search in sources :

Example 6 with WrappedValue

use of org.eclipse.persistence.internal.jaxb.WrappedValue in project eclipselink by eclipse-ee4j.

the class JAXBContext method createJAXBElementFromXMLRoot.

protected JAXBElement createJAXBElementFromXMLRoot(Root xmlRoot, Class<?> declaredType) {
    Object value = xmlRoot.getObject();
    if (value instanceof List) {
        List theList = (List) value;
        for (int i = 0; i < theList.size(); i++) {
            Object next = theList.get(i);
            if (next instanceof Root) {
                theList.set(i, createJAXBElementFromXMLRoot((Root) next, declaredType));
            }
        }
    } else if (value instanceof WrappedValue) {
        QName qname = new QName(xmlRoot.getNamespaceURI(), xmlRoot.getLocalName());
        return new JAXBElement(qname, ((WrappedValue) value).getDeclaredType(), ((WrappedValue) value).getValue());
    } else if (value instanceof JAXBElement) {
        return (JAXBElement) value;
    } else if (value instanceof ManyValue) {
        value = ((ManyValue) value).getItem();
    }
    QName qname = new QName(xmlRoot.getNamespaceURI(), xmlRoot.getLocalName());
    Map<QName, Class<?>> qNamesToDeclaredClasses = getQNamesToDeclaredClasses();
    if (qNamesToDeclaredClasses != null && qNamesToDeclaredClasses.size() > 0) {
        Class<?> declaredClass = qNamesToDeclaredClasses.get(qname);
        if (declaredClass != null) {
            return createJAXBElement(qname, declaredClass, value);
        }
    }
    Class<?> xmlRootDeclaredType = xmlRoot.getDeclaredType();
    if (xmlRootDeclaredType != null) {
        return createJAXBElement(qname, xmlRootDeclaredType, value);
    }
    return createJAXBElement(qname, declaredType, value);
}
Also used : Root(org.eclipse.persistence.internal.oxm.Root) QName(javax.xml.namespace.QName) List(java.util.List) ArrayList(java.util.ArrayList) ManyValue(org.eclipse.persistence.internal.jaxb.many.ManyValue) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) JAXBElement(jakarta.xml.bind.JAXBElement) WrappedValue(org.eclipse.persistence.internal.jaxb.WrappedValue)

Example 7 with WrappedValue

use of org.eclipse.persistence.internal.jaxb.WrappedValue in project eclipselink by eclipse-ee4j.

the class JAXBMarshaller method wrapObject.

private Object wrapObject(Object object, JAXBElement wrapperElement, TypeMappingInfo typeMappingInfo) {
    if (jaxbContext.getTypeMappingInfoToGeneratedType().size() > 0) {
        Class<?> generatedClass = jaxbContext.getTypeMappingInfoToGeneratedType().get(typeMappingInfo);
        if (generatedClass != null && object == null && wrapperElement != null) {
            return wrapObjectInXMLRoot(wrapperElement, null, typeMappingInfo);
        }
        if (generatedClass != null && WrappedValue.class.isAssignableFrom(generatedClass)) {
            ClassDescriptor desc = xmlMarshaller.getXMLContext().getSession(generatedClass).getDescriptor(generatedClass);
            Object newObject = desc.getInstantiationPolicy().buildNewInstance();
            ((WrappedValue) newObject).setValue(object);
            object = newObject;
        } else if (generatedClass != null) {
            // should be a many value
            ClassDescriptor desc = xmlMarshaller.getXMLContext().getSession(generatedClass).getDescriptor(generatedClass);
            Object newObject = desc.getInstantiationPolicy().buildNewInstance();
            ((ManyValue) newObject).setItem(object);
            object = newObject;
        }
    }
    if (null == wrapperElement) {
        Root xmlRoot = new Root();
        QName xmlTagName = typeMappingInfo.getXmlTagName();
        if (null == xmlTagName) {
            return object;
        }
        xmlRoot.setNamespaceURI(typeMappingInfo.getXmlTagName().getNamespaceURI());
        xmlRoot.setLocalName(typeMappingInfo.getXmlTagName().getLocalPart());
        xmlRoot.setObject(object);
        return xmlRoot;
    }
    return wrapObjectInXMLRoot(wrapperElement, object, typeMappingInfo);
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Root(org.eclipse.persistence.internal.oxm.Root) QName(javax.xml.namespace.QName) WrappedValue(org.eclipse.persistence.internal.jaxb.WrappedValue)

Aggregations

WrappedValue (org.eclipse.persistence.internal.jaxb.WrappedValue)7 QName (javax.xml.namespace.QName)4 Root (org.eclipse.persistence.internal.oxm.Root)4 JAXBElement (jakarta.xml.bind.JAXBElement)3 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)3 ManyValue (org.eclipse.persistence.internal.jaxb.many.ManyValue)3 JAXBException (jakarta.xml.bind.JAXBException)1 PropertyException (jakarta.xml.bind.PropertyException)1 UnmarshalException (jakarta.xml.bind.UnmarshalException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 BeanValidationException (org.eclipse.persistence.exceptions.BeanValidationException)1 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)1 EclipseLinkASMClassWriter (org.eclipse.persistence.internal.libraries.asm.EclipseLinkASMClassWriter)1 MethodVisitor (org.eclipse.persistence.internal.libraries.asm.MethodVisitor)1 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)1 DirectCollectionMapping (org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping)1 XMLStreamReaderInputSource (org.eclipse.persistence.internal.oxm.record.XMLStreamReaderInputSource)1