org.codehaus.stax2.XMLStreamWriter2 Java Examples

The following examples show how to use org.codehaus.stax2.XMLStreamWriter2. 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: TestStructuralValidation.java    From woodstox with Apache License 2.0 6 votes vote down vote up
public void testValidStructure()
    throws XMLStreamException
{
    for (int i = 0; i < 3; ++i) {
        boolean nsAware = (i >= 1);
        boolean repairing = (i == 2);

        StringWriter strw = new StringWriter();
        XMLStreamWriter2 sw = getDTDValidatingWriter(strw, SIMPLE_DTD, nsAware, repairing);
        sw.writeStartElement("root");
        sw.writeCharacters("  "); // imitating indentation
        sw.writeStartElement("branch");
        sw.writeEndElement();
        sw.writeStartElement("branch");
        sw.writeCharacters("test");
        sw.writeComment("comment");
        sw.writeEndElement();
        sw.writeEmptyElement("branch");
        sw.writeEmptyElement("end");
        sw.writeAttribute("endAttr", "value");
        sw.writeCharacters("\n"); // imitating indentation
        sw.writeEndElement(); // for root
    }
}
 
Example #2
Source File: Stax2ValidationUtils.java    From cxf with Apache License 2.0 6 votes vote down vote up
public boolean setupValidation(XMLStreamWriter writer, Endpoint endpoint, ServiceInfo serviceInfo)
        throws XMLStreamException {

    XMLStreamWriter2 writer2 = (XMLStreamWriter2) writer;
    XMLValidationSchema vs = getValidator(endpoint, serviceInfo);
    if (vs == null) {
        return false;
    }
    writer2.setValidationProblemHandler(new ValidationProblemHandler() {

        public void reportProblem(XMLValidationProblem problem) throws XMLValidationException {
            throw new Fault(problem.getMessage(), LOG);
        }
    });
    writer2.validateAgainst(vs);
    return true;
}
 
Example #3
Source File: TestOutputValidation.java    From woodstox with Apache License 2.0 6 votes vote down vote up
public void testInvalidMixedContent()
    throws XMLStreamException
{
    final String dtdStr =
        "<!ELEMENT root (branch)>\n"
        +"<!ELEMENT branch ANY>\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");
        // Should get validation exception here:
        try {
            sw.writeCharacters("Illegal text!");
            fail("Expected a validation exception for non-whitespace text output on non-mixed element content");
        } catch (XMLValidationException vex) {
            // expected...
        }
    }
}
 
Example #4
Source File: TestCopyEventFromReader97.java    From woodstox with Apache License 2.0 6 votes vote down vote up
public void testUTF8MsLinefeedCopyEvent() throws Exception
{
    final XMLInputFactory2 xmlIn = getInputFactory();
    final XMLOutputFactory2 xmlOut = getOutputFactory();
    InputStream in = getClass().getResource("issue97.xml").openStream();

    ByteArrayOutputStream bogus = new ByteArrayOutputStream();
    XMLStreamReader2 reader = (XMLStreamReader2) xmlIn.createXMLStreamReader(in);
    XMLStreamWriter2 writer = (XMLStreamWriter2) xmlOut.createXMLStreamWriter(bogus, "UTF-8");
    while (reader.hasNext()) {
       reader.next();
       writer.copyEventFromReader(reader, false);
    }

    in.close();
}
 
Example #5
Source File: TestOutputValidation.java    From woodstox with Apache License 2.0 6 votes vote down vote up
public void testValidMixedContent()
    throws XMLStreamException
{
    final String dtdStr =
        "<!ELEMENT root (#PCDATA | branch)*>\n"
        +"<!ELEMENT branch (branch)*>\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");
        // Should be fine now
        sw.writeCharacters("Text that should be ok");
        sw.writeStartElement("branch");
        // Also, all-whitespace is ok in non-mixed too
        sw.writeCharacters("\t \t   \r   \n");
        sw.writeEndElement();
        sw.writeEndElement();
        sw.writeEndDocument();
    }
}
 
Example #6
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 #7
Source File: TestAttributeValidation.java    From woodstox with Apache License 2.0 6 votes vote down vote up
private void _testInvalidNsAttr(boolean repairing) throws XMLStreamException
{
    String modeDesc = "[namespace-aware, repairing? "+repairing+"]";

    // Invalid case, trying to use "wrong" prefix:

    StringWriter strw = new StringWriter();
    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!)
    try {
        sw.writeAttribute(NS_PREFIX2, NS_URI, "attr", "value");
        fail(modeDesc+" Expected a validation exception when trying to add an attribute with wrong ns prefix");
    } catch (XMLValidationException vex) {
        // expected...
    }
    // Should not close, since stream is invalid now...
}
 
Example #8
Source File: TestAttributeValidation.java    From woodstox with Apache License 2.0 6 votes vote down vote up
public void _testInvalidRequiredAttr(boolean nsAware, boolean repairing)
    throws XMLStreamException
{
    String modeDesc = String.format("[ns-aware? %s, repairing? %s]", nsAware, repairing);

    // Invalid case: leaving the required attr out:
    StringWriter strw = new StringWriter();
    XMLStreamWriter2 sw = getDTDValidatingWriter(strw, REQUIRED_DTD_STR, nsAware, repairing);
    sw.writeStartElement("root");
    try {
        sw.writeEndElement();
        fail(modeDesc+" Expected a validation exception when omitting a #REQUIRED attribute");
    } catch (XMLValidationException vex) {
        // expected...
    }
    // Should not close, since stream is invalid now...
}
 
Example #9
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 #10
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 #11
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 #12
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 #13
Source File: BaseStartElement.java    From woodstox with Apache License 2.0 5 votes vote down vote up
@Override
public void writeUsing(XMLStreamWriter2 w) throws XMLStreamException
{
    QName n = mName;
    w.writeStartElement(n.getPrefix(), n.getLocalPart(),
                        n.getNamespaceURI());
    outputNsAndAttr(w);
}
 
Example #14
Source File: WEntityDeclaration.java    From woodstox with Apache License 2.0 5 votes vote down vote up
/**
 * This method does not make much sense for this event type -- the reason
 * being that the entity declarations can only be written as part of
 * a DTD (internal or external subset), not separately. Can basically
 * choose to either skip silently (output nothing), or throw an
 * exception.
 */
@Override
public void writeUsing(XMLStreamWriter2 w) throws XMLStreamException
{
    /* Fail silently, or throw an exception? Let's do latter; at least
     * then we'll get useful (?) bug reports!
     */
    throw new XMLStreamException("Can not write entity declarations using an XMLStreamWriter");
}
 
Example #15
Source File: WstxOutputFactory.java    From woodstox with Apache License 2.0 5 votes vote down vote up
/**
 * Called by {@link #createSW(OutputStream, Writer, String, boolean)} after all of the nessesary configuration
 * logic is complete.
 */
protected XMLStreamWriter2 createSW(String enc, WriterConfig cfg, XmlWriter xw) {
    if (cfg.willSupportNamespaces()) {
        if (cfg.automaticNamespacesEnabled()) {
            return new RepairingNsStreamWriter(xw, enc, cfg);
        }
        return new SimpleNsStreamWriter(xw, enc, cfg);
    }
    return new NonNsStreamWriter(xw, enc, cfg);
}
 
Example #16
Source File: WstxOutputFactory.java    From woodstox with Apache License 2.0 5 votes vote down vote up
@Override
public XMLEventWriter createXMLEventWriter(XMLStreamWriter sw)
    throws XMLStreamException
{
    XMLStreamWriter2 sw2 = Stax2WriterAdapter.wrapIfNecessary(sw);
    return new Stax2EventWriterImpl(sw2);
}
 
Example #17
Source File: Log4jXmlPrettyPrinter.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public void writePrologLinefeed(final XMLStreamWriter2 sw) throws XMLStreamException {
    // nothing
}
 
Example #18
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();
    }
}
 
Example #19
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 #20
Source File: WstxOutputFactory.java    From woodstox with Apache License 2.0 4 votes vote down vote up
@Override
public XMLStreamWriter2 createXMLStreamWriter(Writer w, String enc)
    throws XMLStreamException
{
    return createSW(null, w, enc, false);
}