Search in sources :

Example 1 with DefaultValidationEventHandler

use of jakarta.xml.bind.helpers.DefaultValidationEventHandler in project jargyle by jh3nd3rs0n.

the class UsersXml method newInstanceFromXml.

public static UsersXml newInstanceFromXml(final InputStream in) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(UsersXml.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    unmarshaller.setEventHandler(new DefaultValidationEventHandler());
    return (UsersXml) unmarshaller.unmarshal(in);
}
Also used : JAXBContext(jakarta.xml.bind.JAXBContext) Unmarshaller(jakarta.xml.bind.Unmarshaller) DefaultValidationEventHandler(jakarta.xml.bind.helpers.DefaultValidationEventHandler)

Example 2 with DefaultValidationEventHandler

use of jakarta.xml.bind.helpers.DefaultValidationEventHandler in project eclipselink by eclipse-ee4j.

the class JAXBContextFactory method getXmlBindings.

/**
 * Convenience method for creating an XmlBindings object based on a given Object. The method
 * will load the eclipselink metadata model and unmarshal the Object. This assumes that the
 * Object represents the eclipselink-oxm.xml metadata file to be unmarshalled.
 *
 * @param metadata assumed to be one of:  File, InputSource, InputStream, Reader, Source
 */
private static XmlBindings getXmlBindings(Object metadata, ClassLoader classLoader, Map<String, Object> properties) {
    JAXBContext jaxbContext = CompilerHelper.getXmlBindingsModelContext();
    InputStream openedStream = null;
    try {
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML);
        unmarshaller.setProperty(UnmarshallerProperties.AUTO_DETECT_MEDIA_TYPE, true);
        unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
        unmarshaller.setEventHandler(new DefaultValidationEventHandler());
        if (metadata instanceof MetadataSource) {
            return ((MetadataSource) metadata).getXmlBindings(properties, classLoader);
        }
        JAXBElement<XmlBindings> bindingsJaxbElement = null;
        if (metadata instanceof XMLEventReader) {
            bindingsJaxbElement = unmarshaller.unmarshal((XMLEventReader) metadata, XmlBindings.class);
        } else if (metadata instanceof XMLStreamReader) {
            bindingsJaxbElement = unmarshaller.unmarshal((XMLStreamReader) metadata, XmlBindings.class);
        } else {
            Source source = null;
            if (metadata instanceof File) {
                source = new StreamSource(new FileInputStream((File) metadata));
            } else if (metadata instanceof InputSource) {
                if (((InputSource) metadata).getByteStream() != null) {
                    source = new StreamSource(((InputSource) metadata).getByteStream());
                } else if (((InputSource) metadata).getCharacterStream() != null) {
                    source = new StreamSource(((InputSource) metadata).getCharacterStream());
                }
            } else if (metadata instanceof InputStream) {
                source = new StreamSource((InputStream) metadata);
            } else if (metadata instanceof Node) {
                source = new DOMSource((Node) metadata);
            } else if (metadata instanceof Reader) {
                source = new StreamSource((Reader) metadata);
            } else if (metadata instanceof Source) {
                source = (Source) metadata;
            } else if (metadata instanceof URL) {
                openedStream = ((URL) metadata).openStream();
                source = new StreamSource(openedStream);
            } else if (metadata instanceof String) {
                StreamSource streamSource = new StreamSource((String) metadata);
                try {
                    bindingsJaxbElement = unmarshaller.unmarshal(streamSource, XmlBindings.class);
                } catch (JAXBException e) {
                    openedStream = classLoader.getResourceAsStream((String) metadata);
                    if (openedStream != null) {
                        bindingsJaxbElement = unmarshaller.unmarshal(new StreamSource(openedStream), XmlBindings.class);
                    } else {
                        throw org.eclipse.persistence.exceptions.JAXBException.couldNotUnmarshalMetadata(e);
                    }
                }
            } else {
                throw org.eclipse.persistence.exceptions.JAXBException.incorrectValueParameterTypeForOxmXmlKey();
            }
            if (bindingsJaxbElement == null) {
                if (source == null) {
                    throw org.eclipse.persistence.exceptions.JAXBException.incorrectValueParameterTypeForOxmXmlKey();
                } else {
                    bindingsJaxbElement = unmarshaller.unmarshal(source, XmlBindings.class);
                }
            }
        }
        if (bindingsJaxbElement != null) {
            return bindingsJaxbElement.getValue();
        }
        throw org.eclipse.persistence.exceptions.JAXBException.incorrectValueParameterTypeForOxmXmlKey();
    } catch (jakarta.xml.bind.JAXBException ex) {
        throw org.eclipse.persistence.exceptions.JAXBException.couldNotUnmarshalMetadata(ex);
    } catch (IOException ioException) {
        throw org.eclipse.persistence.exceptions.JAXBException.couldNotUnmarshalMetadata(ioException);
    } finally {
        if (openedStream != null) {
            try {
                openedStream.close();
            } catch (IOException e) {
                throw org.eclipse.persistence.exceptions.JAXBException.couldNotUnmarshalMetadata(e);
            }
        }
    }
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Node(org.w3c.dom.Node) JAXBException(jakarta.xml.bind.JAXBException) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLEventReader(javax.xml.stream.XMLEventReader) Reader(java.io.Reader) IOException(java.io.IOException) MetadataSource(org.eclipse.persistence.jaxb.metadata.MetadataSource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) MetadataSource(org.eclipse.persistence.jaxb.metadata.MetadataSource) FileInputStream(java.io.FileInputStream) URL(java.net.URL) JAXBException(jakarta.xml.bind.JAXBException) XMLEventReader(javax.xml.stream.XMLEventReader) Unmarshaller(jakarta.xml.bind.Unmarshaller) DefaultValidationEventHandler(jakarta.xml.bind.helpers.DefaultValidationEventHandler) File(java.io.File)

Example 3 with DefaultValidationEventHandler

use of jakarta.xml.bind.helpers.DefaultValidationEventHandler in project jargyle by jh3nd3rs0n.

the class ConfigurationXml method newInstanceFromXml.

public static ConfigurationXml newInstanceFromXml(final InputStream in) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(ConfigurationXml.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    unmarshaller.setEventHandler(new DefaultValidationEventHandler());
    return (ConfigurationXml) unmarshaller.unmarshal(in);
}
Also used : JAXBContext(jakarta.xml.bind.JAXBContext) Unmarshaller(jakarta.xml.bind.Unmarshaller) DefaultValidationEventHandler(jakarta.xml.bind.helpers.DefaultValidationEventHandler)

Aggregations

Unmarshaller (jakarta.xml.bind.Unmarshaller)3 DefaultValidationEventHandler (jakarta.xml.bind.helpers.DefaultValidationEventHandler)3 JAXBContext (jakarta.xml.bind.JAXBContext)2 JAXBException (jakarta.xml.bind.JAXBException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 URL (java.net.URL)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 Source (javax.xml.transform.Source)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamSource (javax.xml.transform.stream.StreamSource)1 MetadataSource (org.eclipse.persistence.jaxb.metadata.MetadataSource)1 XmlBindings (org.eclipse.persistence.jaxb.xmlmodel.XmlBindings)1 Node (org.w3c.dom.Node)1 InputSource (org.xml.sax.InputSource)1