Search in sources :

Example 1 with Marshaller

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

the class Jaxb2XmlEncoder method initMarshaller.

private Marshaller initMarshaller(Class<?> clazz) throws CodecException, JAXBException {
    Marshaller marshaller = this.jaxbContexts.createMarshaller(clazz);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.name());
    marshaller = this.marshallerProcessor.apply(marshaller);
    return marshaller;
}
Also used : Marshaller(jakarta.xml.bind.Marshaller)

Example 2 with Marshaller

use of jakarta.xml.bind.Marshaller 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 3 with Marshaller

use of jakarta.xml.bind.Marshaller 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 4 with Marshaller

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

the class Jaxb2XmlEncoder method encodeValue.

@Override
public DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory, ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
    if (!Hints.isLoggingSuppressed(hints)) {
        LogFormatUtils.traceDebug(logger, traceOn -> {
            String formatted = LogFormatUtils.formatValue(value, !traceOn);
            return Hints.getLogPrefix(hints) + "Encoding [" + formatted + "]";
        });
    }
    boolean release = true;
    DataBuffer buffer = bufferFactory.allocateBuffer(1024);
    try {
        OutputStream outputStream = buffer.asOutputStream();
        Class<?> clazz = ClassUtils.getUserClass(value);
        Marshaller marshaller = initMarshaller(clazz);
        marshaller.marshal(value, outputStream);
        release = false;
        return buffer;
    } catch (MarshalException ex) {
        throw new EncodingException("Could not marshal " + value.getClass() + " to XML", ex);
    } catch (JAXBException ex) {
        throw new CodecException("Invalid JAXB configuration", ex);
    } finally {
        if (release) {
            DataBufferUtils.release(buffer);
        }
    }
}
Also used : Marshaller(jakarta.xml.bind.Marshaller) MarshalException(jakarta.xml.bind.MarshalException) EncodingException(org.springframework.core.codec.EncodingException) OutputStream(java.io.OutputStream) JAXBException(jakarta.xml.bind.JAXBException) CodecException(org.springframework.core.codec.CodecException) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 5 with Marshaller

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

Marshaller (jakarta.xml.bind.Marshaller)5 JAXBException (jakarta.xml.bind.JAXBException)4 JAXBContext (jakarta.xml.bind.JAXBContext)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 MarshalException (jakarta.xml.bind.MarshalException)1 OutputStream (java.io.OutputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 CodecException (org.springframework.core.codec.CodecException)1 EncodingException (org.springframework.core.codec.EncodingException)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1 HttpMessageConversionException (org.springframework.http.converter.HttpMessageConversionException)1