Search in sources :

Example 46 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project spring-framework by spring-projects.

the class AbstractMarshallerTests method marshalJaxp14StaxResultEventWriter.

@Test
public void marshalJaxp14StaxResultEventWriter() throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    StringWriter writer = new StringWriter();
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
    StAXResult result = new StAXResult(eventWriter);
    marshaller.marshal(flights, result);
    assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
Also used : StAXResult(javax.xml.transform.stax.StAXResult) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) StringWriter(java.io.StringWriter) XMLEventWriter(javax.xml.stream.XMLEventWriter) Test(org.junit.Test)

Example 47 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project CFLint by cflint.

the class DefaultCFlintResultMarshaller method output.

@Override
public void output(BugList bugList, Writer writer, boolean showStats) throws MarshallerException {
    try {
        final XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
        XMLStreamWriter xtw = new IndentingXMLStreamWriter(xmlOutputFactory.createXMLStreamWriter(writer));
        writeIssues(bugList, xtw, showStats);
        xtw.flush();
    } catch (XMLStreamException e) {
        throw new MarshallerException(e);
    }
}
Also used : IndentingXMLStreamWriter(javanet.staxutils.IndentingXMLStreamWriter) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) MarshallerException(com.cflint.xml.MarshallerException) XMLStreamException(javax.xml.stream.XMLStreamException) IndentingXMLStreamWriter(javanet.staxutils.IndentingXMLStreamWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 48 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project jqa-core-framework by buschmais.

the class RuleSetWriterImpl method marshal.

private void marshal(Writer writer, JqassistantRules rules) {
    XMLOutputFactory xof = XMLOutputFactory.newInstance();
    XMLStreamWriter streamWriter = null;
    try {
        streamWriter = xof.createXMLStreamWriter(writer);
    } catch (XMLStreamException e) {
        e.printStackTrace();
    }
    XMLStreamWriter indentingStreamWriter = new IndentingXMLStreamWriter(new CDataXMLStreamWriter(streamWriter));
    try {
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.marshal(rules, indentingStreamWriter);
    } catch (JAXBException e) {
        throw new IllegalArgumentException("Cannot write rules to " + writer, e);
    }
}
Also used : IndentingXMLStreamWriter(com.sun.xml.txw2.output.IndentingXMLStreamWriter) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) Marshaller(javax.xml.bind.Marshaller) CDataXMLStreamWriter(com.buschmais.jqassistant.core.rule.impl.reader.CDataXMLStreamWriter) XMLStreamException(javax.xml.stream.XMLStreamException) IndentingXMLStreamWriter(com.sun.xml.txw2.output.IndentingXMLStreamWriter) CDataXMLStreamWriter(com.buschmais.jqassistant.core.rule.impl.reader.CDataXMLStreamWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) JAXBException(javax.xml.bind.JAXBException)

Example 49 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project webservices-axiom by apache.

the class TestCreateXMLStreamWriterThreadSafety method runTest.

@SuppressWarnings("deprecation")
protected void runTest() throws Throwable {
    final XMLOutputFactory factory = staxImpl.getDialect().makeThreadSafe(staxImpl.newNormalizedXMLOutputFactory());
    ConcurrentTestUtils.testThreadSafety(new Action() {

        public void execute() throws Exception {
            String text = String.valueOf((int) (Math.random() * 10000));
            StringWriter out = new StringWriter();
            XMLStreamWriter writer = factory.createXMLStreamWriter(out);
            writer.writeStartElement("root");
            writer.writeCharacters(text);
            writer.writeEndElement();
            writer.writeEndDocument();
            writer.flush();
            writer.close();
            assertEquals("<root>" + text + "</root>", out.toString());
        }
    });
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) Action(org.apache.axiom.testutils.concurrent.Action) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 50 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project webservices-axiom by apache.

the class TestCreateXMLStreamWriterWithNullEncoding method runTest.

protected void runTest() throws Throwable {
    XMLOutputFactory factory = staxImpl.newNormalizedXMLOutputFactory();
    // This should cause an exception
    try {
        factory.createXMLStreamWriter(System.out, null);
    } catch (Throwable ex) {
        // Expected
        return;
    }
    // Attention here: since the fail method works by throwing an exception and we
    // catch Throwable, it must be invoked outside of the catch block!
    fail("Expected createXMLStreamWriter to throw an exception");
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory)

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