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;
}
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);
}
}
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;
}
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);
}
}
}
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();
}
Aggregations