Search in sources :

Example 61 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project cxf by apache.

the class JAXBEncoderDecoderTest method testMarshallIntoStaxStreamWriter.

@Test
public void testMarshallIntoStaxStreamWriter() throws Exception {
    GreetMe obj = new GreetMe();
    obj.setRequestType("Hello");
    QName elName = new QName(wrapperAnnotation.targetNamespace(), wrapperAnnotation.localName());
    MessagePartInfo part = new MessagePartInfo(elName, null);
    part.setElement(true);
    part.setElementQName(elName);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLOutputFactory opFactory = XMLOutputFactory.newInstance();
    opFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
    FixNamespacesXMLStreamWriter writer = new FixNamespacesXMLStreamWriter(opFactory.createXMLStreamWriter(baos));
    assertNull(writer.getMarshaller());
    Marshaller m = context.createMarshaller();
    JAXBEncoderDecoder.marshall(m, obj, part, writer);
    assertEquals(m, writer.getMarshaller());
    writer.flush();
    writer.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    XMLInputFactory ipFactory = XMLInputFactory.newInstance();
    XMLEventReader reader = ipFactory.createXMLEventReader(bais);
    Unmarshaller um = context.createUnmarshaller();
    Object val = um.unmarshal(reader, GreetMe.class);
    assertTrue(val instanceof JAXBElement);
    val = ((JAXBElement<?>) val).getValue();
    assertTrue(val instanceof GreetMe);
    assertEquals(obj.getRequestType(), ((GreetMe) val).getRequestType());
}
Also used : GreetMe(org.apache.hello_world_soap_http.types.GreetMe) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) Marshaller(javax.xml.bind.Marshaller) QName(javax.xml.namespace.QName) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JAXBElement(javax.xml.bind.JAXBElement) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLEventReader(javax.xml.stream.XMLEventReader) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Aggregations

XMLOutputFactory (javax.xml.stream.XMLOutputFactory)61 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)40 XMLStreamException (javax.xml.stream.XMLStreamException)24 StringWriter (java.io.StringWriter)23 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 Test (org.junit.Test)15 XMLEventWriter (javax.xml.stream.XMLEventWriter)12 IOException (java.io.IOException)9 DOMResult (javax.xml.transform.dom.DOMResult)7 StAXResult (javax.xml.transform.stax.StAXResult)7 StreamResult (javax.xml.transform.stream.StreamResult)6 HashMap (java.util.HashMap)5 QName (javax.xml.namespace.QName)5 Result (javax.xml.transform.Result)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 OutputStream (java.io.OutputStream)4 Marshaller (javax.xml.bind.Marshaller)4 XMLEventReader (javax.xml.stream.XMLEventReader)4 XMLInputFactory (javax.xml.stream.XMLInputFactory)4 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)4