Search in sources :

Example 1 with XMLStreamReaderInputSource

use of org.eclipse.persistence.internal.oxm.record.XMLStreamReaderInputSource in project metro-jax-ws by eclipse-ee4j.

the class JAXBBond method unmarshal.

// This is used in RPC
@Override
public T unmarshal(XMLStreamReader in, AttachmentUnmarshaller au) throws JAXBException {
    JAXBUnmarshaller unmarshaller = null;
    try {
        QName tagName = null;
        if (in.getEventType() == XMLStreamConstants.START_ELEMENT) {
            tagName = in.getName();
        }
        unmarshaller = parent.upool.allocate();
        unmarshaller.setAttachmentUnmarshaller(au);
        Object o;
        if (in instanceof XMLStreamReaderEx) {
            CustomXMLStreamReaderReader cr = new CustomXMLStreamReaderReader();
            XMLStreamReaderInputSource is = new XMLStreamReaderInputSource(in);
            SAXSource saxSource = new SAXSource(cr, is);
            o = ((mappingInfo != null) ? unmarshaller.unmarshal(saxSource, mappingInfo) : unmarshaller.unmarshal(saxSource));
        } else {
            o = ((mappingInfo != null) ? unmarshaller.unmarshal(in, mappingInfo) : unmarshaller.unmarshal(in));
        }
        if (o instanceof JAXBElement) {
            o = ((JAXBElement) o).getValue();
        }
        // Workaround for Eclipselink JAXB not consuming END_ELEMENT.
        try {
            if (in.getEventType() == XMLStreamConstants.END_ELEMENT && in.getName().equals(tagName)) {
                in.next();
            }
        } catch (XMLStreamException e) {
            throw new WebServiceException(e);
        }
        return (T) o;
    } finally {
        if (unmarshaller != null) {
            unmarshaller.setAttachmentUnmarshaller(null);
            parent.upool.replace(unmarshaller);
        }
    }
}
Also used : SAXSource(javax.xml.transform.sax.SAXSource) XMLStreamException(javax.xml.stream.XMLStreamException) WebServiceException(jakarta.xml.ws.WebServiceException) XMLStreamReaderEx(org.jvnet.staxex.XMLStreamReaderEx) QName(javax.xml.namespace.QName) JAXBUnmarshaller(org.eclipse.persistence.jaxb.JAXBUnmarshaller) XMLStreamReaderInputSource(org.eclipse.persistence.internal.oxm.record.XMLStreamReaderInputSource) JAXBElement(jakarta.xml.bind.JAXBElement)

Example 2 with XMLStreamReaderInputSource

use of org.eclipse.persistence.internal.oxm.record.XMLStreamReaderInputSource 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 3 with XMLStreamReaderInputSource

use of org.eclipse.persistence.internal.oxm.record.XMLStreamReaderInputSource in project eclipselink by eclipse-ee4j.

the class JAXBUnmarshaller method unmarshal.

@Override
public Object unmarshal(XMLStreamReader streamReader) throws JAXBException {
    if (null == streamReader) {
        throw new IllegalArgumentException();
    }
    try {
        XMLStreamReaderReader staxReader = new XMLStreamReaderReader(xmlUnmarshaller);
        XMLStreamReaderInputSource inputSource = new XMLStreamReaderInputSource(streamReader);
        Object value = xmlUnmarshaller.unmarshal(staxReader, inputSource);
        // xml bindings + object
        return validateAndTransformIfRequired(value);
    } catch (XMLMarshalException xmlMarshalException) {
        throw handleXMLMarshalException(xmlMarshalException);
    } catch (BeanValidationException bve) {
        throw new UnmarshalException(bve.getMessage(), String.valueOf(bve.getErrorCode()), bve);
    }
}
Also used : XMLStreamReaderReader(org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader) UnmarshalException(jakarta.xml.bind.UnmarshalException) BeanValidationException(org.eclipse.persistence.exceptions.BeanValidationException) XMLStreamReaderInputSource(org.eclipse.persistence.internal.oxm.record.XMLStreamReaderInputSource) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException)

Example 4 with XMLStreamReaderInputSource

use of org.eclipse.persistence.internal.oxm.record.XMLStreamReaderInputSource 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 5 with XMLStreamReaderInputSource

use of org.eclipse.persistence.internal.oxm.record.XMLStreamReaderInputSource in project eclipselink by eclipse-ee4j.

the class JAXBTestCases method testXMLToObjectFromXMLStreamReaderEx.

public void testXMLToObjectFromXMLStreamReaderEx() throws Exception {
    if (null != XML_INPUT_FACTORY && isUnmarshalTest()) {
        InputStream instream = ClassLoader.getSystemResourceAsStream(resourceName);
        XMLStreamReader xmlStreamReader = XML_INPUT_FACTORY.createXMLStreamReader(instream);
        ExtendedXMLStreamReaderReader xmlStreamReaderReaderEx = new ExtendedXMLStreamReaderReader();
        xmlStreamReaderReaderEx.setErrorHandler(((JAXBUnmarshaller) jaxbUnmarshaller).getXMLUnmarshaller().getErrorHandler());
        XMLStreamReaderInputSource xmlStreamReaderInputSource = new XMLStreamReaderInputSource(xmlStreamReader);
        SAXSource saxSource = new SAXSource(xmlStreamReaderReaderEx, xmlStreamReaderInputSource);
        jaxbUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, getXMLUnmarshalMediaType());
        Object testObject = null;
        if (getUnmarshalClass() != null) {
            testObject = jaxbUnmarshaller.unmarshal(saxSource, getUnmarshalClass());
        } else {
            testObject = jaxbUnmarshaller.unmarshal(saxSource);
        }
        instream.close();
        xmlToObjectTest(testObject);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) SAXSource(javax.xml.transform.sax.SAXSource) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XMLStreamReaderInputSource(org.eclipse.persistence.internal.oxm.record.XMLStreamReaderInputSource)

Aggregations

XMLStreamReaderInputSource (org.eclipse.persistence.internal.oxm.record.XMLStreamReaderInputSource)19 InputStream (java.io.InputStream)15 XMLStreamReader (javax.xml.stream.XMLStreamReader)15 XMLStreamReaderReader (org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader)14 SAXSource (javax.xml.transform.sax.SAXSource)5 JAXBElement (jakarta.xml.bind.JAXBElement)3 UnmarshalException (jakarta.xml.bind.UnmarshalException)3 BeanValidationException (org.eclipse.persistence.exceptions.BeanValidationException)3 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)3 XMLContext (org.eclipse.persistence.oxm.XMLContext)3 XMLUnmarshaller (org.eclipse.persistence.oxm.XMLUnmarshaller)3 JAXBException (jakarta.xml.bind.JAXBException)2 PropertyException (jakarta.xml.bind.PropertyException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 QName (javax.xml.namespace.QName)2 ConversionException (org.eclipse.persistence.exceptions.ConversionException)2 SAXException (org.xml.sax.SAXException)2 JAXBContext (jakarta.xml.bind.JAXBContext)1 Unmarshaller (jakarta.xml.bind.Unmarshaller)1 WebServiceException (jakarta.xml.ws.WebServiceException)1