Search in sources :

Example 96 with Marshaller

use of javax.xml.bind.Marshaller in project tomee by apache.

the class Info method marshaller.

private static Marshaller marshaller() throws JAXBException {
    final Marshaller marshaller = JAXB_CONTEXT.createMarshaller();
    marshaller.setProperty("jaxb.formatted.output", true);
    return marshaller;
}
Also used : Marshaller(javax.xml.bind.Marshaller)

Example 97 with Marshaller

use of javax.xml.bind.Marshaller in project webservices-axiom by apache.

the class TestGetSAXResultJAXB method runTest.

@Override
protected void runTest() throws Throwable {
    List<OrderItem> items = new ArrayList<OrderItem>(2);
    OrderItem item = new OrderItem();
    item.setPartId("P85-137-19");
    item.setQuantity(2);
    items.add(item);
    item = new OrderItem();
    item.setPartId("O85-554-66");
    item.setQuantity(1);
    items.add(item);
    Order order = new Order();
    order.setCustomerId("73107481");
    order.setItems(items);
    Marshaller marshaller = JAXBContext.newInstance(Order.class).createMarshaller();
    StringWriter out = new StringWriter();
    marshaller.marshal(order, out);
    OMDocument document = metaFactory.getOMFactory().createOMDocument();
    marshaller.marshal(order, document.getSAXResult().getHandler());
    assertAbout(xml()).that(xml(OMDocument.class, document)).hasSameContentAs(out.toString());
}
Also used : Order(org.apache.axiom.ts.om.document.jaxb.Order) Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) OrderItem(org.apache.axiom.ts.om.document.jaxb.OrderItem) ArrayList(java.util.ArrayList) OMDocument(org.apache.axiom.om.OMDocument)

Example 98 with Marshaller

use of javax.xml.bind.Marshaller in project tomee by apache.

the class JaxbJavaee method marshal.

public static <T> void marshal(final Class<T> type, final Object object, final OutputStream out) throws JAXBException {
    final JAXBContext ctx2 = JaxbJavaee.getContext(type);
    final Marshaller marshaller = ctx2.createMarshaller();
    marshaller.setProperty("jaxb.formatted.output", true);
    marshaller.marshal(object, out);
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBContext(javax.xml.bind.JAXBContext)

Example 99 with Marshaller

use of javax.xml.bind.Marshaller in project tomee by apache.

the class JpaJaxbUtil method marshal.

public static <T> String marshal(final Class<T> type, final Object object) throws JAXBException {
    final JAXBContext ctx2 = JAXBContextFactory.newInstance(type);
    final Marshaller marshaller = ctx2.createMarshaller();
    marshaller.setProperty("jaxb.formatted.output", true);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    marshaller.marshal(object, baos);
    return new String(baos.toByteArray());
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBContext(javax.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 100 with Marshaller

use of javax.xml.bind.Marshaller in project tomee by apache.

the class OpenejbJarTest method testAll.

public void testAll() throws Exception {
    final JAXBContext ctx = JAXBContextFactory.newInstance(OpenejbJar.class);
    final Unmarshaller unmarshaller = ctx.createUnmarshaller();
    final InputStream in = this.getClass().getClassLoader().getResourceAsStream("openejb-jar.xml");
    final String expected = readContent(in);
    unmarshaller.setEventHandler(new TestValidationEventHandler());
    final Object object = unmarshaller.unmarshal(new ByteArrayInputStream(expected.getBytes()));
    assertTrue(object instanceof OpenejbJar);
    final Marshaller marshaller = ctx.createMarshaller();
    marshaller.setProperty("jaxb.formatted.output", true);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    marshaller.marshal(object, baos);
    final String actual = new String(baos.toByteArray());
    XMLUnit.setIgnoreWhitespace(true);
    try {
        final Diff myDiff = new DetailedDiff(new Diff(expected, actual));
        assertTrue("Files are not similar " + myDiff, myDiff.similar());
    } catch (final AssertionFailedError e) {
        e.printStackTrace();
        assertEquals(expected, actual);
        throw e;
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) Diff(org.custommonkey.xmlunit.Diff) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JAXBContext(javax.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Unmarshaller(javax.xml.bind.Unmarshaller) AssertionFailedError(junit.framework.AssertionFailedError)

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