Search in sources :

Example 1 with XMLMarshalException

use of org.eclipse.persistence.exceptions.XMLMarshalException 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 XMLMarshalException

use of org.eclipse.persistence.exceptions.XMLMarshalException in project eclipselink by eclipse-ee4j.

the class JAXBUnmarshaller method unmarshal.

@Override
public Object unmarshal(InputStream inputStream) throws JAXBException {
    try {
        if (xmlUnmarshaller.isAutoDetectMediaType() || xmlUnmarshaller.getMediaType() == MediaType.APPLICATION_JSON || null == jaxbContext.getXMLInputFactory() || XMLUnmarshaller.NONVALIDATING != xmlUnmarshaller.getValidationMode()) {
            // xml bindings + object inside inputStream
            return validateAndTransformIfRequired(xmlUnmarshaller.unmarshal(inputStream));
        } else {
            if (null == inputStream) {
                throw XMLMarshalException.nullArgumentException();
            }
            XMLStreamReader xmlStreamReader;
            xmlStreamReader = jaxbContext.getXMLInputFactory().createXMLStreamReader(inputStream);
            Object value = unmarshal(xmlStreamReader);
            xmlStreamReader.close();
            return value;
        }
    } catch (JAXBException jaxbException) {
        throw jaxbException;
    } catch (BeanValidationException bve) {
        throw new UnmarshalException(bve.getMessage(), String.valueOf(bve.getErrorCode()), bve);
    } catch (XMLMarshalException xmlMarshalException) {
        throw handleXMLMarshalException(xmlMarshalException);
    } catch (Exception exception) {
        throw new UnmarshalException(exception);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) UnmarshalException(jakarta.xml.bind.UnmarshalException) JAXBException(jakarta.xml.bind.JAXBException) BeanValidationException(org.eclipse.persistence.exceptions.BeanValidationException) 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)

Example 3 with XMLMarshalException

use of org.eclipse.persistence.exceptions.XMLMarshalException in project eclipselink by eclipse-ee4j.

the class JAXBUnmarshaller method unmarshal.

@Override
public JAXBElement unmarshal(XMLStreamReader streamReader, Class javaClass) throws JAXBException {
    if (null == streamReader || null == javaClass) {
        throw new IllegalArgumentException();
    }
    try {
        XMLStreamReaderReader staxReader = new XMLStreamReaderReader(xmlUnmarshaller);
        XMLStreamReaderInputSource inputSource = new XMLStreamReaderInputSource(streamReader);
        if (XMLConversionManager.getDefaultJavaTypes().get(javaClass) != null || CoreClassConstants.XML_GREGORIAN_CALENDAR.isAssignableFrom(javaClass) || CoreClassConstants.DURATION.isAssignableFrom(javaClass)) {
            PrimitiveContentHandler primitiveContentHandler = new PrimitiveContentHandler(javaClass);
            staxReader.setContentHandler(primitiveContentHandler);
            staxReader.parse(inputSource);
            return primitiveContentHandler.getJaxbElement();
        }
        Class<?> classToUnmarshalTo = getClassToUnmarshalTo(javaClass);
        // xmlbindings + object (xmlelement) + "nomappings.SomeClass" + "jaxb.stax.EndEventRoot"
        JAXBElement unmarshalled = validateAndBuildJAXBElement(xmlUnmarshaller.unmarshal(staxReader, inputSource, classToUnmarshalTo), javaClass);
        if (classToUnmarshalTo != javaClass) {
            JAXBElement returnVal = new JAXBElement(unmarshalled.getName(), javaClass, unmarshalled.getScope(), unmarshalled.getValue());
            return returnVal;
        }
        return unmarshalled;
    } catch (XMLMarshalException xmlMarshalException) {
        // Exception [EclipseLink-25004] cvc-maxInclusive-valid: Value '1234567' is not facet-valid with respect to maxInclusive '999999' for type 'id-type'.
        throw handleXMLMarshalException(xmlMarshalException);
    } catch (Exception e) {
        throw new JAXBException(e);
    }
}
Also used : XMLStreamReaderReader(org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader) JAXBException(jakarta.xml.bind.JAXBException) XMLStreamReaderInputSource(org.eclipse.persistence.internal.oxm.record.XMLStreamReaderInputSource) 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)

Example 4 with XMLMarshalException

use of org.eclipse.persistence.exceptions.XMLMarshalException in project eclipselink by eclipse-ee4j.

the class JAXBUnmarshaller method unmarshal.

public JAXBElement unmarshal(XMLStreamReader streamReader, Type type) throws JAXBException {
    if (null == streamReader || null == type) {
        throw new IllegalArgumentException();
    }
    try {
        if (jaxbContext.getTypeToTypeMappingInfo() != null) {
            TypeMappingInfo tmi = jaxbContext.getTypeToTypeMappingInfo().get(type);
            if (tmi != null) {
                return unmarshal(streamReader, tmi);
            }
        }
        Class<?> unmarshalClass = jaxbContext.getCollectionClassesToGeneratedClasses().get(type);
        if (unmarshalClass != null) {
            JAXBElement unmarshalled = unmarshal(streamReader, unmarshalClass);
            Class<?> declaredClass = null;
            if (type instanceof Class) {
                declaredClass = (Class) type;
            } else {
                declaredClass = Object.class;
            }
            JAXBElement returnVal = new JAXBElement(unmarshalled.getName(), declaredClass, unmarshalled.getScope(), unmarshalled.getValue());
            return returnVal;
        } else if (type instanceof Class) {
            return unmarshal(streamReader, (Class) type);
        }
        return null;
    } catch (XMLMarshalException xmlMarshalException) {
        throw handleXMLMarshalException(xmlMarshalException);
    }
}
Also used : JAXBElement(jakarta.xml.bind.JAXBElement) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException)

Example 5 with XMLMarshalException

use of org.eclipse.persistence.exceptions.XMLMarshalException 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)

Aggregations

XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)114 InputStream (java.io.InputStream)24 StringWriter (java.io.StringWriter)16 SAXException (org.xml.sax.SAXException)15 Document (org.w3c.dom.Document)14 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)12 Node (org.w3c.dom.Node)12 JAXBElement (jakarta.xml.bind.JAXBElement)9 JAXBException (jakarta.xml.bind.JAXBException)9 QName (javax.xml.namespace.QName)9 CoreAbstractSession (org.eclipse.persistence.internal.core.sessions.CoreAbstractSession)9 InputSource (org.xml.sax.InputSource)9 Element (org.w3c.dom.Element)8 UnmarshalException (jakarta.xml.bind.UnmarshalException)7 URL (java.net.URL)7 BeanValidationException (org.eclipse.persistence.exceptions.BeanValidationException)7 PropertyException (jakarta.xml.bind.PropertyException)6 IOException (java.io.IOException)6 PrintWriter (java.io.PrintWriter)6 XMLRoot (org.eclipse.persistence.oxm.XMLRoot)6