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