use of javax.xml.bind.Unmarshaller 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.Unmarshaller in project spring-framework by spring-projects.
the class Jaxb2Marshaller method createUnmarshaller.
/**
* Return a newly created JAXB unmarshaller.
* Note: JAXB unmarshallers are not necessarily thread-safe.
*/
protected Unmarshaller createUnmarshaller() {
try {
Unmarshaller unmarshaller = getJaxbContext().createUnmarshaller();
initJaxbUnmarshaller(unmarshaller);
return unmarshaller;
} catch (JAXBException ex) {
throw convertJaxbException(ex);
}
}
use of javax.xml.bind.Unmarshaller in project play-cookbook by spinscale.
the class ApiPlugin method getXml.
private Object getXml(Class clazz) {
try {
Unmarshaller um = jc.createUnmarshaller();
if (clazz.getAnnotation(XmlRootElement.class) != null) {
String body;
if (Request.current().params._contains("body")) {
body = Request.current().params.get("body");
} else {
body = IOUtils.toString(Request.current().body);
}
StringReader sr = new StringReader(body);
return um.unmarshal(sr);
}
} catch (Exception e) {
Logger.error(e, "Problem rendering XML: %s", e.getMessage());
}
return null;
}
use of javax.xml.bind.Unmarshaller in project play-cookbook by spinscale.
the class ApiPlugin method getXml.
private Object getXml(Class clazz) {
try {
if (clazz.getAnnotation(XmlRootElement.class) != null) {
Unmarshaller um = jc.createUnmarshaller();
StringReader sr = new StringReader(Request.current().params.get("body"));
return um.unmarshal(sr);
}
} catch (JAXBException e) {
Logger.error("Problem rendering XML: %s", e.getMessage());
}
return null;
}
use of javax.xml.bind.Unmarshaller in project spring-framework by spring-projects.
the class Jaxb2CollectionHttpMessageConverter method read.
@Override
@SuppressWarnings("unchecked")
public T read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
ParameterizedType parameterizedType = (ParameterizedType) type;
T result = createCollection((Class<?>) parameterizedType.getRawType());
Class<?> elementClass = (Class<?>) parameterizedType.getActualTypeArguments()[0];
try {
Unmarshaller unmarshaller = createUnmarshaller(elementClass);
XMLStreamReader streamReader = this.inputFactory.createXMLStreamReader(inputMessage.getBody());
int event = moveToFirstChildOfRootElement(streamReader);
while (event != XMLStreamReader.END_DOCUMENT) {
if (elementClass.isAnnotationPresent(XmlRootElement.class)) {
result.add(unmarshaller.unmarshal(streamReader));
} else if (elementClass.isAnnotationPresent(XmlType.class)) {
result.add(unmarshaller.unmarshal(streamReader, elementClass).getValue());
} else {
// should not happen, since we check in canRead(Type)
throw new HttpMessageConversionException("Could not unmarshal to [" + elementClass + "]");
}
event = moveToNextElement(streamReader);
}
return result;
} catch (UnmarshalException ex) {
throw new HttpMessageNotReadableException("Could not unmarshal to [" + elementClass + "]: " + ex.getMessage(), ex);
} catch (JAXBException ex) {
throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
} catch (XMLStreamException ex) {
throw new HttpMessageConversionException(ex.getMessage(), ex);
}
}
Aggregations