Search in sources :

Example 1 with JAXBContext

use of jakarta.xml.bind.JAXBContext 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 JAXBContext

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

the class XmlUtilities method save.

public static File save(Object object, String fileName) {
    if (fileName == null || fileName.isEmpty()) {
        return null;
    }
    File newFile = new File(fileName);
    try (FileOutputStream fileOut = new FileOutputStream(newFile)) {
        JAXBContext jaxbContext = getContext(object.getClass());
        if (jaxbContext == null) {
            return null;
        }
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        // first: marshal to byte array
        jaxbMarshaller.marshal(object, out);
        // second: postprocess xml and then write it to the file
        XmlUtilities.saveWithCustomIndentation(new ByteArrayInputStream(out.toByteArray()), fileOut, 1);
        out.close();
        jaxbMarshaller.marshal(object, out);
    } catch (JAXBException | IOException e) {
        log.log(Level.SEVERE, e.getMessage(), e);
    }
    return newFile;
}
Also used : Marshaller(jakarta.xml.bind.Marshaller) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) JAXBException(jakarta.xml.bind.JAXBException) JAXBContext(jakarta.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 3 with JAXBContext

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

the class XmlUtilities method getContext.

public static <T> JAXBContext getContext(Class<T> cls) {
    try {
        final JAXBContext jaxbContext;
        if (jaxbContexts.containsKey(cls)) {
            jaxbContext = jaxbContexts.get(cls);
        } else {
            jaxbContext = JAXBContext.newInstance(cls);
            jaxbContexts.put(cls, jaxbContext);
        }
        return jaxbContext;
    } catch (final JAXBException e) {
        log.log(Level.SEVERE, e.getMessage(), e);
    }
    return null;
}
Also used : JAXBException(jakarta.xml.bind.JAXBException) JAXBContext(jakarta.xml.bind.JAXBContext)

Example 4 with JAXBContext

use of jakarta.xml.bind.JAXBContext 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 5 with JAXBContext

use of jakarta.xml.bind.JAXBContext in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method marshallTest.

/*
     * @testName: marshallTest
     * 
     * @assertion_ids: JAXRS:JAVADOC:815; JAXRS:JAVADOC:816;
     * 
     * @test_Strategy:
     */
@Test
public void marshallTest() throws Fault {
    Link link = RuntimeDelegate.getInstance().createLinkBuilder().uri(url).title(title).rel(rel).type(media).param(param_names[0], param_vals[0]).param(param_names[1], param_vals[1]).build();
    Model model = new Model(link);
    ByteArrayOutputStream ostream = new ByteArrayOutputStream(1000);
    JAXBContext jc = null;
    Marshaller marshaller = null;
    byte[] array = null;
    try {
        jc = JAXBContext.newInstance(Model.class);
        marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(model, ostream);
        array = ostream.toByteArray();
        String string = new String(array, Charset.defaultCharset());
        assertContains(string, "link href=", "Marshalled Link", string, "does not contain expected uri reference field");
        assertContains(string, url, "Marshalled Link", string, " does not contain expected uri reference", url);
        assertContains(string, media, "MediaType has not been marshalled in", string);
        assertContains(string, title, "Title has not been marshalled in", string);
        assertContains(string, rel, "Relation has not been marshalled in", string);
        assertContains(string, param_names[0], "parameter name", param_names[0], "has not been marshalled in", string);
        assertContains(string, param_names[1], "parameter name", param_names[1], "has not been marshalled in", string);
        assertContains(string, param_vals[0], "parameter value", param_vals[0], "has not been marshalled in", string);
        assertContains(string, param_vals[1], "parameter value", param_vals[1], "has not been marshalled in", string);
        logMsg("Marshalled Link contains expected", string);
    } catch (JAXBException e) {
        throw new Fault(e);
    }
// return array;
}
Also used : Marshaller(jakarta.xml.bind.Marshaller) JAXBException(jakarta.xml.bind.JAXBException) JAXBContext(jakarta.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Link(jakarta.ws.rs.core.Link) Test(org.junit.jupiter.api.Test)

Aggregations

JAXBContext (jakarta.xml.bind.JAXBContext)12 JAXBException (jakarta.xml.bind.JAXBException)9 Marshaller (jakarta.xml.bind.Marshaller)7 Unmarshaller (jakarta.xml.bind.Unmarshaller)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Test (org.junit.jupiter.api.Test)3 Link (jakarta.ws.rs.core.Link)2 JAXBElement (jakarta.xml.bind.JAXBElement)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 QName (javax.xml.namespace.QName)2 HttpMessageConversionException (org.springframework.http.converter.HttpMessageConversionException)2 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 GZIPInputStream (java.util.zip.GZIPInputStream)1