Search in sources :

Example 1 with WrappedValue

use of org.eclipse.persistence.internal.jaxb.WrappedValue 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 WrappedValue

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

the class JAXBUnmarshaller method unmarshal.

/**
 * Unmarshal the object based on the binding metadata associated with the
 * TypeMappingInfo.
 */
public JAXBElement unmarshal(XMLStreamReader streamReader, TypeMappingInfo type) throws JAXBException {
    try {
        Descriptor xmlDescriptor = type.getXmlDescriptor();
        if (type.getType() instanceof Class) {
            Class<?> javaClass = (Class) type.getType();
            Class<?> componentClass = javaClass.getComponentType();
            if (javaClass.isArray() && javaClass != CoreClassConstants.APBYTE && javaClass != CoreClassConstants.ABYTE && XMLConversionManager.getDefaultJavaTypes().get(componentClass) != null) {
                // Top-level array.  Descriptor will be for an EL-generated class, containing one DirectCollection mapping.
                DirectCollectionMapping mapping = (DirectCollectionMapping) xmlDescriptor.getMappings().get(0);
                XMLStreamReaderReader staxReader = new XMLStreamReaderReader(xmlUnmarshaller);
                staxReader.setErrorHandler(xmlUnmarshaller.getErrorHandler());
                PrimitiveArrayContentHandler primitiveArrayContentHandler = new PrimitiveArrayContentHandler(javaClass, componentClass, mapping.usesSingleNode());
                staxReader.setContentHandler(primitiveArrayContentHandler);
                XMLStreamReaderInputSource inputSource = new XMLStreamReaderInputSource(streamReader);
                staxReader.parse(inputSource);
                return primitiveArrayContentHandler.getJaxbElement();
            }
        }
        if (null != xmlDescriptor && null == getSchema()) {
            RootLevelXmlAdapter adapter = null;
            if (jaxbContext.getTypeMappingInfoToJavaTypeAdapters().size() > 0) {
                adapter = jaxbContext.getTypeMappingInfoToJavaTypeAdapters().get(type);
            }
            UnmarshalRecord wrapper = (UnmarshalRecord) xmlDescriptor.getObjectBuilder().createRecordFromXMLContext(xmlUnmarshaller.getXMLContext());
            org.eclipse.persistence.internal.oxm.record.UnmarshalRecord unmarshalRecord = wrapper.getUnmarshalRecord();
            XMLStreamReaderReader staxReader = new XMLStreamReaderReader(xmlUnmarshaller);
            unmarshalRecord.setUnmarshaller(xmlUnmarshaller);
            unmarshalRecord.setXMLReader(staxReader);
            staxReader.setContentHandler(unmarshalRecord);
            staxReader.parse(streamReader);
            Object value = null;
            if (unmarshalRecord.isNil()) {
                value = null;
            } else {
                value = unmarshalRecord.getCurrentObject();
            }
            if (value instanceof WrappedValue) {
                value = ((WrappedValue) value).getValue();
            }
            if (value instanceof ManyValue) {
                value = ((ManyValue) value).getItem();
            }
            if (adapter != null) {
                try {
                    value = adapter.getXmlAdapter().unmarshal(value);
                } catch (Exception ex) {
                    throw new JAXBException(XMLMarshalException.marshalException(ex));
                }
            }
            Class<?> declaredClass = null;
            if (type.getType() instanceof Class) {
                declaredClass = (Class) type.getType();
            } else {
                declaredClass = Object.class;
            }
            return new JAXBElement(new QName(unmarshalRecord.getRootElementNamespaceUri(), unmarshalRecord.getLocalName()), declaredClass, value);
        }
        if (jaxbContext.getTypeMappingInfoToGeneratedType() == null) {
            return unmarshal(streamReader, type.getType());
        }
        RootLevelXmlAdapter adapter = null;
        if (jaxbContext.getTypeMappingInfoToJavaTypeAdapters().size() > 0) {
            adapter = jaxbContext.getTypeMappingInfoToJavaTypeAdapters().get(type);
        }
        Class<?> unmarshalClass = null;
        if (jaxbContext.getTypeMappingInfoToGeneratedType().size() > 0) {
            unmarshalClass = jaxbContext.getTypeMappingInfoToGeneratedType().get(type);
        }
        if (unmarshalClass != null) {
            JAXBElement unmarshalled = unmarshal(streamReader, unmarshalClass);
            Class<?> declaredClass = null;
            if (type.getType() instanceof Class) {
                declaredClass = (Class) type.getType();
            } else {
                declaredClass = Object.class;
            }
            Object value = unmarshalled.getValue();
            if (adapter != null) {
                try {
                    value = adapter.getXmlAdapter().unmarshal(value);
                } catch (Exception ex) {
                    throw new JAXBException(XMLMarshalException.marshalException(ex));
                }
            }
            JAXBElement returnVal = new JAXBElement(unmarshalled.getName(), declaredClass, unmarshalled.getScope(), value);
            return returnVal;
        } else if (type.getType() instanceof Class) {
            if (adapter != null) {
                JAXBElement element = unmarshal(streamReader, adapter.getBoundType());
                try {
                    Object value = adapter.getXmlAdapter().unmarshal(element.getValue());
                    element.setValue(value);
                    return element;
                } catch (Exception ex) {
                    throw new JAXBException(XMLMarshalException.marshalException(ex));
                }
            }
            return unmarshal(streamReader, (Class) type.getType());
        } else if (type.getType() instanceof ParameterizedType) {
            return unmarshal(streamReader, ((ParameterizedType) type.getType()).getRawType());
        }
        return null;
    } catch (XMLMarshalException xmlMarshalException) {
        throw handleXMLMarshalException(xmlMarshalException);
    } catch (SAXException e) {
        throw new JAXBException(e);
    }
}
Also used : QName(javax.xml.namespace.QName) JAXBException(jakarta.xml.bind.JAXBException) ManyValue(org.eclipse.persistence.internal.jaxb.many.ManyValue) JAXBElement(jakarta.xml.bind.JAXBElement) WrappedValue(org.eclipse.persistence.internal.jaxb.WrappedValue) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) BeanValidationException(org.eclipse.persistence.exceptions.BeanValidationException) SAXException(org.xml.sax.SAXException) UnmarshalException(jakarta.xml.bind.UnmarshalException) PropertyException(jakarta.xml.bind.PropertyException) JAXBException(jakarta.xml.bind.JAXBException) DirectCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping) RootLevelXmlAdapter(org.eclipse.persistence.jaxb.JAXBContext.RootLevelXmlAdapter) SAXException(org.xml.sax.SAXException) ParameterizedType(java.lang.reflect.ParameterizedType) XMLStreamReaderReader(org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader) UnmarshalRecord(org.eclipse.persistence.oxm.record.UnmarshalRecord) XMLStreamReaderInputSource(org.eclipse.persistence.internal.oxm.record.XMLStreamReaderInputSource) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException)

Example 3 with WrappedValue

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

the class JAXBMarshaller method wrapEnumeration.

private Object wrapEnumeration(Object object, Class<?> enumerationClass) {
    Class<?> generatedClass = this.getClassToGeneratedClasses().get(enumerationClass.getName());
    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;
    }
    return object;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) WrappedValue(org.eclipse.persistence.internal.jaxb.WrappedValue)

Example 4 with WrappedValue

use of org.eclipse.persistence.internal.jaxb.WrappedValue 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 5 with WrappedValue

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

the class MappingsGenerator method generateWrapperClass.

public Class<?> generateWrapperClass(String className, String attributeType, boolean isList, QName theQName) {
    EclipseLinkASMClassWriter cw = new EclipseLinkASMClassWriter();
    String sig = null;
    cw.visit(Opcodes.ACC_PUBLIC, className.replace(".", "/"), sig, Type.getType(WrappedValue.class).getInternalName(), null);
    String fieldType = null;
    if (isList) {
        fieldType = "Ljava/util/List;";
    } else {
        fieldType = attributeType.replace(".", "/");
        if (!(fieldType.startsWith("["))) {
            fieldType = "L" + fieldType + ";";
        }
    }
    if (theQName == null) {
        theQName = RESERVED_QNAME;
    }
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitTypeInsn(Opcodes.NEW, "javax/xml/namespace/QName");
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn(theQName.getNamespaceURI());
    mv.visitLdcInsn(theQName.getLocalPart());
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "javax/xml/namespace/QName", "<init>", "(Ljava/lang/String;Ljava/lang/String;)V", false);
    mv.visitLdcInsn(Type.getType(fieldType));
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "org/eclipse/persistence/internal/jaxb/WrappedValue", "<init>", "(Ljavax/xml/namespace/QName;Ljava/lang/Class;Ljava/lang/Object;)V", false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(5, 1);
    mv.visitEnd();
    cw.visitEnd();
    byte[] classBytes = cw.toByteArray();
    // byte[] classBytes = new byte[]{};
    Module moxyModule = MappingsGenerator.class.getModule();
    if (moxyModule.isNamed() && !moxyModule.isExported(WrappedValue.class.getPackageName(), getJaxbClassLoader().getUnnamedModule())) {
        // our generated classes live in unnamed module, therefore we need to export our private class
        // to the unnamed module as we don't want to export internal package from eclipselink.jar
        moxyModule.addExports(WrappedValue.class.getPackageName(), getJaxbClassLoader().getUnnamedModule());
    }
    Class<?> generatedClass = getJaxbClassLoader().generateClass(className, classBytes);
    return generatedClass;
}
Also used : EclipseLinkASMClassWriter(org.eclipse.persistence.internal.libraries.asm.EclipseLinkASMClassWriter) WrappedValue(org.eclipse.persistence.internal.jaxb.WrappedValue) MethodVisitor(org.eclipse.persistence.internal.libraries.asm.MethodVisitor)

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