Search in sources :

Example 1 with Unmarshaller

use of jakarta.xml.bind.Unmarshaller in project litiengine by gurkenlabs.

the class ResourceBundle method getResourceBundle.

private static ResourceBundle getResourceBundle(URL file) throws JAXBException, IOException {
    final JAXBContext jaxbContext = XmlUtilities.getContext(ResourceBundle.class);
    final Unmarshaller um = jaxbContext.createUnmarshaller();
    try (InputStream inputStream = Resources.get(file)) {
        // try to get compressed game file
        final GZIPInputStream zipStream = new GZIPInputStream(inputStream);
        return (ResourceBundle) um.unmarshal(zipStream);
    } catch (final ZipException e) {
        // if it fails to load the compressed file, get it from plain XML
        return XmlUtilities.read(ResourceBundle.class, file);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JAXBContext(jakarta.xml.bind.JAXBContext) ZipException(java.util.zip.ZipException) Unmarshaller(jakarta.xml.bind.Unmarshaller)

Example 2 with Unmarshaller

use of jakarta.xml.bind.Unmarshaller in project litiengine by gurkenlabs.

the class XmlUtilities method read.

public static <T> T read(Class<T> cls, URL path) throws JAXBException {
    final JAXBContext jaxbContext = getContext(cls);
    if (jaxbContext == null) {
        return null;
    }
    final Unmarshaller um = jaxbContext.createUnmarshaller();
    um.setAdapter(new URLAdapter(path));
    return cls.cast(um.unmarshal(path));
}
Also used : JAXBContext(jakarta.xml.bind.JAXBContext) Unmarshaller(jakarta.xml.bind.Unmarshaller)

Example 3 with Unmarshaller

use of jakarta.xml.bind.Unmarshaller in project spring-framework by spring-projects.

the class AbstractJaxb2HttpMessageConverter method createUnmarshaller.

/**
 * Create a new {@link Unmarshaller} for the given class.
 * @param clazz the class to create the unmarshaller for
 * @return the {@code Unmarshaller}
 * @throws HttpMessageConversionException in case of JAXB errors
 */
protected final Unmarshaller createUnmarshaller(Class<?> clazz) {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        customizeUnmarshaller(unmarshaller);
        return unmarshaller;
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not create Unmarshaller for class [" + clazz + "]: " + ex.getMessage(), ex);
    }
}
Also used : JAXBException(jakarta.xml.bind.JAXBException) HttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException) JAXBContext(jakarta.xml.bind.JAXBContext) Unmarshaller(jakarta.xml.bind.Unmarshaller)

Example 4 with Unmarshaller

use of jakarta.xml.bind.Unmarshaller in project spring-framework by spring-projects.

the class Jaxb2XmlDecoder method unmarshal.

private Object unmarshal(List<XMLEvent> events, Class<?> outputClass) {
    try {
        Unmarshaller unmarshaller = initUnmarshaller(outputClass);
        XMLEventReader eventReader = StaxUtils.createXMLEventReader(events);
        if (outputClass.isAnnotationPresent(XmlRootElement.class)) {
            return unmarshaller.unmarshal(eventReader);
        } else {
            JAXBElement<?> jaxbElement = unmarshaller.unmarshal(eventReader, outputClass);
            return jaxbElement.getValue();
        }
    } catch (UnmarshalException ex) {
        throw new DecodingException("Could not unmarshal XML to " + outputClass, ex);
    } catch (JAXBException ex) {
        throw new CodecException("Invalid JAXB configuration", ex);
    }
}
Also used : UnmarshalException(jakarta.xml.bind.UnmarshalException) JAXBException(jakarta.xml.bind.JAXBException) XMLEventReader(javax.xml.stream.XMLEventReader) DecodingException(org.springframework.core.codec.DecodingException) CodecException(org.springframework.core.codec.CodecException) Unmarshaller(jakarta.xml.bind.Unmarshaller)

Aggregations

Unmarshaller (jakarta.xml.bind.Unmarshaller)4 JAXBContext (jakarta.xml.bind.JAXBContext)3 JAXBException (jakarta.xml.bind.JAXBException)2 UnmarshalException (jakarta.xml.bind.UnmarshalException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ZipException (java.util.zip.ZipException)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 CodecException (org.springframework.core.codec.CodecException)1 DecodingException (org.springframework.core.codec.DecodingException)1 HttpMessageConversionException (org.springframework.http.converter.HttpMessageConversionException)1