Search in sources :

Example 6 with JAXBContext

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

the class JAXRSClientIT method getJaxbToken.

private static String getJaxbToken() {
    JAXBElement<String> element = new JAXBElement<String>(new QName("content"), String.class, plaincontent);
    try {
        JAXBContext context = JAXBContext.newInstance(String.class);
        java.io.StringWriter writer = new java.io.StringWriter();
        context.createMarshaller().marshal(element, writer);
        return writer.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}
Also used : QName(javax.xml.namespace.QName) JAXBException(jakarta.xml.bind.JAXBException) JAXBContext(jakarta.xml.bind.JAXBContext) JAXBElement(jakarta.xml.bind.JAXBElement)

Example 7 with JAXBContext

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

the class AbstractJaxb2HttpMessageConverter method createMarshaller.

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

Example 8 with JAXBContext

use of jakarta.xml.bind.JAXBContext in project xades4j by luisgoncalves.

the class BaseJAXBMarshaller method doJAXBMarshalling.

private void doJAXBMarshalling(Node qualifyingPropsNode, TXml xmlProps) throws MarshalException {
    try {
        // Create the JAXB marshaller.
        JAXBContext jaxbContext = jaxbContexts.get(xmlProps.getClass());
        Marshaller marshaller = jaxbContext.createMarshaller();
        // Create the root JAXBElement.
        Object propsElem = createPropsXmlElem(new ObjectFactory(), xmlProps);
        // Marshal the properties.
        marshaller.marshal(propsElem, qualifyingPropsNode);
    } catch (JAXBException ex) {
        throw new MarshalException("Error on JAXB marshalling", ex);
    }
}
Also used : Marshaller(jakarta.xml.bind.Marshaller) ObjectFactory(xades4j.xml.bind.xades.ObjectFactory) JAXBException(jakarta.xml.bind.JAXBException) JAXBContext(jakarta.xml.bind.JAXBContext) PropertyDataObject(xades4j.properties.data.PropertyDataObject)

Example 9 with JAXBContext

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

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

the class ResourceBundle method save.

public String save(final String fileName, final boolean compress) {
    String fileNameWithExtension = fileName;
    if (!fileNameWithExtension.endsWith("." + FILE_EXTENSION)) {
        fileNameWithExtension += "." + FILE_EXTENSION;
    }
    final File newFile = new File(fileNameWithExtension);
    if (newFile.exists()) {
        try {
            Files.delete(newFile.toPath().toAbsolutePath());
        } catch (IOException e) {
            log.log(Level.WARNING, e.getMessage(), e);
        }
    }
    Collections.sort(this.getMaps());
    Collections.sort(this.getSpriteSheets());
    Collections.sort(this.getTilesets());
    Collections.sort(this.getEmitters());
    Collections.sort(this.getBluePrints());
    Collections.sort(this.getSounds());
    try (FileOutputStream fileOut = new FileOutputStream(newFile, false)) {
        final JAXBContext jaxbContext = XmlUtilities.getContext(ResourceBundle.class);
        final Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
        if (compress) {
            final GZIPOutputStream stream = new GZIPOutputStream(fileOut);
            jaxbMarshaller.marshal(this, stream);
            stream.flush();
            stream.close();
        } else {
            final ByteArrayOutputStream out = new ByteArrayOutputStream();
            // first: marshal to byte array
            jaxbMarshaller.marshal(this, out);
            out.flush();
            // second: postprocess xml and then write it to the file
            XmlUtilities.saveWithCustomIndentation(new ByteArrayInputStream(out.toByteArray()), fileOut, 1);
            out.close();
        }
    } catch (final JAXBException | IOException e) {
        log.log(Level.SEVERE, e.getMessage(), e);
    }
    return newFile.toString();
}
Also used : Marshaller(jakarta.xml.bind.Marshaller) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) JAXBException(jakarta.xml.bind.JAXBException) JAXBContext(jakarta.xml.bind.JAXBContext) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File)

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