Java Code Examples for javax.xml.stream.XMLOutputFactory#newInstance()

The following examples show how to use javax.xml.stream.XMLOutputFactory#newInstance() . 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: XMLStreamWriterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @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());
}
 
Example 2
Source File: StreamResultTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testStreamWriterWithStAXResultNStreamWriter() {
    final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"?><root></root>";

    try {
        XMLOutputFactory ofac = XMLOutputFactory.newInstance();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        XMLStreamWriter writer = ofac.createXMLStreamWriter(buffer);
        StAXResult res = new StAXResult(writer);
        writer = ofac.createXMLStreamWriter(res);
        writer.writeStartDocument("1.0");
        writer.writeStartElement("root");
        writer.writeEndElement();
        writer.writeEndDocument();
        writer.close();
        Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.toString());
    }
}
 
Example 3
Source File: Bug6846132Test.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSAXResult() {
    DefaultHandler handler = new DefaultHandler();

    final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"?><root></root>";
    try {
        SAXResult saxResult = new SAXResult(handler);
        // saxResult.setSystemId("jaxp-ri/unit-test/javax/xml/stream/XMLOutputFactoryTest/cr6846132.xml");
        XMLOutputFactory ofac = XMLOutputFactory.newInstance();
        XMLStreamWriter writer = ofac.createXMLStreamWriter(saxResult);
        writer.writeStartDocument("1.0");
        writer.writeStartElement("root");
        writer.writeEndElement();
        writer.writeEndDocument();
        writer.flush();
        writer.close();
    } catch (Exception e) {
        if (e instanceof UnsupportedOperationException) {
            // expected
        } else {
            e.printStackTrace();
            Assert.fail(e.toString());
        }
    }
}
 
Example 4
Source File: StreamResultTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testStreamResult() {
    final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"?><root></root>";
    try {
        XMLOutputFactory ofac = XMLOutputFactory.newInstance();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        StreamResult sr = new StreamResult(buffer);
        XMLStreamWriter writer = ofac.createXMLStreamWriter(sr);
        writer.writeStartDocument("1.0");
        writer.writeStartElement("root");
        writer.writeEndElement();
        writer.writeEndDocument();
        writer.close();
        Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.toString());
    }
}
 
Example 5
Source File: ParamData.java    From Course_Generator with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Save parameters on disk
 * 
 * @param fname Name of the file
 */
public void SaveCurve(String fname, int unit) {
	if (data.size() <= 0) {
		return;
	}

	// -- Save the data in the home directory
	XMLOutputFactory factory = XMLOutputFactory.newInstance();
	try {
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(fname));
		XMLStreamWriter writer = new IndentingXMLStreamWriter(
				factory.createXMLStreamWriter(bufferedOutputStream, "UTF-8"));

		writer.writeStartDocument("UTF-8", "1.0");
		writer.writeStartElement("Project");
		Utils.WriteStringToXML(writer, "Name", name);
		Utils.WriteStringToXML(writer, "Comment", comment);
		writer.writeStartElement("Param");
		for (CgParam curvePoint : data) {
			writer.writeStartElement("Item");
			Utils.WriteStringToXML(writer, "Slope", String.format(Locale.ROOT, "%f", curvePoint.getSlope()));
			// Saving the curve speeds using the metric system.
			Utils.WriteStringToXML(writer, "Speed", String.format(Locale.ROOT, "%f", curvePoint.getSpeedNumber()));
			writer.writeEndElement(); // Item
		}
		writer.writeEndElement(); // Param
		writer.writeEndElement(); // Project
		writer.writeEndDocument();
		writer.flush();
		writer.close();
		bufferedOutputStream.close();

	} catch (XMLStreamException | IOException e) {
		e.printStackTrace();
	}
}
 
Example 6
Source File: AbstractMarshallerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void marshalStaxResultStreamWriter() throws Exception {
	XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
	StringWriter writer = new StringWriter();
	XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
	Result result = StaxUtils.createStaxResult(streamWriter);
	marshaller.marshal(flights, result);
	assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
}
 
Example 7
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 8
Source File: NullUriDetectionTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test1() throws Exception {
    XMLOutputFactory xof = XMLOutputFactory.newInstance();
    xof.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);

    StringWriter sw = new StringWriter();
    XMLStreamWriter w = xof.createXMLStreamWriter(sw);
    w.writeStartDocument();
    w.writeStartElement("foo", "bar", "zot");
    w.writeDefaultNamespace(null);
    w.writeCharacters("---");
}
 
Example 9
Source File: DefaultFactoryWrapperTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name = "jaxpFactories")
public Object[][] jaxpFactories() throws Exception {
    return new Object[][] {
            { DocumentBuilderFactory.newInstance(), (Produce)factory -> ((DocumentBuilderFactory)factory).newDocumentBuilder() },
            { SAXParserFactory.newInstance(), (Produce)factory -> ((SAXParserFactory)factory).newSAXParser() },
            { SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI), (Produce)factory -> ((SchemaFactory)factory).newSchema() },
            { TransformerFactory.newInstance(), (Produce)factory -> ((TransformerFactory)factory).newTransformer() },
            { XMLEventFactory.newInstance(), (Produce)factory -> ((XMLEventFactory)factory).createStartDocument() },
            { XMLInputFactory.newInstance(), (Produce)factory -> ((XMLInputFactory)factory).createXMLEventReader(new StringReader("")) },
            { XMLOutputFactory.newInstance(), (Produce)factory -> ((XMLOutputFactory)factory).createXMLEventWriter(new StringWriter()) },
            { XPathFactory.newInstance(), (Produce)factory -> ((XPathFactory)factory).newXPath() },
            { DatatypeFactory.newInstance(), (Produce)factory -> ((DatatypeFactory)factory).newXMLGregorianCalendar() }
    };
}
 
Example 10
Source File: XMLEventStreamWriterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void createStreamReader() throws Exception {
	stringWriter = new StringWriter();
	XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
	XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(stringWriter);
	streamWriter = new XMLEventStreamWriter(eventWriter, XMLEventFactory.newInstance());
}
 
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 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 12
Source File: XStreamMarshallerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void marshalStaxResultXMLStreamWriter() throws Exception {
	XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
	StringWriter writer = new StringWriter();
	XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
	Result result = StaxUtils.createStaxResult(streamWriter);
	marshaller.marshal(flight, result);
	assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
 
Example 13
Source File: AbstractMarshallerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 14
Source File: StaxUtilsTests.java    From spring-analysis-note with MIT License 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 15
Source File: StaxUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void isStaxResult() throws Exception {
	XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
	XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
	Result result = StaxUtils.createCustomStaxResult(streamWriter);

	assertTrue("Not a StAX Result", StaxUtils.isStaxResult(result));
}
 
Example 16
Source File: Bug6452107.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Ensure that charset aliases are checked. The encoding ISO-8859-1 is
 * returned as ISO8859_1 by the underlying writer. Thus, if alias are not
 * inspected, this test throws an exception.
 */
@Test
public void test() {
    final String ENCODING = "ISO-8859-1";

    try {
        OutputStream out = new ByteArrayOutputStream();
        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        XMLStreamWriter writer = factory.createXMLStreamWriter(out, ENCODING);
        writer.writeStartDocument(ENCODING, "1.0");
    } catch (XMLStreamException e) {
        Assert.fail("Exception occured: " + e.getMessage());
    }
}
 
Example 17
Source File: StaxResultTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
	TransformerFactory transformerFactory = TransformerFactory.newInstance();
	transformer = transformerFactory.newTransformer();
	inputFactory = XMLOutputFactory.newInstance();
}
 
Example 18
Source File: CfgSync.java    From aion with MIT License 4 votes vote down vote up
public String toXML() {
    final XMLOutputFactory output = XMLOutputFactory.newInstance();
    XMLStreamWriter xmlWriter;
    String xml;
    try {
        Writer strWriter = new StringWriter();
        xmlWriter = output.createXMLStreamWriter(strWriter);

        // start element sync
        xmlWriter.writeCharacters("\r\n\t");
        xmlWriter.writeStartElement("sync");

        // sub-element show-status
        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeStartElement("show-status");
        xmlWriter.writeCharacters(this.showStatus + "");
        xmlWriter.writeEndElement();

        // sub-element show-status
        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeComment(
                "requires show-status=true; comma separated list of options: "
                        + Arrays.toString(StatsType.values()).toLowerCase());
        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeStartElement("show-statistics");
        xmlWriter.writeCharacters(printSelectedStats().toLowerCase());
        xmlWriter.writeEndElement();

        // close element sync
        xmlWriter.writeCharacters("\r\n\t");
        xmlWriter.writeEndElement();

        xml = strWriter.toString();
        strWriter.flush();
        strWriter.close();
        xmlWriter.flush();
        xmlWriter.close();
        return xml;
    } catch (IOException | XMLStreamException e) {
        return "";
    }
}
 
Example 19
Source File: DumpTube.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * @param name
 *      Specify the name that identifies this {@link DumpTube}
 *      instance. This string will be printed when this pipe
 *      dumps messages, and allows people to distinguish which
 *      pipe instance is dumping a message when multiple
 *      {@link DumpTube}s print messages out.
 * @param out
 *      The output to send dumps to.
 * @param next
 *      The next {@link Tube} in the pipeline.
 */
public DumpTube(String name, PrintStream out, Tube next) {
    super(next);
    this.name = name;
    this.out = out;
    this.staxOut = XMLOutputFactory.newInstance();
    //staxOut.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES,true);
}
 
Example 20
Source File: FreeColXMLWriter.java    From freecol with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Get the {@code XMLOutputFactory} to create the output stream with.
 *
 * @return An {@code XMLOutputFactory}.
 */
private XMLOutputFactory getFactory() {
    return XMLOutputFactory.newInstance();
}