Java 类javax.xml.stream.XMLEventWriter 实例源码

项目:dhus-core    文件:SearchController.java   
void xmlToJson(InputStream xmlInput, OutputStream jsonOutput) throws XMLStreamException
{
   JsonXMLConfig config = new JsonXMLConfigBuilder()
         .autoArray(true)
         .autoPrimitive(false)
         .fieldPrefix("")
         .contentField("content")
         .build();

   XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(xmlInput);
   XMLEventWriter writer = new JsonXMLOutputFactory(config).createXMLEventWriter(jsonOutput);

   writer.add(reader);

   reader.close();
   writer.close();
}
项目:openjdk-jdk10    文件:XMLEventWriterTest.java   
/**
 * Test XMLStreamWriter parsing a file with an external entity reference.
 */
@Test
public void testXMLStreamWriter() {

    try {
        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
        XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(System.out);
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        String file = getClass().getResource("XMLEventWriterTest.xml").getPath();
        XMLEventReader eventReader = inputFactory.createXMLEventReader(new StreamSource(new File(file)));

        // adds the event to the consumer.
        eventWriter.add(eventReader);
        eventWriter.flush();
        eventWriter.close();

        // expected success
    } catch (Exception exception) {
        exception.printStackTrace();
        Assert.fail(exception.toString());
    }
}
项目:openjdk-jdk10    文件:ReaderToWriterTest.java   
/**
 * Test: 6419687 NPE in XMLEventWriterImpl.
 */
@Test
public void testCR6419687() {

    try {
        InputStream in = getClass().getResourceAsStream("ReaderToWriterTest.wsdl");
        OutputStream out = new FileOutputStream(USER_DIR + "ReaderToWriterTest-out.xml");

        XMLEventReader reader = XML_INPUT_FACTORY.createXMLEventReader(in);
        XMLEventWriter writer = XML_OUTPUT_FACTORY.createXMLEventWriter(out, "UTF-8");
        while (reader.hasNext()) {
            XMLEvent event = reader.nextEvent();
            writer.add(event);
        }
        reader.close();
        writer.close();
    } catch (XMLStreamException xmlStreamException) {
        xmlStreamException.printStackTrace();
        Assert.fail(xmlStreamException.toString());
    } catch (FileNotFoundException fileNotFoundException) {
        fileNotFoundException.printStackTrace();
        Assert.fail(fileNotFoundException.toString());
    }
}
项目:openjdk-jdk10    文件:ReaderToWriterTest.java   
@Test
public void testUTF8Encoding() {
    try {
        InputStream in = util.BOMInputStream.createStream("UTF-16BE", this.getClass().getResourceAsStream(INPUT_FILE));
        OutputStream out = new FileOutputStream(OUTPUT_FILE);

        XMLEventReader reader = XML_INPUT_FACTORY.createXMLEventReader(in);
        XMLEventWriter writer = XML_OUTPUT_FACTORY.createXMLEventWriter(out, "UTF-8");

        writeEvents(reader, writer);
        checkOutput(OUTPUT_FILE);

    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception occured: " + e.getMessage());
    } finally {
        File file = new File(OUTPUT_FILE);
        if (file.exists())
            file.delete();
    }
}
项目:openjdk-jdk10    文件:ReaderToWriterTest.java   
@Test
public void testNoEncoding() {
    try {
        InputStream in = util.BOMInputStream.createStream("UTF-16BE", this.getClass().getResourceAsStream(INPUT_FILE));
        OutputStream out = new FileOutputStream(OUTPUT_FILE);

        XMLEventReader reader = XML_INPUT_FACTORY.createXMLEventReader(in);
        XMLEventWriter writer = XML_OUTPUT_FACTORY.createXMLEventWriter(out);

        writeEvents(reader, writer);
        checkOutput(OUTPUT_FILE);

    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception occured: " + e.getMessage());
    } finally {
        File file = new File(OUTPUT_FILE);
        if (file.exists())
            file.delete();
    }
}
项目:openjdk-jdk10    文件:XMLStreamWriterTest.java   
/**
 * @bug 8139584
 * Verifies that the resulting XML contains the standalone setting.
 */
@Test
public void testCreateStartDocument() throws XMLStreamException {

    StringWriter stringWriter = new StringWriter();
    XMLOutputFactory out = XMLOutputFactory.newInstance();
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();

    XMLEventWriter eventWriter = out.createXMLEventWriter(stringWriter);

    XMLEvent event = eventFactory.createStartDocument("iso-8859-15", "1.0", true);
    eventWriter.add(event);
    eventWriter.flush();
    Assert.assertTrue(stringWriter.toString().contains("encoding=\"iso-8859-15\""));
    Assert.assertTrue(stringWriter.toString().contains("version=\"1.0\""));
    Assert.assertTrue(stringWriter.toString().contains("standalone=\"yes\""));
}
项目:openjdk-jdk10    文件:XMLStreamWriterTest.java   
/**
 * @bug 8139584
 * Verifies that the resulting XML contains the standalone setting.
 */
@Test
public void testCreateStartDocument_DOMWriter()
        throws ParserConfigurationException, XMLStreamException {

    XMLOutputFactory xof = XMLOutputFactory.newInstance();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();
    XMLEventWriter eventWriter = xof.createXMLEventWriter(new DOMResult(doc));
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEvent event = eventFactory.createStartDocument("iso-8859-15", "1.0", true);
    eventWriter.add(event);
    eventWriter.flush();
    Assert.assertEquals(doc.getXmlEncoding(), "iso-8859-15");
    Assert.assertEquals(doc.getXmlVersion(), "1.0");
    Assert.assertTrue(doc.getXmlStandalone());
}
项目:openjdk-jdk10    文件:Bug6846132Test.java   
@Test
public void testSAXResult1() {
    DefaultHandler handler = new DefaultHandler();

    try {
        SAXResult saxResult = new SAXResult(handler);
        XMLOutputFactory ofac = XMLOutputFactory.newInstance();
        XMLEventWriter writer = ofac.createXMLEventWriter(saxResult);
    } catch (Exception e) {
        if (e instanceof UnsupportedOperationException) {
            // expected
        } else {
            e.printStackTrace();
            Assert.fail(e.toString());
        }
    }
}
项目:openjdk9    文件:XMLEventWriterTest.java   
/**
 * Test XMLStreamWriter parsing a file with an external entity reference.
 */
@Test
public void testXMLStreamWriter() {

    try {
        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
        XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(System.out);
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        String file = getClass().getResource("XMLEventWriterTest.xml").getPath();
        XMLEventReader eventReader = inputFactory.createXMLEventReader(new StreamSource(new File(file)));

        // adds the event to the consumer.
        eventWriter.add(eventReader);
        eventWriter.flush();
        eventWriter.close();

        // expected success
    } catch (Exception exception) {
        exception.printStackTrace();
        Assert.fail(exception.toString());
    }
}
项目:openjdk9    文件:ReaderToWriterTest.java   
/**
 * Test: 6419687 NPE in XMLEventWriterImpl.
 */
@Test
public void testCR6419687() {

    try {
        InputStream in = getClass().getResourceAsStream("ReaderToWriterTest.wsdl");
        OutputStream out = new FileOutputStream("ReaderToWriterTest-out.xml");

        XMLEventReader reader = XML_INPUT_FACTORY.createXMLEventReader(in);
        XMLEventWriter writer = XML_OUTPUT_FACTORY.createXMLEventWriter(out, "UTF-8");
        while (reader.hasNext()) {
            XMLEvent event = reader.nextEvent();
            writer.add(event);
        }
        reader.close();
        writer.close();
    } catch (XMLStreamException xmlStreamException) {
        xmlStreamException.printStackTrace();
        Assert.fail(xmlStreamException.toString());
    } catch (FileNotFoundException fileNotFoundException) {
        fileNotFoundException.printStackTrace();
        Assert.fail(fileNotFoundException.toString());
    }
}
项目:openjdk9    文件:ReaderToWriterTest.java   
@Test
public void testUTF8Encoding() {
    try {
        InputStream in = util.BOMInputStream.createStream("UTF-16BE", this.getClass().getResourceAsStream(INPUT_FILE));
        OutputStream out = new FileOutputStream(OUTPUT_FILE);

        XMLEventReader reader = XML_INPUT_FACTORY.createXMLEventReader(in);
        XMLEventWriter writer = XML_OUTPUT_FACTORY.createXMLEventWriter(out, "UTF-8");

        writeEvents(reader, writer);
        checkOutput(OUTPUT_FILE);

    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception occured: " + e.getMessage());
    } finally {
        File file = new File(OUTPUT_FILE);
        if (file.exists())
            file.delete();
    }
}
项目:openjdk9    文件:ReaderToWriterTest.java   
@Test
public void testNoEncoding() {
    try {
        InputStream in = util.BOMInputStream.createStream("UTF-16BE", this.getClass().getResourceAsStream(INPUT_FILE));
        OutputStream out = new FileOutputStream(OUTPUT_FILE);

        XMLEventReader reader = XML_INPUT_FACTORY.createXMLEventReader(in);
        XMLEventWriter writer = XML_OUTPUT_FACTORY.createXMLEventWriter(out);

        writeEvents(reader, writer);
        checkOutput(OUTPUT_FILE);

    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception occured: " + e.getMessage());
    } finally {
        File file = new File(OUTPUT_FILE);
        if (file.exists())
            file.delete();
    }
}
项目:openjdk9    文件:Bug6846132Test.java   
@Test
public void testSAXResult1() {
    DefaultHandler handler = new DefaultHandler();

    try {
        SAXResult saxResult = new SAXResult(handler);
        XMLOutputFactory ofac = XMLOutputFactory.newInstance();
        XMLEventWriter writer = ofac.createXMLEventWriter(saxResult);
    } catch (Exception e) {
        if (e instanceof UnsupportedOperationException) {
            // expected
        } else {
            e.printStackTrace();
            Assert.fail(e.toString());
        }
    }
}
项目:spring4-understanding    文件:AbstractMarshaller.java   
/**
 * Template method for handling {@code StaxResult}s.
 * <p>This implementation delegates to {@code marshalXMLSteamWriter} or
 * {@code marshalXMLEventConsumer}, depending on what is contained in the
 * {@code StaxResult}.
 * @param graph the root of the object graph to marshal
 * @param staxResult a JAXP 1.4 {@link StAXSource}
 * @throws XmlMappingException if the given object cannot be marshalled to the result
 * @throws IllegalArgumentException if the {@code domResult} is empty
 * @see #marshalDomNode(Object, org.w3c.dom.Node)
 */
protected void marshalStaxResult(Object graph, Result staxResult) throws XmlMappingException {
    XMLStreamWriter streamWriter = StaxUtils.getXMLStreamWriter(staxResult);
    if (streamWriter != null) {
        marshalXmlStreamWriter(graph, streamWriter);
    }
    else {
        XMLEventWriter eventWriter = StaxUtils.getXMLEventWriter(staxResult);
        if (eventWriter != null) {
            marshalXmlEventWriter(graph, eventWriter);
        }
        else {
            throw new IllegalArgumentException("StaxResult contains neither XMLStreamWriter nor XMLEventConsumer");
        }
    }
}
项目:fluentxml4j    文件:XmlResult.java   
public XMLEventWriter getXMLEventWriter(AutoFlushPolicy autoFlush)
{
    try
    {
        return XMLEventWriterProxy.proxyFor(XMLOutputFactory.newFactory().createXMLEventWriter(getOutputStream()), autoFlush == AutoFlushPolicy.AUTO_FLUSH);
    }
    catch (XMLStreamException ex)
    {
        throw new IllegalStateException(ex);
    }
}
项目:fluentxml4j    文件:StaxUtils.java   
public static XMLEventWriter newXMLEventWriter(OutputStream out)
{
    try
    {
        return XMLOutputFactory.newFactory().createXMLEventWriter(out);
    }
    catch (XMLStreamException ex)
    {
        throw new IllegalStateException(ex);
    }
}
项目:fluentxml4j    文件:StaxUtilsTest.java   
@Test
public void newXMLEventWriter() throws Exception
{
    XMLEventWriter writer = StaxUtils.newXMLEventWriter(outputStream);

    assertThat(writer, is(not(nullValue())));
}
项目:openjdk-jdk10    文件:ReaderToWriterTest.java   
private void writeEvents(XMLEventReader reader, XMLEventWriter writer) throws XMLStreamException {
    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        writer.add(event);
    }
    reader.close();
    writer.close();
}
项目:openjdk-jdk10    文件:StreamResultTest.java   
@Test
public void testEventWriterWithStAXResultNStreamWriter() {
    String encoding = "";
    if (getSystemProperty("file.encoding").equals("UTF-8")) {
        encoding = " encoding=\"UTF-8\"";
    }
    final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"" + encoding + "?><root></root>";

    try {
        XMLOutputFactory ofac = XMLOutputFactory.newInstance();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        XMLStreamWriter swriter = ofac.createXMLStreamWriter(buffer);
        StAXResult res = new StAXResult(swriter);
        XMLEventWriter writer = ofac.createXMLEventWriter(res);

        XMLEventFactory efac = XMLEventFactory.newInstance();
        writer.add(efac.createStartDocument(null, "1.0"));
        writer.add(efac.createStartElement("", "", "root"));
        writer.add(efac.createEndElement("", "", "root"));
        writer.add(efac.createEndDocument());
        writer.close();

        Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.toString());
    }
}
项目:openjdk-jdk10    文件:StreamResultTest.java   
@Test
public void testEventWriterWithStAXResultNEventWriter() {
    String encoding = "";
    if (getSystemProperty("file.encoding").equals("UTF-8")) {
        encoding = " encoding=\"UTF-8\"";
    }
    final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"" + encoding + "?><root></root>";

    try {
        XMLOutputFactory ofac = XMLOutputFactory.newInstance();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        XMLEventWriter writer = ofac.createXMLEventWriter(buffer);
        StAXResult res = new StAXResult(writer);
        writer = ofac.createXMLEventWriter(res);

        XMLEventFactory efac = XMLEventFactory.newInstance();
        writer.add(efac.createStartDocument(null, "1.0"));
        writer.add(efac.createStartElement("", "", "root"));
        writer.add(efac.createEndElement("", "", "root"));
        writer.add(efac.createEndDocument());
        writer.close();

        Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.toString());
    }
}
项目:openjdk-jdk10    文件:StreamResultTest.java   
@Test
public void testStreamWriterWithStAXResultNEventWriter() throws Exception {
    try {
        XMLOutputFactory ofac = XMLOutputFactory.newInstance();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        XMLEventWriter writer = ofac.createXMLEventWriter(buffer);
        StAXResult res = new StAXResult(writer);
        XMLStreamWriter swriter = ofac.createXMLStreamWriter(res);
        Assert.fail("Expected an Exception as XMLStreamWriter can't be created " + "with a StAXResult which has EventWriter.");
    } catch (Exception e) {
        System.out.println(e.toString());
    }
}
项目:openjdk-jdk10    文件:TransformTest.java   
private XMLEventWriter getXMLEventWriter() {
    try {
        return ofac.createXMLEventWriter(transOutputStream());
    } catch (XMLStreamException e) {
        throw new WrapperException(e);
    }
}
项目:openjdk9    文件:ReaderToWriterTest.java   
private void writeEvents(XMLEventReader reader, XMLEventWriter writer) throws XMLStreamException {
    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        writer.add(event);
    }
    reader.close();
    writer.close();
}
项目:openjdk9    文件:StreamResultTest.java   
@Test
public void testEventWriterWithStAXResultNStreamWriter() {
    String encoding = "";
    if (System.getProperty("file.encoding").equals("UTF-8")) {
        encoding = " encoding=\"UTF-8\"";
    }
    final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"" + encoding + "?><root></root>";

    try {
        XMLOutputFactory ofac = XMLOutputFactory.newInstance();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        XMLStreamWriter swriter = ofac.createXMLStreamWriter(buffer);
        StAXResult res = new StAXResult(swriter);
        XMLEventWriter writer = ofac.createXMLEventWriter(res);

        XMLEventFactory efac = XMLEventFactory.newInstance();
        writer.add(efac.createStartDocument(null, "1.0"));
        writer.add(efac.createStartElement("", "", "root"));
        writer.add(efac.createEndElement("", "", "root"));
        writer.add(efac.createEndDocument());
        writer.close();

        Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.toString());
    }
}
项目:openjdk9    文件:StreamResultTest.java   
@Test
public void testEventWriterWithStAXResultNEventWriter() {
    String encoding = "";
    if (System.getProperty("file.encoding").equals("UTF-8")) {
        encoding = " encoding=\"UTF-8\"";
    }
    final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"" + encoding + "?><root></root>";

    try {
        XMLOutputFactory ofac = XMLOutputFactory.newInstance();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        XMLEventWriter writer = ofac.createXMLEventWriter(buffer);
        StAXResult res = new StAXResult(writer);
        writer = ofac.createXMLEventWriter(res);

        XMLEventFactory efac = XMLEventFactory.newInstance();
        writer.add(efac.createStartDocument(null, "1.0"));
        writer.add(efac.createStartElement("", "", "root"));
        writer.add(efac.createEndElement("", "", "root"));
        writer.add(efac.createEndDocument());
        writer.close();

        Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.toString());
    }
}
项目:openjdk9    文件:StreamResultTest.java   
@Test
public void testStreamWriterWithStAXResultNEventWriter() throws Exception {
    try {
        XMLOutputFactory ofac = XMLOutputFactory.newInstance();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        XMLEventWriter writer = ofac.createXMLEventWriter(buffer);
        StAXResult res = new StAXResult(writer);
        XMLStreamWriter swriter = ofac.createXMLStreamWriter(res);
        Assert.fail("Expected an Exception as XMLStreamWriter can't be created " + "with a StAXResult which has EventWriter.");
    } catch (Exception e) {
        System.out.println(e.toString());
    }
}
项目:openjdk9    文件:TransformTest.java   
private XMLEventWriter getXMLEventWriter() {
    try {
        return ofac.createXMLEventWriter(transOutputStream());
    } catch (XMLStreamException e) {
        throw new WrapperException(e);
    }
}
项目:spring4-understanding    文件:StaxResult.java   
/**
 * Construct a new instance of the {@code StaxResult} with the specified {@code XMLEventWriter}.
 * @param eventWriter the {@code XMLEventWriter} to write to
 */
public StaxResult(XMLEventWriter eventWriter) {
    StaxEventHandler handler = new StaxEventHandler(eventWriter);
    super.setHandler(handler);
    super.setLexicalHandler(handler);
    this.eventWriter = eventWriter;
}
项目:spring4-understanding    文件:StaxUtils.java   
/**
 * Return the {@link XMLEventWriter} for the given StAX Result.
 * @param result a JAXP 1.4 {@link StAXResult}
 * @return the {@link XMLStreamReader}
 * @throws IllegalArgumentException if {@code source} isn't a JAXP 1.4 {@link StAXResult}
 * or custom StAX Result
 */
public static XMLEventWriter getXMLEventWriter(Result result) {
    if (result instanceof StAXResult) {
        return ((StAXResult) result).getXMLEventWriter();
    }
    else if (result instanceof StaxResult) {
        return ((StaxResult) result).getXMLEventWriter();
    }
    else {
        throw new IllegalArgumentException("Result '" + result + "' is neither StaxResult nor StAXResult");
    }
}
项目:spring4-understanding    文件:StaxResultTests.java   
@Test
public void eventWriterSource() throws Exception {
    StringWriter stringWriter = new StringWriter();
    XMLEventWriter eventWriter = inputFactory.createXMLEventWriter(stringWriter);
    Reader reader = new StringReader(XML);
    Source source = new StreamSource(reader);
    StaxResult result = new StaxResult(eventWriter);
    assertEquals("Invalid eventWriter returned", eventWriter, result.getXMLEventWriter());
    assertNull("StreamWriter returned", result.getXMLStreamWriter());
    transformer.transform(source, result);
    assertXMLEqual("Invalid result", XML, stringWriter.toString());
}
项目:spring4-understanding    文件:StaxEventHandlerTests.java   
@Override
protected AbstractStaxHandler createStaxHandler(Result result)
        throws XMLStreamException {
    XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(result);
    return new StaxEventHandler(eventWriter);
}
项目:spring4-understanding    文件:XMLEventStreamWriterTests.java   
@Before
public void createStreamReader() throws Exception {
    stringWriter = new StringWriter();
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(stringWriter);
    streamWriter = new XMLEventStreamWriter(eventWriter, XMLEventFactory.newInstance());
}
项目:Camel    文件:StaxConverterTest.java   
public void testEncodingXmlEventReader() throws Exception {
    TEST_XML_WITH_XML_HEADER_ISO_8859_1_AS_BYTE_ARRAY_STREAM.reset();
    XMLEventReader reader = null;
    XMLEventWriter writer = null;
    ByteArrayOutputStream output = null;
    try {
        // enter text encoded with Latin1
        reader = context.getTypeConverter().mandatoryConvertTo(XMLEventReader.class,
                TEST_XML_WITH_XML_HEADER_ISO_8859_1_AS_BYTE_ARRAY_STREAM);

        output = new ByteArrayOutputStream();
        // ensure UTF-8 encoding
        Exchange exchange = new DefaultExchange(context);
        exchange.setProperty(Exchange.CHARSET_NAME, UTF_8.toString());
        writer = context.getTypeConverter().mandatoryConvertTo(XMLEventWriter.class, exchange, output);
        while (reader.hasNext()) {
            writer.add(reader.nextEvent());
        }
    } finally {
        if (reader != null) {
            reader.close();
        }
        if (writer != null) {
            writer.close();
        }
    }
    assertNotNull(output);

    String result = new String(output.toByteArray(), UTF_8.name());
    // normalize the auotation mark
    if (result.indexOf('\'') > 0) {
        result = result.replace('\'', '"');
    }
    boolean equals = TEST_XML_WITH_XML_HEADER.equals(result) || TEST_XML_WITH_XML_HEADER_ISO_8859_1.equals(result);
    assertTrue("Should match header", equals);
}
项目:spring4-understanding    文件:Jaxb2Marshaller.java   
private void marshalStaxResult(Marshaller jaxbMarshaller, Object graph, Result staxResult) throws JAXBException {
    XMLStreamWriter streamWriter = StaxUtils.getXMLStreamWriter(staxResult);
    if (streamWriter != null) {
        jaxbMarshaller.marshal(graph, streamWriter);
    }
    else {
        XMLEventWriter eventWriter = StaxUtils.getXMLEventWriter(staxResult);
        if (eventWriter != null) {
            jaxbMarshaller.marshal(graph, eventWriter);
        }
        else {
            throw new IllegalArgumentException("StAX Result contains neither XMLStreamWriter nor XMLEventConsumer");
        }
    }
}
项目:spring4-understanding    文件:XStreamMarshallerTests.java   
@Test
public void marshalStaxResultXMLEventWriter() throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    StringWriter writer = new StringWriter();
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
    Result result = StaxUtils.createStaxResult(eventWriter);
    marshaller.marshal(flight, result);
    assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
}
项目:spring4-understanding    文件:AbstractMarshallerTests.java   
@Test
public void marshalStaxResultEventWriter() throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    StringWriter writer = new StringWriter();
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
    Result result = StaxUtils.createStaxResult(eventWriter);
    marshaller.marshal(flights, result);
    assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
}
项目:spring4-understanding    文件:AbstractMarshallerTests.java   
@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);
    assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
}
项目:svg-stockpile    文件:Stockpile.java   
/**
 * Writes the processed {@link XMLEvent}s to an {@link OutputStream}.
 * @param outputStream The {@link OutputStream} to write the {@link XMLEvent}s to.
 * @return The {@link Stockpile} instance for chaining.
 * @throws XMLStreamException If an XML error occurs.
 */
public Stockpile write(OutputStream outputStream) throws XMLStreamException {
    XMLEventWriter writer = output.createXMLEventWriter(outputStream);

    try {
        addEventsTo(writer);
    } finally {
        writer.flush();
        writer.close();
    }

    return this;
}
项目:ts-reaktive    文件:StaxWriter.java   
/**
 * Adds a namespace mapping for [name] to [namespaces], if it doesn't already exist on [writer].
 */
private static boolean register(XMLEventWriter writer, List<Namespace> namespaces, QName name) {
    String nsUri = name.getNamespaceURI();
    if (nsUri != null && !nsUri.isEmpty()) {
        String existing = writer.getNamespaceContext().getNamespaceURI(name.getPrefix());
        Namespace ns = evtFactory.createNamespace(name.getPrefix(), nsUri);
        if ((existing == null || !existing.equals(nsUri)) && !namespaces.stream().anyMatch(n ->
            n.getPrefix().equals(ns.getPrefix())
        )) {
            namespaces.add(ns);
            return true;
        }
    }
    return false;
}
项目:ts-reaktive    文件:Stax.java   
public <T> void write(T obj, Writer<XMLEvent,T> writer, OutputStream out) {
    try {
        XMLEventWriter xmlW = outFactory.createXMLEventWriter(out);
        writer.applyAndReset(obj).forEach(addTo(xmlW));
    } catch (XMLStreamException | FactoryConfigurationError e) {
        throw new IllegalArgumentException(e);
    }
}