Package org.codehaus.stax2

Examples of org.codehaus.stax2.XMLEventReader2


    }

    public void testPropertiesStreamReader() throws XMLStreamException
    {
        XMLInputFactory f = getInputFactory();
        XMLStreamReader2 r = (XMLStreamReader2) f.createXMLStreamReader(new StringReader("<root></root>"));
       
        // First, verify property is indeed unsupported
        assertFalse(r.isPropertySupported(NO_SUCH_PROPERTY));

        /* Ok: as of Woodstox 4.0, behavior is such that no exception is thrown,
         * because javadocs do not indicate that it should be done (save for case
         * where property name is null). Whether this is right interpretation or not
         * is open to discussion; but for now we will verify that behavior does not
         * change from 4.0 without explicit decision.
         */
        /*
        try {
            Object ob = r.getProperty(NO_SUCH_PROPERTY);
            fail("Expected exception, instead got result: "+ob);
        } catch (IllegalArgumentException e) {
            verifyException(e, NO_SUCH_PROPERTY);
        }
        */
        Object ob = r.getProperty(NO_SUCH_PROPERTY);
        assertNull(ob);

        // And although setter is specified by Stax2, it too fails on unrecognized:
        try {
            r.setProperty(NO_SUCH_PROPERTY, "foobar");
            fail("Expected exception");
        } catch (IllegalArgumentException e) {
            verifyException(e, NO_SUCH_PROPERTY);
        }
    }
View Full Code Here


    String runMe = System.getProperty("testWsdlValidation");
    if (runMe == null || "".equals(runMe)) {
      return;
    }
    XMLInputFactory2 factory = getInputFactory();
    XMLStreamReader2 reader = (XMLStreamReader2) factory.createXMLStreamReader(getClass().getResourceAsStream("test-message.xml"), "utf-8");
    QName msgQName = new QName("http://server.hw.demo/", "sayHi");
    while (true) {
      int what = reader.nextTag();
      if (what == XMLStreamConstants.START_ELEMENT) {
        if (reader.getName().equals(msgQName)) {
          reader.validateAgainst(schema);
        }
      } else if (what == XMLStreamConstants.END_ELEMENT) {
        if (reader.getName().equals(msgQName)) {
          reader.stopValidatingAgainst(schema);
        }
      } else if (what == XMLStreamConstants.END_DOCUMENT) {
        break;
      }
    }
View Full Code Here

    private void doTestXmlId(boolean xmlidEnabled,
                             boolean nsAware, boolean coal)
        throws XMLStreamException
    {
        XMLStreamReader2 sr = getReader(XML_WITH_XMLID, xmlidEnabled, nsAware, coal);
        final String xmlidType = xmlidEnabled ? "ID" : "CDATA";

        // root:
        assertTokenType(START_ELEMENT, sr.next());
        assertEquals(1, sr.getAttributeCount());
        assertEquals("id", sr.getAttributeLocalName(0));
        assertEquals("CDATA", sr.getAttributeType(0));
        assertEquals(-1, sr.getAttributeInfo().getIdAttributeIndex());

        // leaf#1:
        assertTokenType(START_ELEMENT, sr.next());
        assertEquals(1, sr.getAttributeCount());
        assertEquals("abc", sr.getAttributeValue(0));
        if (xmlidEnabled) {
            assertEquals(0, sr.getAttributeInfo().getIdAttributeIndex());
        } else {
            assertEquals(-1, sr.getAttributeInfo().getIdAttributeIndex());
        }
        assertEquals(xmlidType, sr.getAttributeType(0));
        assertTokenType(END_ELEMENT, sr.next());

        // leaf#2:
        assertTokenType(START_ELEMENT, sr.next());
        assertEquals(1, sr.getAttributeCount());
        assertEquals("foobar", sr.getAttributeValue(0));
        assertEquals("id", sr.getAttributeLocalName(0));
        assertEquals(-1, sr.getAttributeInfo().getIdAttributeIndex());
        assertEquals("CDATA", sr.getAttributeType(0));
        assertTokenType(END_ELEMENT, sr.next());

        // leaf#3:
        assertTokenType(START_ELEMENT, sr.next());
        assertEquals(1, sr.getAttributeCount());
        assertEquals(xmlidType, sr.getAttributeType(0));
        if (xmlidEnabled) {
            assertEquals(0, sr.getAttributeInfo().getIdAttributeIndex());
        } else {
            assertEquals(-1, sr.getAttributeInfo().getIdAttributeIndex());
        }

        // also, should be normalized:
        if (xmlidEnabled) {
            assertEquals("_otherId", sr.getAttributeValue(0));
        } else {
            assertEquals("  _otherId ", sr.getAttributeValue(0));
        }
        assertTokenType(END_ELEMENT, sr.next());

        sr.close();
    }
View Full Code Here

    }

    private void doTestInvalid(boolean nsAware, boolean coal)
        throws XMLStreamException
    {
        XMLStreamReader2 sr = getValidatingReader(XML_WITH_XMLID_INVALID, nsAware, coal);
        try {
            assertTokenType(DTD, sr.next());
            assertTokenType(START_ELEMENT, sr.next());
            fail("Expected a validation exception for invalid Xml:id attribute declaration");
        } catch (XMLValidationException vex) {
            //System.err.println("VLD exc -> "+vex);
        }
    }
View Full Code Here

        throws XMLStreamException
    {
        /* In non-validating mode, shouldn't matter: but just to make sure,
         * let's also disable xml:id processing
         */
        XMLStreamReader2 sr = getReader(XML_WITH_XMLID_INVALID, false, nsAware, coal);
        assertTokenType(DTD, sr.next());
        assertTokenType(START_ELEMENT, sr.next());
    }
View Full Code Here

            ;
        /* Let's also add trailing CDATA, to ensure no coalescing is done
         * when not requested
         */
        String XML = "<root>" + CONTENT_IN + "<![CDATA[cdata]]></root>";
        XMLStreamReader2 sr = getReader(XML, false);
        assertTokenType(START_ELEMENT, sr.next());
        assertTokenType(CHARACTERS, sr.next());
        StringWriter sw = new StringWriter();
        sr.getText(sw, false);
        String act = sw.toString();
        if (!act.equals(CONTENT_OUT)) {
            if (CONTENT_OUT.startsWith(act)) {
                fail("Streaming text accessors returned partial match; first "
                     +act.length()+" chars of the expected "
                     +CONTENT_OUT.length()+" chars");
            }
            fail("Content accessed using streaming text accessor (len "
                     +act.length()+"; exp "+CONTENT_OUT.length()+" chars) wrong: "
                 +"expected ["+CONTENT_OUT+"], got ["+act+"]");
        }

        // And should get the following CDATA, then:
        assertTokenType(CDATA, sr.next());
        // and then closing element; let's not check CDATA contents here
        assertTokenType(END_ELEMENT, sr.next());
    }
View Full Code Here

            ;
        /* Let's also add trailing text, to ensure no coalescing is done
         * when not requested
         */
        String XML = "<root><![CDATA[" + CONTENT_INOUT + "]]>some text!</root>";
        XMLStreamReader2 sr = getReader(XML, false);
        assertTokenType(START_ELEMENT, sr.next());
        assertTokenType(CDATA, sr.next());
        StringWriter sw = new StringWriter();
        sr.getText(sw, false);
        String act = sw.toString();
        if (!act.equals(CONTENT_INOUT)) {
            if (CONTENT_INOUT.startsWith(act)) {
                fail("Streaming text accessors returned partial match; first "
                     +act.length()+" chars of the expected "
                     +CONTENT_INOUT.length()+" chars");
            }
            fail("Content accessed using streaming text accessor (len "
                     +act.length()+"; exp "+CONTENT_INOUT.length()+" chars) wrong: "
                 +"expected ["+CONTENT_INOUT+"], got ["+act+"]");
        }

        // And should get the following CHARACTERS then:
        assertTokenType(CHARACTERS, sr.next());
        // and then closing element; let's not check text contents here
        assertTokenType(END_ELEMENT, sr.next());
    }
View Full Code Here

        String CONTENT_OUT2 = "Then CDATA and text...\nneat-o!";

        for (int i = 0; i < 2; ++i) {
            boolean first = (i == 0);
            String XML = "<root>" + (first ? CONTENT_IN1 : CONTENT_IN2) + "</root>";
            XMLStreamReader2 sr = getReader(XML, true);
            assertTokenType(START_ELEMENT, sr.next());
            assertTokenType(CHARACTERS, sr.next());
            StringWriter sw = new StringWriter();
            sr.getText(sw, false);
            String act = sw.toString();
            String exp = first ? CONTENT_OUT1 : CONTENT_OUT2;
            if (!act.equals(exp)) {
                if (exp.startsWith(act)) {
                    fail("Streaming text accessors returned partial match; first "
                         +act.length()+" chars of the expected "
                         +exp.length()+" chars");
                }
                fail("Content accessed using streaming text accessor (len "
                     +act.length()+"; exp "+exp.length()+" chars) wrong: "
                     +"expected ["+exp+"], got ["+act+"]");
            }
            // and then closing element
            assertTokenType(END_ELEMENT, sr.next());
        }
    }
View Full Code Here

    }
   
    protected void verifyFailure(String xml, XMLValidationSchema schema, String failMsg,
                                 String failPhrase, boolean strict) throws XMLStreamException
    {
        XMLStreamReader2 sr = constructStreamReader(getInputFactory(), xml);
        sr.validateAgainst(schema);
        try {
            while (sr.hasNext()) {
                /* int type = */sr.next();
            }
            fail("Expected validity exception for " + failMsg);
        } catch (XMLValidationException vex) {
            String origMsg = vex.getMessage();
            String msg = (origMsg == null) ? "" : origMsg.toLowerCase();
View Full Code Here

            +"<root>"
            +"<xy:elem xmlns:xy='http://foo' xmlns:another='http://x'>"
            +"<?proc instr?>&intEnt;<another:x /></xy:elem>"
            +"</root>"
            ;
        XMLStreamReader2 sr = getReader(XML, ns);
        try {
            assertTokenType(DTD, sr.next());
            assertEquals("root", sr.getPrefixedName());
            assertTokenType(START_ELEMENT, sr.next());
            assertEquals("root", sr.getPrefixedName());
            assertTokenType(START_ELEMENT, sr.next());
            assertEquals("xy:elem", sr.getPrefixedName());
            assertTokenType(PROCESSING_INSTRUCTION, sr.next());
            assertEquals("proc", sr.getPrefixedName());
            assertTokenType(START_ELEMENT, sr.next());
            assertEquals("leaf", sr.getPrefixedName());
            assertTokenType(END_ELEMENT, sr.next());
            assertEquals("leaf", sr.getPrefixedName());
            assertTokenType(START_ELEMENT, sr.next());
            assertEquals("another:x", sr.getPrefixedName());
            assertTokenType(END_ELEMENT, sr.next());
            assertEquals("another:x", sr.getPrefixedName());
            assertTokenType(END_ELEMENT, sr.next());
            assertEquals("xy:elem", sr.getPrefixedName());
            assertTokenType(END_ELEMENT, sr.next());
            assertEquals("root", sr.getPrefixedName());
            assertTokenType(END_DOCUMENT, sr.next());
        } catch (XMLStreamException xse) {
            fail("Did not expect any problems during parsing, but got: "+xse);
        }
    }
View Full Code Here

TOP

Related Classes of org.codehaus.stax2.XMLEventReader2

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.