Package org.codehaus.stax2

Examples of org.codehaus.stax2.XMLStreamWriter2


            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();
        }
    }
View Full Code Here


                break;
            }

            StringWriter strw = new StringWriter();

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

            /* The only obviously invalid cases are using non-declared
             * elements or attributes... so let's test them here (these
             * may be redundant to some degree)
             */
            sw.writeStartElement("root");
            try {
                sw.writeStartElement("unknown");
                fail(modeDesc+" Expected a validation exception when trying to add an undeclared element");
            } catch (XMLValidationException vex) {
                // expected...
            }
            sw.close();

            // undecl attr:
            sw = getDTDValidatingWriter(strw, dtdStr, nsAware, repairing);
            sw.writeStartElement("root");
            try {
                sw.writeAttribute("unknown", "value");
                fail(modeDesc+" Expected a validation exception when trying to add an undeclared attribute");
            } catch (XMLValidationException vex) {
                // expected...
            }
            sw.close();
        }
    }
View Full Code Here

            StringWriter strw = new StringWriter();
           
            /* Ok; can test for "wrong" root element only if we explicitly
             * output DOCTYPE declaration with specific name...
             */
            XMLStreamWriter2 sw = getDTDValidatingWriter(strw, SIMPLE_DTD, nsAware, repairing);
            sw.writeDTD("root", "http://foo", "public-id", SIMPLE_DTD);
            try {
                sw.writeStartElement("branch");
                fail(modeDesc+" Expected a validation exception when trying to write wrong root element");
            } catch (XMLValidationException vex) {
                // expected...
            }
            // should not continue after exception; state may not be valid
           
            // And then undeclared root:
            sw = getDTDValidatingWriter(strw, SIMPLE_DTD, nsAware, repairing);
            try {
                sw.writeStartElement("undefined");
                fail(modeDesc+" Expected a validation exception when trying to write an undefined root element");
            } catch (XMLValidationException vex) {
                // expected...
            }
           
            // and same for explicitly empty element; wrong root
            sw = getDTDValidatingWriter(strw, SIMPLE_DTD, nsAware, repairing);
            sw.writeDTD("root", "http://foo", "public-id", SIMPLE_DTD);
            try {
                sw.writeEmptyElement("branch");
                fail(modeDesc+" Expected a validation exception when trying to write wrong root element");
            } catch (XMLValidationException vex) {
                // expected...
            }
        }
View Full Code Here

        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
        }
    }
View Full Code Here

            StringWriter strw = new StringWriter();
           
            // Let's try omitting the end element, first...

            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.writeCharacters("\n"); // imitating indentation
            try {
                sw.writeEndElement(); // for root
                fail(modeDesc+" Expected a validation exception when omitting non-optional <end> element");
            } catch (XMLValidationException vex) {
                // expected...
            }
            // should not continue after exception; state may not be valid

            // And then leaving out branch...
            sw = getDTDValidatingWriter(strw, SIMPLE_DTD, nsAware, repairing);
            sw.writeStartElement("root");
            sw.writeCharacters("  "); // imitating indentation
            sw.writeComment("comment");
            try {
                sw.writeEmptyElement("end");
                fail(modeDesc+" Expected a validation exception when omitting non-optional <branch> element");
            } catch (XMLValidationException vex) {
                // expected...
            }
        }
View Full Code Here

    {
        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();
        }
    }
View Full Code Here

            StringWriter strw = new StringWriter();
           
            // Let's try omitting the end element, first...

            XMLStreamWriter2 sw = getDTDValidatingWriter(strw, SIMPLE_NS_DTD, true, repairing);
            // prefix, local name, uri (for elems)
            try {
                sw.writeStartElement(NS_PREFIX2, "root", NS_URI);
                fail(modeDesc+" Expected a validation exception when passing wrong (unexpected) ns for element");
            } catch (XMLValidationException vex) {
                // expected...
            }
            // should not continue after exception; state may not be valid

            // and then the same for empty elem
            sw = getDTDValidatingWriter(strw, SIMPLE_NS_DTD, true, repairing);
            // prefix, local name, uri (for elems)
            try {
                sw.writeEmptyElement(NS_PREFIX2, NS_URI, "root");
                fail(modeDesc+" Expected a validation exception when passing wrong (unexpected) ns for element");
            } catch (XMLValidationException vex) {
                // expected...
            }

            // Oh, and finally, using non-ns DTD:
            sw = getDTDValidatingWriter(strw, SIMPLE_DTD, true, repairing);
            // prefix, local name, uri (for elems)
            try {
                sw.writeEmptyElement(NS_PREFIX, NS_URI, "root");
                fail(modeDesc+" Expected a validation exception when passing wrong (unexpected) ns for element");
            } catch (XMLValidationException vex) {
                // expected...
            }
        }
View Full Code Here

            boolean nsAware = (i >= 1);
            boolean repairing = (i == 2);
            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();
        }
    }
View Full Code Here

            // Invalid case, trying to add some other value:

            // non-empty but not same
            StringWriter strw = new StringWriter();
            XMLStreamWriter2 sw = getDTDValidatingWriter(strw, FIXED_DTD_STR, nsAware, repairing);
            sw.writeStartElement("root");
            try {
                sw.writeAttribute("fixAttr", "otherValue");
                fail(modeDesc+" Expected a validation exception when trying to add a #FIXED attribute with 'wrong' value");
            } catch (XMLValidationException vex) {
                // expected...
            }
            // Should not close, since stream is invalid now...

            // empty is not the same as leaving it out:
            strw = new StringWriter();
            sw = getDTDValidatingWriter(strw, FIXED_DTD_STR, nsAware, repairing);
            sw.writeStartElement("root");
            try {
                sw.writeAttribute("fixAttr", "");
                fail(modeDesc+" Expected a validation exception when trying to add a #FIXED attribute with an empty value");
            } catch (XMLValidationException vex) {
                // expected...
            }

            // And finally, same for empty elem in case impl. is different
            strw = new StringWriter();
            sw = getDTDValidatingWriter(strw, FIXED_DTD_STR, nsAware, repairing);
            sw.writeEmptyElement("root");
            try {
                sw.writeAttribute("fixAttr", "foobar");
                fail(modeDesc+" Expected a validation exception when trying to add a #FIXED attribute with an empty value");
            } catch (XMLValidationException vex) {
                // expected...
            }
        }
View Full Code Here

            boolean nsAware = (i >= 1);
            boolean repairing = (i == 2);
            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();
        }
    }
View Full Code Here

TOP

Related Classes of org.codehaus.stax2.XMLStreamWriter2

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.