Java Code Examples for org.codehaus.stax2.XMLStreamWriter2#close()

The following examples show how to use org.codehaus.stax2.XMLStreamWriter2#close() . 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: TestAttributeValidation.java    From woodstox with Apache License 2.0 6 votes vote down vote up
private void _testValidFixedAttr(boolean nsAware, boolean repairing) throws XMLStreamException
{
    StringWriter strw = new StringWriter();

    // Ok either without being added:
    XMLStreamWriter2 sw = getDTDValidatingWriter(strw, FIXED_DTD_STR, nsAware, repairing);
    sw.writeStartElement("root");
    sw.writeEndElement();
    sw.writeEndDocument();
    sw.close();

    sw = getDTDValidatingWriter(strw, FIXED_DTD_STR, nsAware, repairing);
    sw.writeEmptyElement("root");
    sw.writeEndDocument();
    sw.close();

    // or by using the exact same value
    sw = getDTDValidatingWriter(strw, FIXED_DTD_STR, nsAware, repairing);
    sw.writeStartElement("root");
    sw.writeAttribute("fixAttr", "fixedValue");
    sw.writeEndElement();
    sw.writeEndDocument();
    sw.close();
}
 
Example 2
Source File: TestAttributeValidation.java    From woodstox with Apache License 2.0 6 votes vote down vote up
private void _testValidNsAttr(boolean repairing) throws XMLStreamException
{
    StringWriter strw = new StringWriter();

    // Ok, as long as we use the right ns prefix... better also
    // output namespace declaration, in non-repairing mode.
    XMLStreamWriter2 sw = getDTDValidatingWriter(strw, IMPLIED_NS_DTD_STR, true, repairing);
    sw.writeStartElement("root");
    if (!repairing) {
        sw.writeNamespace(NS_PREFIX, NS_URI);
    }
    // prefix, uri, localname (for attrs!)
    sw.writeAttribute(NS_PREFIX, NS_URI, "attr", "value");
    sw.writeEndElement();
    sw.writeEndDocument();
    sw.close();
}
 
Example 3
Source File: W3CSchemaWrite23Test.java    From woodstox with Apache License 2.0 5 votes vote down vote up
public void testSchemaValidatingCopy23() throws Exception
{
    final String SCHEMA = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<xs:schema elementFormDefault=\"unqualified\"\n" +
            "           xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
            "    <xs:element name=\"Document\">\n" +
            "        <xs:complexType>\n" +
            "            <xs:sequence>\n" +
            "                <xs:element name=\"Paragraph\" type=\"xs:string\"/>\n" +
            "            </xs:sequence>\n" +
            "        </xs:complexType>\n" +
            "    </xs:element>\n" +
            "</xs:schema>";
    final String CONTENT = "<Document>\n" +
            "    <Paragraph>Hello world!</Paragraph>\n" +
            "</Document>";
    final String DOC = "<?xml version='1.0' encoding='UTF-8'?>\n"+CONTENT;
            

    StringWriter strw = new StringWriter();
    XMLStreamWriter2 xmlWriter = getSchemaValidatingWriter(strw, SCHEMA, false);
    XMLStreamReader2 xmlReader = constructNsStreamReader(DOC, false);

    // For this test we need validation, otherwise the reader returns characters events instead of white-space events.
    xmlReader.validateAgainst(parseW3CSchema(SCHEMA));

    while (xmlReader.hasNext()) {
        /*int type =*/ xmlReader.next();
        xmlWriter.copyEventFromReader(xmlReader, true);
    }
 
    xmlWriter.close();
    xmlReader.close();

    String xml = strw.toString();
    if (!xml.contains(CONTENT)) {
        fail("Should contain ["+CONTENT+"], does not: ["+xml+"]");
    }
}
 
Example 4
Source File: TestAttributeValidation.java    From woodstox with Apache License 2.0 5 votes vote down vote up
private void _testValidRequiredAttr(boolean nsAware, boolean repairing)
    throws XMLStreamException
{
    for (int i = 0; i < 3; ++i) {
        StringWriter strw = new StringWriter();

        // Ok if value is added:
        XMLStreamWriter2 sw = getDTDValidatingWriter(strw, REQUIRED_DTD_STR, nsAware, repairing);
        sw.writeStartElement("root");
        sw.writeAttribute("reqAttr", "value");
        sw.writeEndElement();
        sw.writeEndDocument();
        sw.close();

        // ... even if with empty value (for CDATA type, at least)
        sw = getDTDValidatingWriter(strw, REQUIRED_DTD_STR, nsAware, repairing);
        sw.writeStartElement("root");
        sw.writeAttribute("reqAttr", "");
        sw.writeEndElement();
        sw.writeEndDocument();
        sw.close();

        // and ditto for empty element:
        sw = getDTDValidatingWriter(strw, REQUIRED_DTD_STR, nsAware, repairing);
        sw.writeEmptyElement("root");
        sw.writeAttribute("reqAttr", "hii & haa");
        sw.writeEndDocument();
        sw.close();
    }
}
 
Example 5
Source File: TestStructuralValidation.java    From woodstox with Apache License 2.0 5 votes vote down vote up
public void testValidNsElem()
    throws XMLStreamException
{
    for (int i = 0; i < 3; ++i) {
        boolean repairing = (i == 2);
        StringWriter strw = new StringWriter();

        XMLStreamWriter2 sw = getDTDValidatingWriter(strw, SIMPLE_NS_DTD, true, repairing);
        // prefix, local name, uri (for elems)
        sw.writeStartElement(NS_PREFIX, "root", NS_URI);
        if (!repairing) {
            sw.writeNamespace(NS_PREFIX, NS_URI);
        }
        sw.writeEndElement();
        sw.writeEndDocument();
        sw.close();

        // and same with empty elem
        sw = getDTDValidatingWriter(strw, SIMPLE_NS_DTD, true, repairing);
        sw.writeEmptyElement(NS_PREFIX, "root", NS_URI);
        if (!repairing) {
            sw.writeNamespace(NS_PREFIX, NS_URI);
        }
        sw.writeEndDocument();
        sw.close();
    }
}
 
Example 6
Source File: TestOutputValidation.java    From woodstox with Apache License 2.0 4 votes vote down vote up
public void testValidEmptyContent()
    throws XMLStreamException
{
    final String dtdStr = "<!ELEMENT root EMPTY>\n"
        +"<!ATTLIST root attr CDATA #IMPLIED>\n";

    for (int i = 0; i < 3; ++i) {
        boolean nsAware = (i >= 1);
        boolean repairing = (i == 2);
        StringWriter strw = new StringWriter();

        XMLStreamWriter2 sw = getDTDValidatingWriter(strw, dtdStr, nsAware, repairing);

        sw.writeStartElement("root");
        // No content whatsoever is allowed...
        sw.writeEndElement();
        sw.writeEndDocument();
        sw.close();

        // Next; same but with an attribute
        sw = getDTDValidatingWriter(strw, dtdStr, nsAware, repairing);

        sw.writeStartElement("root");
        // no content, but attribute is fine
        sw.writeAttribute("attr", "value");

        sw.writeEndElement();
        sw.writeEndDocument();
        sw.close();

        // And then using empty element write method(s)
        sw = getDTDValidatingWriter(strw, dtdStr, nsAware, repairing);
        sw.writeEmptyElement("root");
        // note: empty element need/can not be closed
        sw.writeEndDocument();
        sw.close();

        // and finally empty with attribute
        sw = getDTDValidatingWriter(strw, dtdStr, nsAware, repairing);
        sw.writeEmptyElement("root");
        sw.writeAttribute("attr", "otherValue");
        sw.writeEndDocument();
        sw.close();
    }
}
 
Example 7
Source File: TestOutputValidation.java    From woodstox with Apache License 2.0 4 votes vote down vote up
public void testValidAnyContent()
    throws XMLStreamException
{
    final String dtdStr = "<!ELEMENT root ANY>\n"
        +"<!ATTLIST root attr CDATA #IMPLIED>\n"
        +"<!ELEMENT leaf ANY>\n"
            ;

    for (int i = 0; i < 3; ++i) {
        boolean nsAware = (i >= 1);
        boolean repairing = (i == 2);
        StringWriter strw = new StringWriter();

        // First simplest case
        XMLStreamWriter2 sw = getDTDValidatingWriter(strw, dtdStr, nsAware, repairing);
        sw.writeStartElement("root");
        sw.writeStartElement("leaf");
        sw.writeCharacters("whatever");
        sw.writeEndElement();
        sw.writeEndElement();
        sw.writeEndDocument();
        sw.close();

        // Then one with no content
        sw = getDTDValidatingWriter(strw, dtdStr, nsAware, repairing);
        sw.writeStartElement("root");
        sw.writeEndElement();
        sw.writeEndDocument();
        sw.close();

        // Then one with explicitly empty elem
        sw = getDTDValidatingWriter(strw, dtdStr, nsAware, repairing);
        sw.writeStartElement("root");
        sw.writeEmptyElement("leaf");
        sw.writeEndElement();
        sw.writeEndDocument();
        sw.close();

        // Then one with an attribute
        sw = getDTDValidatingWriter(strw, dtdStr, nsAware, repairing);
        sw.writeStartElement("root");
        sw.writeAttribute("attr", "value");
        sw.writeStartElement("leaf");
        sw.writeEndElement();
        sw.writeEndElement();
        sw.writeEndDocument();
        sw.close();
    }
}