Search in sources :

Example 21 with Marshaller

use of javax.xml.bind.Marshaller in project jersey by jersey.

the class ResourceDoclet method getSerializedLinkFromTag.

private static String getSerializedLinkFromTag(final SeeTag linkTag) {
    final MemberDoc referencedMember = linkTag.referencedMember();
    if (referencedMember == null) {
        throw new NullPointerException("Referenced member of @link " + print(linkTag) + " cannot be resolved.");
    }
    if (!referencedMember.isStatic()) {
        LOG.warning("Referenced member of @link " + print(linkTag) + " is not static." + " Right now only references to static members are supported.");
        return null;
    }
    /* Get referenced example bean
         */
    final ClassDoc containingClass = referencedMember.containingClass();
    final Object object;
    try {
        final Field declaredField = Class.forName(containingClass.qualifiedName(), false, Thread.currentThread().getContextClassLoader()).getDeclaredField(referencedMember.name());
        if (referencedMember.isFinal()) {
            declaredField.setAccessible(true);
        }
        object = declaredField.get(null);
        LOG.log(Level.FINE, "Got object " + object);
    } catch (final Exception e) {
        LOG.info("Have classloader: " + ResourceDoclet.class.getClassLoader().getClass());
        LOG.info("Have thread classloader " + Thread.currentThread().getContextClassLoader().getClass());
        LOG.info("Have system classloader " + ClassLoader.getSystemClassLoader().getClass());
        LOG.log(Level.SEVERE, "Could not get field " + referencedMember.qualifiedName(), e);
        return null;
    }
    /* marshal the bean to xml
         */
    try {
        final JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
        final StringWriter stringWriter = new StringWriter();
        final Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(object, stringWriter);
        final String result = stringWriter.getBuffer().toString();
        LOG.log(Level.FINE, "Got marshalled output:\n" + result);
        return result;
    } catch (final Exception e) {
        LOG.log(Level.SEVERE, "Could serialize bean to xml: " + object, e);
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JAXBContext(javax.xml.bind.JAXBContext) MemberDoc(com.sun.javadoc.MemberDoc) MalformedURLException(java.net.MalformedURLException) ClassDoc(com.sun.javadoc.ClassDoc)

Example 22 with Marshaller

use of javax.xml.bind.Marshaller in project quickstarts by jboss-switchyard.

the class JaxbTransformationTest method testJaxbOrderAckToXML.

@Test
public void testJaxbOrderAckToXML() throws Exception {
    OrderAck orderAck = new OrderAck();
    orderAck.setAccepted(Boolean.TRUE);
    orderAck.setOrderId("PO-19838-XYZ");
    orderAck.setStatus("Order Accepted");
    JAXBContext jaxbContext = JAXBContext.newInstance(new Class[] { OrderAck.class });
    StringWriter resultWriter = new StringWriter();
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.marshal(orderAck, resultWriter);
    _testKit.compareXMLToResource(resultWriter.toString(), ORDER_ACK_XML);
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JAXBContext(javax.xml.bind.JAXBContext) Test(org.junit.Test)

Example 23 with Marshaller

use of javax.xml.bind.Marshaller in project quickstarts by jboss-switchyard.

the class JaxbTransformationTest method testJaxbOrderToXML.

@Test
public void testJaxbOrderToXML() throws Exception {
    Order order = new Order();
    order.setItemId("BUTTER");
    order.setOrderId("PO-19838-XYZ");
    order.setQuantity(200);
    JAXBContext jaxbContext = JAXBContext.newInstance(new Class[] { Order.class });
    StringWriter resultWriter = new StringWriter();
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.marshal(order, resultWriter);
    _testKit.compareXMLToResource(resultWriter.toString(), ORDER_XML);
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JAXBContext(javax.xml.bind.JAXBContext) Test(org.junit.Test)

Example 24 with Marshaller

use of javax.xml.bind.Marshaller in project openhab1-addons by openhab.

the class ConfigParser method marshal.

/**
     * This method saves List of Request objects into xml file
     *
     * @param requests
     *            object to be saved
     * @param xmlFileLocation
     *            file object to save the object into
     */
@SuppressWarnings("resource")
public void marshal(List<Request> requests, File xmlFileLocation) throws StiebelHeatPumpException {
    JAXBContext context;
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(xmlFileLocation), "UTF-8"));
    } catch (IOException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
    try {
        context = JAXBContext.newInstance(Requests.class);
    } catch (JAXBException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
    Marshaller m;
    try {
        m = context.createMarshaller();
    } catch (JAXBException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
    try {
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    } catch (PropertyException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
    try {
        m.marshal(new Requests(requests), writer);
    } catch (JAXBException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
    try {
        writer.close();
    } catch (IOException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) PropertyException(javax.xml.bind.PropertyException) FileOutputStream(java.io.FileOutputStream) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) Requests(org.openhab.binding.stiebelheatpump.protocol.Requests) BufferedWriter(java.io.BufferedWriter)

Example 25 with Marshaller

use of javax.xml.bind.Marshaller in project openhab1-addons by openhab.

the class LgTvChannelSet method savetofile.

/**
     * Save Channel List to File f
     * 
     * @param f
     */
public void savetofile(String f) {
    Writer writer = null;
    JAXBContext jc;
    try {
        jc = JAXBContext.newInstance(envelope.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "utf-8"));
        marshaller.marshal(envel, writer);
    } catch (PropertyException e) {
        logger.error("error in savetofile", e);
    } catch (JAXBException e) {
        logger.error("error in savetofile", e);
    } catch (IOException ex) {
        logger.error("error in savetofile", ex);
    } finally {
        try {
            writer.close();
        } catch (Exception ex) {
        }
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) PropertyException(javax.xml.bind.PropertyException) FileOutputStream(java.io.FileOutputStream) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) PropertyException(javax.xml.bind.PropertyException) JAXBException(javax.xml.bind.JAXBException) BufferedWriter(java.io.BufferedWriter)

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