use of javax.xml.bind.Marshaller in project openhab1-addons by openhab.
the class LgTvEventChannelChanged method readevent.
public String readevent(String s) throws JAXBException {
JAXBContext jc;
jc = JAXBContext.newInstance(envelope.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
int start = s.indexOf("<envelope>");
int stop = s.indexOf("</envelope>") + "</envelope>".length();
String t = s.substring(start, stop);
// System.out.println(t);
StringReader reader = new StringReader(t);
envel = null;
envel = (envelope) unmarshaller.unmarshal(reader);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter sw = new StringWriter();
marshaller.marshal(envel, sw);
return new String(sw.toString());
}
use of javax.xml.bind.Marshaller in project openhab1-addons by openhab.
the class DenonConnector method postDocument.
private <T, S> T postDocument(String uri, Class<T> response, S request) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(request.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(request, sw);
String result = doHttpRequest("POST", uri, sw.toString());
if (StringUtils.isNotBlank(result)) {
JAXBContext jcResponse = JAXBContext.newInstance(response);
@SuppressWarnings("unchecked") T obj = (T) jcResponse.createUnmarshaller().unmarshal(IOUtils.toInputStream(result));
return obj;
}
} catch (JAXBException e) {
logger.debug("Encoding error in post", e);
}
return null;
}
use of javax.xml.bind.Marshaller in project orientdb by orientechnologies.
the class OServerConfigurationLoaderXml method load.
public OServerConfiguration load() throws IOException {
try {
if (file != null) {
fileLastModified = file.lastModified();
String path = OFileUtils.getPath(file.getAbsolutePath());
String current = OFileUtils.getPath(new File("").getAbsolutePath());
if (path.startsWith(current))
path = path.substring(current.length() + 1);
OLogManager.instance().info(this, "Loading configuration from: %s...", path);
} else {
OLogManager.instance().info(this, "Loading configuration from input stream");
}
context = JAXBContext.newInstance(rootClass);
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setSchema(null);
final OServerConfiguration obj;
if (file != null) {
if (file.exists())
obj = rootClass.cast(unmarshaller.unmarshal(file));
else {
OLogManager.instance().error(this, "Server configuration file not found: %s", file);
return rootClass.getConstructor(OServerConfigurationLoaderXml.class).newInstance(this);
}
obj.location = file.getAbsolutePath();
} else {
obj = rootClass.cast(unmarshaller.unmarshal(inputStream));
obj.location = "memory";
}
// AUTO CONFIGURE SYSTEM CONFIGURATION
OGlobalConfiguration config;
if (obj.properties != null)
for (OServerEntryConfiguration prop : obj.properties) {
try {
config = OGlobalConfiguration.findByKey(prop.name);
if (config != null) {
config.setValue(prop.value);
}
} catch (Exception e) {
}
}
return obj;
} catch (Exception e) {
// SYNTAX ERROR? PRINT AN EXAMPLE
OLogManager.instance().error(this, "Invalid syntax. Below an example of how it should be:", e);
try {
context = JAXBContext.newInstance(rootClass);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
Object example = rootClass.getConstructor(OServerConfigurationLoaderXml.class).newInstance(this);
marshaller.marshal(example, System.out);
} catch (Exception ex) {
}
throw new IOException(e);
}
}
use of javax.xml.bind.Marshaller in project platformlayer by platformlayer.
the class JdbcJobRepository method toXml.
private String toXml(Action action) throws RepositoryException {
Object o;
try {
Marshaller marshaller = jaxbContext.createMarshaller();
StringWriter writer = new StringWriter();
marshaller.marshal(action, writer);
return writer.toString();
} catch (JAXBException e) {
throw new RepositoryException("Error serializing action", e);
}
}
use of javax.xml.bind.Marshaller in project platformlayer by platformlayer.
the class MarshallerContextResolver method getContext.
@Override
public Marshaller getContext(Class<?> clazz) {
if (clazz.equals(ManagedItemCollection.class)) {
// OK
} else if (ItemBase.class.isAssignableFrom(clazz)) {
// OK
} else {
return null;
}
JAXBContext jaxbContext = jaxbContextHelper.getJaxbContext(clazz);
try {
Marshaller m = jaxbContext.createMarshaller();
// m.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl());
m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl());
return m;
} catch (JAXBException e) {
throw new IllegalStateException("Error creating XML marshaller", e);
}
}
Aggregations