Search in sources :

Example 26 with Marshaller

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());
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 27 with Marshaller

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;
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext)

Example 28 with Marshaller

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);
    }
}
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 29 with Marshaller

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);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JAXBException(javax.xml.bind.JAXBException) RepositoryException(org.platformlayer.RepositoryException)

Example 30 with Marshaller

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);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) ItemBase(org.platformlayer.core.model.ItemBase) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext)

Aggregations

Marshaller (javax.xml.bind.Marshaller)214 JAXBContext (javax.xml.bind.JAXBContext)129 JAXBException (javax.xml.bind.JAXBException)75 StringWriter (java.io.StringWriter)73 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 File (java.io.File)22 Test (org.junit.Test)21 IOException (java.io.IOException)19 FileOutputStream (java.io.FileOutputStream)18 Unmarshaller (javax.xml.bind.Unmarshaller)18 JAXBElement (javax.xml.bind.JAXBElement)16 ByteArrayInputStream (java.io.ByteArrayInputStream)12 Writer (java.io.Writer)11 Document (org.w3c.dom.Document)8 InputStream (java.io.InputStream)7 QName (javax.xml.namespace.QName)7 Schema (javax.xml.validation.Schema)7 Diff (org.custommonkey.xmlunit.Diff)7 ArrayList (java.util.ArrayList)6 SchemaFactory (javax.xml.validation.SchemaFactory)6