Search in sources :

Example 26 with Unmarshaller

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);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) OGlobalConfiguration(com.orientechnologies.orient.core.config.OGlobalConfiguration) IOException(java.io.IOException) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException)

Example 27 with Unmarshaller

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);
    }
}
Also used : JAXBException(javax.xml.bind.JAXBException) MimeUnmarshaller(org.springframework.oxm.mime.MimeUnmarshaller) AttachmentUnmarshaller(javax.xml.bind.attachment.AttachmentUnmarshaller) GenericUnmarshaller(org.springframework.oxm.GenericUnmarshaller) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 28 with Unmarshaller

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;
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller) JsonParseException(com.google.gson.JsonParseException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException)

Example 29 with Unmarshaller

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;
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) JAXBException(javax.xml.bind.JAXBException) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 30 with Unmarshaller

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);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) JAXBException(javax.xml.bind.JAXBException) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) XMLStreamException(javax.xml.stream.XMLStreamException) UnmarshalException(javax.xml.bind.UnmarshalException) HttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException) Unmarshaller(javax.xml.bind.Unmarshaller)

Aggregations

Unmarshaller (javax.xml.bind.Unmarshaller)292 JAXBContext (javax.xml.bind.JAXBContext)240 JAXBException (javax.xml.bind.JAXBException)97 InputStream (java.io.InputStream)91 Test (org.junit.Test)79 StringReader (java.io.StringReader)40 BaseTest (org.orcid.core.BaseTest)39 V2Convertible (org.orcid.core.version.V2Convertible)39 File (java.io.File)33 InputSource (org.xml.sax.InputSource)22 IOException (java.io.IOException)21 JAXBElement (javax.xml.bind.JAXBElement)18 Marshaller (javax.xml.bind.Marshaller)18 ByteArrayInputStream (java.io.ByteArrayInputStream)17 SAXSource (javax.xml.transform.sax.SAXSource)17 SAXParserFactory (javax.xml.parsers.SAXParserFactory)13 XMLInputFactory (javax.xml.stream.XMLInputFactory)13 XMLStreamException (javax.xml.stream.XMLStreamException)13 XMLStreamReader (javax.xml.stream.XMLStreamReader)13 Schema (javax.xml.validation.Schema)13