Search in sources :

Example 31 with XMLOutputFactory

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

the class JAXBEncoderDecoderTest method testMarshallWithoutQNameInfo.

@Test
public void testMarshallWithoutQNameInfo() throws Exception {
    GreetMe obj = new GreetMe();
    obj.setRequestType("Hello");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLOutputFactory opFactory = XMLOutputFactory.newInstance();
    opFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
    XMLEventWriter writer = opFactory.createXMLEventWriter(baos);
    // STARTDOCUMENT/ENDDOCUMENT is not required
    // writer.add(eFactory.createStartDocument("utf-8", "1.0"));
    JAXBEncoderDecoder.marshall(context.createMarshaller(), obj, null, writer);
    // writer.add(eFactory.createEndDocument());
    writer.flush();
    writer.close();
    // System.out.println(baos.toString());
    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) XMLEventWriter(javax.xml.stream.XMLEventWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLEventReader(javax.xml.stream.XMLEventReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JAXBElement(javax.xml.bind.JAXBElement) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Example 32 with XMLOutputFactory

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

the class XMLStreamDataWriterTest method setUp.

@Before
public void setUp() throws Exception {
    baos = new ByteArrayOutputStream();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    streamWriter = factory.createXMLStreamWriter(baos);
    assertNotNull(streamWriter);
    inFactory = XMLInputFactory.newInstance();
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Before(org.junit.Before)

Example 33 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project Payara by payara.

the class ConfigPersistence method test.

@Test
public void test() throws TransactionFailure {
    final DomDocument document = getDocument(getHabitat());
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.reset();
    final ConfigurationPersistence testPersistence = new ConfigurationPersistence() {

        public void save(DomDocument doc) throws IOException, XMLStreamException {
            XMLOutputFactory factory = XMLOutputFactory.newInstance();
            XMLStreamWriter writer = factory.createXMLStreamWriter(baos);
            doc.writeTo(new IndentingXMLStreamWriter(writer));
            writer.close();
        }
    };
    TransactionListener testListener = new TransactionListener() {

        public void transactionCommited(List<PropertyChangeEvent> changes) {
            try {
                testPersistence.save(document);
            } catch (IOException e) {
                // To change body of catch statement use File | Settings | File Templates.
                e.printStackTrace();
            } catch (XMLStreamException e) {
                // To change body of catch statement use File | Settings | File Templates.
                e.printStackTrace();
            }
        }

        public void unprocessedTransactedEvents(List<UnprocessedChangeEvents> changes) {
        }
    };
    Transactions transactions = getHabitat().getService(Transactions.class);
    try {
        transactions.addTransactionsListener(testListener);
        doTest();
    } catch (TransactionFailure f) {
        f.printStackTrace();
        throw f;
    } finally {
        transactions.waitForDrain();
        transactions.removeTransactionsListener(testListener);
    }
    // now check if we persisted correctly...
    final String resultingXml = baos.toString();
    logger.fine(resultingXml);
    assertTrue("assertResult from " + getClass().getName() + " was false from " + resultingXml, assertResult(resultingXml));
}
Also used : TransactionListener(org.jvnet.hk2.config.TransactionListener) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) IndentingXMLStreamWriter(org.jvnet.hk2.config.IndentingXMLStreamWriter) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) ConfigurationPersistence(org.glassfish.config.support.ConfigurationPersistence) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DomDocument(org.jvnet.hk2.config.DomDocument) Transactions(org.jvnet.hk2.config.Transactions) XMLStreamException(javax.xml.stream.XMLStreamException) IndentingXMLStreamWriter(org.jvnet.hk2.config.IndentingXMLStreamWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) List(java.util.List) Test(org.junit.Test)

Example 34 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project Payara by payara.

the class DeepCopyTest method save.

public OutputStream save(DomDocument doc) throws IOException, XMLStreamException {
    final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    outStream.reset();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = factory.createXMLStreamWriter(outStream);
    doc.writeTo(new IndentingXMLStreamWriter(writer));
    writer.close();
    return outStream;
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 35 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project Payara by payara.

the class ConfigModularityUtils method serializeConfigBean.

public String serializeConfigBean(ConfigBeanProxy configBean) {
    if (configBean == null)
        return null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    XMLOutputFactory xmlFactory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = null;
    IndentingXMLStreamWriter indentingXMLStreamWriter = null;
    String s = null;
    try {
        writer = xmlFactory.createXMLStreamWriter(new BufferedOutputStream(bos));
        indentingXMLStreamWriter = new IndentingXMLStreamWriter(writer);
        Dom configBeanDom = Dom.unwrap(configBean);
        configBeanDom.writeTo(configBeanDom.model.getTagName(), indentingXMLStreamWriter);
        indentingXMLStreamWriter.flush();
        s = bos.toString();
    } catch (XMLStreamException e) {
        if (LOG.isLoggable(Level.FINE)) {
            LOG.log(Level.FINE, "Cannot serialize the configbean: " + configBean.toString(), e);
        }
        return null;
    } finally {
        try {
            if (bos != null)
                bos.close();
            if (writer != null)
                writer.close();
            if (indentingXMLStreamWriter != null)
                indentingXMLStreamWriter.close();
        } catch (IOException e) {
            if (LOG.isLoggable(Level.FINE)) {
                LOG.log(Level.FINE, "Cannot serialize the configbean: " + configBean.toString(), e);
            }
        } catch (XMLStreamException e) {
            if (LOG.isLoggable(Level.FINE)) {
                LOG.log(Level.FINE, "Cannot serialize the configbean: " + configBean.toString(), e);
            }
        }
    }
    return s;
}
Also used : IndentingXMLStreamWriter(org.jvnet.hk2.config.IndentingXMLStreamWriter) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) Dom(org.jvnet.hk2.config.Dom) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) IndentingXMLStreamWriter(org.jvnet.hk2.config.IndentingXMLStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

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