javax.xml.transform.stax.StAXResult Java Examples

The following examples show how to use javax.xml.transform.stax.StAXResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XMLOutputFactoryImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(javax.xml.transform.Result result) throws javax.xml.stream.XMLStreamException {

        if (result instanceof StreamResult) {
            return createXMLStreamWriter((StreamResult) result, null);
        } else if (result instanceof DOMResult) {
            return new XMLDOMWriterImpl((DOMResult) result);
        } else if (result instanceof StAXResult) {
            if (((StAXResult) result).getXMLStreamWriter() != null) {
                return ((StAXResult) result).getXMLStreamWriter();
            } else {
                throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported");
            }
        } else {
            if (result.getSystemId() !=null) {
                //this is not correct impl of SAXResult. Keep it for now for compatibility
                return createXMLStreamWriter(new StreamResult(result.getSystemId()));
            } else {
                throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported. " +
                        "Supported result types are: DOMResult, StAXResult and StreamResult.");
            }
        }

    }
 
Example #2
Source File: StaEDIXMLStreamReaderTest.java    From staedi with Apache License 2.0 6 votes vote down vote up
@Test
void testXmlIOEquivalence() throws Exception {
    XMLStreamReader xmlReader = getXmlReader("/x12/extraDelimiter997.edi");
    xmlReader.next(); // Per StAXSource JavaDoc, put in START_DOCUMENT state
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    StringWriter result = new StringWriter();
    transformer.transform(new StAXSource(xmlReader), new StreamResult(result));
    String resultString = result.toString();

    EDIOutputFactory ediOutFactory = EDIOutputFactory.newFactory();
    ediOutFactory.setProperty(EDIOutputFactory.PRETTY_PRINT, "true");
    ByteArrayOutputStream resultEDI = new ByteArrayOutputStream();
    EDIStreamWriter ediWriter = ediOutFactory.createEDIStreamWriter(resultEDI);
    XMLStreamWriter xmlWriter = new StaEDIXMLStreamWriter(ediWriter);
    transformer.transform(new StreamSource(new StringReader(resultString)), new StAXResult(xmlWriter));
    String expectedEDI = String.join("\n", Files.readAllLines(Paths.get("src/test/resources/x12/extraDelimiter997.edi")));
    assertEquals(expectedEDI, new String(resultEDI.toByteArray()).trim());
}
 
Example #3
Source File: XMLOutputFactoryImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(javax.xml.transform.Result result) throws javax.xml.stream.XMLStreamException {

        if (result instanceof StreamResult) {
            return createXMLStreamWriter((StreamResult) result, null);
        } else if (result instanceof DOMResult) {
            return new XMLDOMWriterImpl((DOMResult) result);
        } else if (result instanceof StAXResult) {
            if (((StAXResult) result).getXMLStreamWriter() != null) {
                return ((StAXResult) result).getXMLStreamWriter();
            } else {
                throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported");
            }
        } else {
            if (result.getSystemId() !=null) {
                //this is not correct impl of SAXResult. Keep it for now for compatibility
                return createXMLStreamWriter(new StreamResult(result.getSystemId()));
            } else {
                throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported. " +
                        "Supported result types are: DOMResult, StAXResult and StreamResult.");
            }
        }

    }
 
Example #4
Source File: XMLOutputFactoryImpl.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(javax.xml.transform.Result result) throws javax.xml.stream.XMLStreamException {

        if (result instanceof StreamResult) {
            return createXMLStreamWriter((StreamResult) result, null);
        } else if (result instanceof DOMResult) {
            return new XMLDOMWriterImpl((DOMResult) result);
        } else if (result instanceof StAXResult) {
            if (((StAXResult) result).getXMLStreamWriter() != null) {
                return ((StAXResult) result).getXMLStreamWriter();
            } else {
                throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported");
            }
        } else {
            if (result.getSystemId() !=null) {
                //this is not correct impl of SAXResult. Keep it for now for compatibility
                return createXMLStreamWriter(new StreamResult(result.getSystemId()));
            } else {
                throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported. " +
                        "Supported result types are: DOMResult, StAXResult and StreamResult.");
            }
        }

    }
 
Example #5
Source File: XMLOutputFactoryImpl.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(javax.xml.transform.Result result) throws javax.xml.stream.XMLStreamException {

        if (result instanceof StreamResult) {
            return createXMLStreamWriter((StreamResult) result, null);
        } else if (result instanceof DOMResult) {
            return new XMLDOMWriterImpl((DOMResult) result);
        } else if (result instanceof StAXResult) {
            if (((StAXResult) result).getXMLStreamWriter() != null) {
                return ((StAXResult) result).getXMLStreamWriter();
            } else {
                throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported");
            }
        } else {
            if (result.getSystemId() !=null) {
                //this is not correct impl of SAXResult. Keep it for now for compatibility
                return createXMLStreamWriter(new StreamResult(result.getSystemId()));
            } else {
                throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported. " +
                        "Supported result types are: DOMResult, StAXResult and StreamResult.");
            }
        }

    }
 
Example #6
Source File: XMLOutputFactoryImpl.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
public XMLStreamWriter createXMLStreamWriter(Result result)
        throws XMLStreamException {

    if (result instanceof StreamResult) {
        return createXMLStreamWriter((StreamResult) result, null);
    } else if (result instanceof DOMResult) {
        return new XMLDOMWriterImpl((DOMResult) result);
    } else if (result instanceof StAXResult) {
        if (((StAXResult) result).getXMLStreamWriter() != null) {
            return ((StAXResult) result).getXMLStreamWriter();
        } else {
            throw new UnsupportedOperationException(
                    "Result of type " + result + " is not supported");
        }
    } else if (result.getSystemId() != null) {
        //this is not correct impl of SAXResult. Keep it for now for compatibility
        return createXMLStreamWriter(new StreamResult(result.getSystemId()));
    } else {
        throw new UnsupportedOperationException(
                "Result of type " + result + " is not supported. Supported result "
                        + "types are: DOMResult, StAXResult and StreamResult.");
    }

}
 
Example #7
Source File: XMLOutputFactoryImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(javax.xml.transform.Result result) throws javax.xml.stream.XMLStreamException {

        if (result instanceof StreamResult) {
            return createXMLStreamWriter((StreamResult) result, null);
        } else if (result instanceof DOMResult) {
            return new XMLDOMWriterImpl((DOMResult) result);
        } else if (result instanceof StAXResult) {
            if (((StAXResult) result).getXMLStreamWriter() != null) {
                return ((StAXResult) result).getXMLStreamWriter();
            } else {
                throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported");
            }
        } else {
            if (result.getSystemId() !=null) {
                //this is not correct impl of SAXResult. Keep it for now for compatibility
                return createXMLStreamWriter(new StreamResult(result.getSystemId()));
            } else {
                throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported. " +
                        "Supported result types are: DOMResult, StAXResult and StreamResult.");
            }
        }

    }
 
Example #8
Source File: XMLOutputFactoryImpl.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(javax.xml.transform.Result result) throws javax.xml.stream.XMLStreamException {

        if (result instanceof StreamResult) {
            return createXMLStreamWriter((StreamResult) result, null);
        } else if (result instanceof DOMResult) {
            return new XMLDOMWriterImpl((DOMResult) result);
        } else if (result instanceof StAXResult) {
            if (((StAXResult) result).getXMLStreamWriter() != null) {
                return ((StAXResult) result).getXMLStreamWriter();
            } else {
                throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported");
            }
        } else {
            if (result.getSystemId() !=null) {
                //this is not correct impl of SAXResult. Keep it for now for compatibility
                return createXMLStreamWriter(new StreamResult(result.getSystemId()));
            } else {
                throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported. " +
                        "Supported result types are: DOMResult, StAXResult and StreamResult.");
            }
        }

    }
 
Example #9
Source File: XMLOutputFactoryImpl.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(javax.xml.transform.Result result) throws javax.xml.stream.XMLStreamException {

        if (result instanceof StreamResult) {
            return createXMLStreamWriter((StreamResult) result, null);
        } else if (result instanceof DOMResult) {
            return new XMLDOMWriterImpl((DOMResult) result);
        } else if (result instanceof StAXResult) {
            if (((StAXResult) result).getXMLStreamWriter() != null) {
                return ((StAXResult) result).getXMLStreamWriter();
            } else {
                throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported");
            }
        } else {
            if (result.getSystemId() !=null) {
                //this is not correct impl of SAXResult. Keep it for now for compatibility
                return createXMLStreamWriter(new StreamResult(result.getSystemId()));
            } else {
                throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported. " +
                        "Supported result types are: DOMResult, StAXResult and StreamResult.");
            }
        }

    }
 
Example #10
Source File: StaxUtilsTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void isStaxResultJaxp14() throws Exception {
	XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
	XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
	StAXResult result = new StAXResult(streamWriter);

	assertTrue("Not a StAX Result", StaxUtils.isStaxResult(result));
}
 
Example #11
Source File: StreamResultTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@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());
    }
}
 
Example #12
Source File: StaxUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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");
	}
}
 
Example #13
Source File: XML.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Marshal the given object to a stream, DOM or other destinations.
 * This is the most flexible marshalling method provided in this {@code XML} class.
 * The destination is specified by the {@code output} argument implementation, for example
 * {@link javax.xml.transform.stream.StreamResult} for writing to a file or output stream.
 * The optional {@code properties} map can contain any key documented in this {@code XML} class,
 * together with the keys documented in the <cite>supported properties</cite> section of the the
 * {@link Marshaller} class.
 *
 * @param  object      the root of content tree to be marshalled.
 * @param  output      the file to be written.
 * @param  properties  an optional map of properties to give to the marshaller, or {@code null} if none.
 * @throws JAXBException if a property has an illegal value, or if an error occurred during the marshalling.
 *
 * @since 0.4
 */
public static void marshal(final Object object, final Result output, final Map<String,?> properties) throws JAXBException {
    ensureNonNull("object", object);
    ensureNonNull("output", output);
    final MarshallerPool pool = getPool();
    final Marshaller marshaller = pool.acquireMarshaller();
    if (properties != null) {
        for (final Map.Entry<String,?> entry : properties.entrySet()) {
            marshaller.setProperty(entry.getKey(), entry.getValue());
        }
    }
    /*
     * STAX results are not handled by JAXB as of JDK 8. We have to handle those cases ourselves.
     * This workaround should be removed if a future JDK version handles those cases.
     */
    if (output instanceof StAXResult) {
        @Workaround(library = "JDK", version = "1.8")
        final XMLStreamWriter writer = ((StAXResult) output).getXMLStreamWriter();
        if (writer != null) {
            marshaller.marshal(object, writer);
        } else {
            marshaller.marshal(object, ((StAXResult) output).getXMLEventWriter());
        }
    } else {
        marshaller.marshal(object, output);
    }
    pool.recycle(marshaller);
}
 
Example #14
Source File: JDBCSQLXML.java    From evosql with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves a new Result for setting the XML value designated by this
 * SQLXML instance.
 *
 * @param resultClass The class of the result, or null.
 * @throws java.sql.SQLException if there is an error processing the XML
 *         value or the state is not writable
 * @return for setting the XML value designated by this SQLXML instance.
 */
protected <T extends Result>T createResult(
        Class<T> resultClass) throws SQLException {

    checkWritable();
    setWritable(false);
    setReadable(true);

    if (JAXBResult.class.isAssignableFrom(resultClass)) {

        // Must go first presently, since JAXBResult extends SAXResult
        // (purely as an implementation detail) and it's not possible
        // to instantiate a valid JAXBResult with a Zero-Args
        // constructor(or any subclass thereof, due to the finality of
        // its private UnmarshallerHandler)
        // FALL THROUGH... will throw an exception
    } else if ((resultClass == null)
               || StreamResult.class.isAssignableFrom(resultClass)) {
        return createStreamResult(resultClass);
    } else if (DOMResult.class.isAssignableFrom(resultClass)) {
        return createDOMResult(resultClass);
    } else if (SAXResult.class.isAssignableFrom(resultClass)) {
        return createSAXResult(resultClass);
    } else if (StAXResult.class.isAssignableFrom(resultClass)) {
        return createStAXResult(resultClass);
    }

    throw JDBCUtil.invalidArgument("resultClass: " + resultClass);
}
 
Example #15
Source File: Bug4892774.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testSAX2StAXStream() {
    try {
        Source input = saxUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
        StAXResult staxResult = (StAXResult) staxUtil.prepareStreamResult();
        idTransform.transform(input, staxResult);
        staxUtil.checkStreamResult(staxResult, EXPECTED_VERSION);

    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception occured: " + e.getMessage());
    }
}
 
Example #16
Source File: Bug4892774.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testDOM2StAX() {
    try {
        Source input = domUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
        StAXResult staxResult = (StAXResult) staxUtil.prepareResult();
        idTransform.transform(input, staxResult);
        staxUtil.checkResult(staxResult, EXPECTED_VERSION, "UTF-8");

    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception occured: " + e.getMessage());
    }
}
 
Example #17
Source File: DocViewFormat.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
/** internally formats the given file and computes their checksum
 * 
 * @param file the file
 * @param original checksum of the original file
 * @param formatted checksum of the formatted file
 * @return the formatted bytes
 * @throws IOException if an error occurs */
private byte[] format(File file, Checksum original, Checksum formatted) throws IOException {
    try (InputStream in = new CheckedInputStream(new BufferedInputStream(new FileInputStream(file)), original)) {
        @SuppressWarnings("resource")
        ByteArrayOutputStream buffer = formattingBuffer != null ? formattingBuffer.get() : null;
        if (buffer == null) {
            buffer = new ByteArrayOutputStream();
            formattingBuffer = new WeakReference<>(buffer);
        } else {
            buffer.reset();
        }

        try (OutputStream out = new CheckedOutputStream(buffer, formatted);
             FormattingXmlStreamWriter writer = FormattingXmlStreamWriter.create(out, format)) {
            // cannot use XMlStreamReader due to comment handling:
            // https://stackoverflow.com/questions/15792007/why-does-xmlstreamreader-staxsource-strip-comments-from-xml
            TransformerFactory tf = TransformerFactory.newInstance();
            tf.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
            SAXSource saxSource = new SAXSource(new InputSource(in));
            SAXParserFactory sf = SAXParserFactory.newInstance();
            sf.setNamespaceAware(true);
            sf.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
            sf.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
            saxSource.setXMLReader(new NormalizingSaxFilter(sf.newSAXParser().getXMLReader()));
            Transformer t = tf.newTransformer();
            StAXResult result = new StAXResult(writer);
            t.transform(saxSource, result);
        }
        return buffer.toByteArray();
    } catch (TransformerException | XMLStreamException | FactoryConfigurationError | ParserConfigurationException | SAXException ex) {
        throw new IOException(ex);
    }
}
 
Example #18
Source File: Bug4892774.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testSAX2StAX() {
    try {
        Source input = saxUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
        StAXResult staxResult = (StAXResult) staxUtil.prepareResult();
        idTransform.transform(input, staxResult);
        staxUtil.checkResult(staxResult, EXPECTED_VERSION, "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception occured: " + e.getMessage());
    }
}
 
Example #19
Source File: TransformerFactoryTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Refer to 6631168 : StAXSource & StAXResult support in JavaSE6
 */
@Test
public final void testFeatures() {
    TransformerFactory tff = TransformerFactory.newInstance();
    Assert.assertTrue(tff.getFeature(StAXSource.FEATURE));
    Assert.assertTrue(tff.getFeature(StAXResult.FEATURE));
}
 
Example #20
Source File: Bug4892774.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testStream2Stax() {
    try {
        Source input = streamUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
        StAXResult staxResult = (StAXResult) staxUtil.prepareResult();
        idTransform.transform(input, staxResult);
        staxUtil.checkResult(staxResult, EXPECTED_VERSION, "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception occured: " + e.getMessage());
    }
}
 
Example #21
Source File: AbstractMarshallerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void marshalJaxp14StaxResultStreamWriter() throws Exception {
	XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
	StringWriter writer = new StringWriter();
	XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
	StAXResult result = new StAXResult(streamWriter);
	marshaller.marshal(flights, result);
	assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
}
 
Example #22
Source File: AbstractMarshallerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #23
Source File: Bug4892774.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testStAX2StAX() {
    try {
        Source input = staxUtil.prepareStreamSource(this.getClass().getResourceAsStream(XML10_FILE));
        StAXResult staxResult = (StAXResult) staxUtil.prepareResult();
        idTransform.transform(input, staxResult);
        staxUtil.checkResult(staxResult, "1.0", "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception occured: " + e.getMessage());
    }
}
 
Example #24
Source File: StreamResultTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@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());
    }
}
 
Example #25
Source File: Bug4892774.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testDOM2StAXStream() {
    try {
        Source input = domUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
        StAXResult staxResult = (StAXResult) staxUtil.prepareStreamResult();
        idTransform.transform(input, staxResult);
        staxUtil.checkStreamResult(staxResult, EXPECTED_VERSION);

    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception occured: " + e.getMessage());
    }
}
 
Example #26
Source File: StreamResultTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@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());
    }
}
 
Example #27
Source File: OutputFactory.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new writer for the JAXP result.
 * Note that this method is identified as <em>optional</em> in JSE javadoc.
 * It is caller's responsibility to close the given stream writer after usage
 * (it will <strong>not</strong> be done by {@link XMLEventWriter#close()}).
 *
 * @param  out  where to write to.
 * @return the writer.
 * @throws XMLStreamException if the writer can not be created.
 */
public static XMLEventWriter createXMLEventWriter(final XMLStreamWriter out) throws XMLStreamException {
    return FACTORY.createXMLEventWriter(new StAXResult(new StreamWriterDelegate(out) {
        @Override public void close() throws XMLStreamException {
            /*
             * Do not close the XMLStreamWriter because user may continue writing to it.
             * Do not flush neither; the default XMLStreamWriterImpl does nothing more
             * than forwarding to java.io.Writer.flush() and flushing an output stream
             * have a performance impact. If the user really wants to flush, (s)he can
             * invoke XMLStreamWriter.flush() himself.
             */
        }
    }));
}
 
Example #28
Source File: XMLOutputFactoryImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public XMLEventWriter createXMLEventWriter(Result result)
        throws XMLStreamException {

    if (result instanceof StAXResult && ((StAXResult) result).getXMLEventWriter() != null) {
        return ((StAXResult) result).getXMLEventWriter();
    }

    return new XMLEventWriterImpl(createXMLStreamWriter(result));
}
 
Example #29
Source File: StaxUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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");
	}
}
 
Example #30
Source File: StaxUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return the {@link XMLStreamWriter} 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 XMLStreamWriter getXMLStreamWriter(Result result) {
	if (result instanceof StAXResult) {
		return ((StAXResult) result).getXMLStreamWriter();
	}
	else if (result instanceof StaxResult) {
		return ((StaxResult) result).getXMLStreamWriter();
	}
	else {
		throw new IllegalArgumentException("Result '" + result + "' is neither StaxResult nor StAXResult");
	}
}