Search in sources :

Example 1 with RootLevelXmlAdapter

use of org.eclipse.persistence.jaxb.JAXBContext.RootLevelXmlAdapter 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(Source source, TypeMappingInfo type) throws JAXBException {
    try {
        Class<?> unmarshalClass = jaxbContext.getTypeMappingInfoToGeneratedType().get(type);
        RootLevelXmlAdapter adapter = jaxbContext.getTypeMappingInfoToJavaTypeAdapters().get(type);
        if (unmarshalClass != null) {
            JAXBElement unmarshalled = unmarshal(source, 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(source, 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(source, (Class) type.getType());
        } else if (type.getType() instanceof ParameterizedType) {
            return unmarshal(source, ((ParameterizedType) type.getType()).getRawType());
        }
        return null;
    } catch (XMLMarshalException xmlMarshalException) {
        throw handleXMLMarshalException(xmlMarshalException);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) JAXBException(jakarta.xml.bind.JAXBException) JAXBElement(jakarta.xml.bind.JAXBElement) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) 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) RootLevelXmlAdapter(org.eclipse.persistence.jaxb.JAXBContext.RootLevelXmlAdapter)

Example 2 with RootLevelXmlAdapter

use of org.eclipse.persistence.jaxb.JAXBContext.RootLevelXmlAdapter 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(XMLEventReader eventReader, TypeMappingInfo type) throws JAXBException {
    try {
        if (jaxbContext.getTypeMappingInfoToGeneratedType() == null) {
            return unmarshal(eventReader, type.getType());
        }
        Class<?> unmarshalClass = jaxbContext.getTypeMappingInfoToGeneratedType().get(type);
        RootLevelXmlAdapter adapter = jaxbContext.getTypeMappingInfoToJavaTypeAdapters().get(type);
        if (unmarshalClass != null) {
            JAXBElement unmarshalled = unmarshal(eventReader, 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(eventReader, 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(eventReader, (Class) type.getType());
        } else if (type.getType() instanceof ParameterizedType) {
            return unmarshal(eventReader, ((ParameterizedType) type.getType()).getRawType());
        }
        return null;
    } catch (XMLMarshalException xmlMarshalException) {
        throw handleXMLMarshalException(xmlMarshalException);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) JAXBException(jakarta.xml.bind.JAXBException) JAXBElement(jakarta.xml.bind.JAXBElement) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) 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) RootLevelXmlAdapter(org.eclipse.persistence.jaxb.JAXBContext.RootLevelXmlAdapter)

Example 3 with RootLevelXmlAdapter

use of org.eclipse.persistence.jaxb.JAXBContext.RootLevelXmlAdapter 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 4 with RootLevelXmlAdapter

use of org.eclipse.persistence.jaxb.JAXBContext.RootLevelXmlAdapter in project eclipselink by eclipse-ee4j.

the class JAXBMarshaller method marshal.

public void marshal(Object object, Result result, TypeMappingInfo type) throws JAXBException {
    if (jaxbContext.getTypeMappingInfoToGeneratedType() == null) {
        marshal(object, result);
    } else {
        JAXBElement element = null;
        Object value = object;
        if (object instanceof JAXBElement) {
            // use the JAXBElement's properties to populate an XMLRoot
            element = (JAXBElement) object;
            value = element.getValue();
        }
        if (jaxbContext.getTypeMappingInfoToJavaTypeAdapters().size() > 0) {
            RootLevelXmlAdapter adapter = jaxbContext.getTypeMappingInfoToJavaTypeAdapters().get(type);
            if (adapter != null) {
                try {
                    value = adapter.getXmlAdapter().marshal(value);
                } catch (Exception ex) {
                    throw new JAXBException(XMLMarshalException.marshalException(ex));
                }
            }
        }
        value = wrapObject(value, element, type);
        marshal(value, result);
    }
}
Also used : JAXBException(jakarta.xml.bind.JAXBException) JAXBElement(jakarta.xml.bind.JAXBElement) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) BeanValidationException(org.eclipse.persistence.exceptions.BeanValidationException) PropertyException(jakarta.xml.bind.PropertyException) MarshalException(jakarta.xml.bind.MarshalException) JAXBException(jakarta.xml.bind.JAXBException) RootLevelXmlAdapter(org.eclipse.persistence.jaxb.JAXBContext.RootLevelXmlAdapter)

Example 5 with RootLevelXmlAdapter

use of org.eclipse.persistence.jaxb.JAXBContext.RootLevelXmlAdapter in project eclipselink by eclipse-ee4j.

the class JAXBMarshaller method marshal.

public void marshal(Object object, XMLEventWriter eventWriter, TypeMappingInfo type) throws JAXBException {
    if (jaxbContext.getTypeMappingInfoToGeneratedType() == null) {
        marshal(object, eventWriter);
    } else {
        JAXBElement element = null;
        Object value = object;
        if (object instanceof JAXBElement) {
            // use the JAXBElement's properties to populate an XMLRoot
            element = (JAXBElement) object;
            value = element.getValue();
        }
        RootLevelXmlAdapter adapter = jaxbContext.getTypeMappingInfoToJavaTypeAdapters().get(type);
        if (adapter != null) {
            try {
                value = adapter.getXmlAdapter().marshal(value);
            } catch (Exception ex) {
                throw new JAXBException(XMLMarshalException.marshalException(ex));
            }
        }
        value = wrapObject(value, element, type);
        marshal(value, eventWriter);
    }
}
Also used : JAXBException(jakarta.xml.bind.JAXBException) JAXBElement(jakarta.xml.bind.JAXBElement) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) BeanValidationException(org.eclipse.persistence.exceptions.BeanValidationException) PropertyException(jakarta.xml.bind.PropertyException) MarshalException(jakarta.xml.bind.MarshalException) JAXBException(jakarta.xml.bind.JAXBException) RootLevelXmlAdapter(org.eclipse.persistence.jaxb.JAXBContext.RootLevelXmlAdapter)

Aggregations

JAXBElement (jakarta.xml.bind.JAXBElement)7 JAXBException (jakarta.xml.bind.JAXBException)7 PropertyException (jakarta.xml.bind.PropertyException)7 BeanValidationException (org.eclipse.persistence.exceptions.BeanValidationException)7 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)7 RootLevelXmlAdapter (org.eclipse.persistence.jaxb.JAXBContext.RootLevelXmlAdapter)7 MarshalException (jakarta.xml.bind.MarshalException)4 UnmarshalException (jakarta.xml.bind.UnmarshalException)3 ParameterizedType (java.lang.reflect.ParameterizedType)3 SAXException (org.xml.sax.SAXException)3 QName (javax.xml.namespace.QName)1 WrappedValue (org.eclipse.persistence.internal.jaxb.WrappedValue)1 ManyValue (org.eclipse.persistence.internal.jaxb.many.ManyValue)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 XMLStreamReaderReader (org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader)1 UnmarshalRecord (org.eclipse.persistence.oxm.record.UnmarshalRecord)1