Search in sources :

Example 81 with Marshaller

use of javax.xml.bind.Marshaller in project SmartApplianceEnabler by camueller.

the class SempController method marshall.

private String marshall(Device2EM device2EM) {
    StringWriter writer = new StringWriter();
    try {
        JAXBContext context = JAXBContext.newInstance(Device2EM.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(device2EM, writer);
        return writer.toString();
    } catch (JAXBException e) {
        logger.error("Error marshalling", 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 82 with Marshaller

use of javax.xml.bind.Marshaller in project ddf by codice.

the class XmlParser method marshal.

private void marshal(ParserConfigurator configurator, Consumer<Marshaller> marshallerConsumer) throws ParserException {
    JAXBContext jaxbContext = getContext(configurator.getContextPath(), configurator.getClassLoader());
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(configurator.getClassLoader());
        Marshaller marshaller = jaxbContext.createMarshaller();
        if (configurator.getAdapter() != null) {
            marshaller.setAdapter(configurator.getAdapter());
        }
        if (configurator.getHandler() != null) {
            marshaller.setEventHandler(configurator.getHandler());
        }
        for (Map.Entry<String, Object> propRow : configurator.getProperties().entrySet()) {
            marshaller.setProperty(propRow.getKey(), propRow.getValue());
        }
        marshallerConsumer.accept(marshaller);
    } catch (RuntimeException e) {
        LOGGER.debug("Error marshalling ", e);
        throw new ParserException("Error marshalling ", e);
    } catch (JAXBException e) {
        LOGGER.debug("Error marshalling ", e);
        throw new ParserException("Error marshalling", e);
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }
}
Also used : ParserException(org.codice.ddf.parser.ParserException) Marshaller(javax.xml.bind.Marshaller) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) Map(java.util.Map)

Example 83 with Marshaller

use of javax.xml.bind.Marshaller in project ddf by codice.

the class KMLTransformerImpl method marshalKml.

private String marshalKml(Kml kmlResult) {
    String kmlResultString = null;
    StringWriter writer = new StringWriter();
    try {
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, UTF_8);
        marshaller.marshal(kmlResult, writer);
    } catch (JAXBException e) {
        LOGGER.debug("Failed to marshal KML: ", e);
    }
    kmlResultString = writer.toString();
    return kmlResultString;
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JAXBException(javax.xml.bind.JAXBException) LineString(com.vividsolutions.jts.geom.LineString)

Example 84 with Marshaller

use of javax.xml.bind.Marshaller in project cogcomp-nlp by CogComp.

the class XmlModel method getMarshaller.

private static Marshaller getMarshaller(Object o) throws JAXBException {
    Marshaller m = getContext(o.getClass()).createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.setProperty("jaxb.encoding", "UTF-8");
    return m;
}
Also used : Marshaller(javax.xml.bind.Marshaller)

Example 85 with Marshaller

use of javax.xml.bind.Marshaller in project jdk8u_jdk by JetBrains.

the class XmlConfigUtils method toString.

/**
     * Creates an XML string representation of the given bean.
     * @throws IllegalArgumentException if the bean class is not known by the
     *         underlying XMLbinding context.
     * @return An XML string representation of the given bean.
     **/
public static String toString(Object bean) {
    try {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final Marshaller m = createMarshaller();
        m.setProperty(m.JAXB_FRAGMENT, Boolean.TRUE);
        m.marshal(bean, baos);
        return baos.toString();
    } catch (JAXBException x) {
        final IllegalArgumentException iae = new IllegalArgumentException("Failed to write SessionConfigBean: " + x, x);
        throw iae;
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBException(javax.xml.bind.JAXBException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

Marshaller (javax.xml.bind.Marshaller)280 JAXBContext (javax.xml.bind.JAXBContext)162 JAXBException (javax.xml.bind.JAXBException)100 StringWriter (java.io.StringWriter)82 Test (org.junit.Test)33 JAXBElement (javax.xml.bind.JAXBElement)32 ByteArrayOutputStream (java.io.ByteArrayOutputStream)31 File (java.io.File)29 Unmarshaller (javax.xml.bind.Unmarshaller)21 IOException (java.io.IOException)20 FileOutputStream (java.io.FileOutputStream)19 ByteArrayInputStream (java.io.ByteArrayInputStream)15 QName (javax.xml.namespace.QName)14 Element (org.w3c.dom.Element)14 HashMap (java.util.HashMap)12 Writer (java.io.Writer)11 Document (org.w3c.dom.Document)11 InputStream (java.io.InputStream)7 OutputStream (java.io.OutputStream)7 ArrayList (java.util.ArrayList)7