Search in sources :

Example 66 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project webservices-axiom by apache.

the class TestIsCharactersOnCDATASection method runTest.

protected void runTest() throws Throwable {
    XMLInputFactory factory = staxImpl.newNormalizedXMLInputFactory();
    factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    XMLStreamReader reader = factory.createXMLStreamReader(new StringReader("<root><![CDATA[X]]></root>"));
    reader.nextTag();
    reader.next();
    assertTrue(reader.isCharacters());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StringReader(java.io.StringReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 67 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project webservices-axiom by apache.

the class TestNextAfterEndDocument method runTest.

protected void runTest() throws Throwable {
    XMLInputFactory factory = staxImpl.newNormalizedXMLInputFactory();
    XMLStreamReader reader = factory.createXMLStreamReader(new StringReader("<root/>"));
    while (reader.next() != XMLStreamReader.END_DOCUMENT) {
    // Just loop
    }
    try {
        reader.next();
        fail("Expected exception");
    } catch (IllegalStateException ex) {
    // Expected
    } catch (NoSuchElementException ex) {
    // This is also OK
    } catch (XMLStreamException ex) {
        fail("Expected IllegalStateException or NoSuchElementException");
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) NoSuchElementException(java.util.NoSuchElementException)

Example 68 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project webservices-axiom by apache.

the class TestCreateXMLStreamReaderThreadSafety method runTest.

protected void runTest() throws Throwable {
    final XMLInputFactory factory = staxImpl.getDialect().makeThreadSafe(staxImpl.newNormalizedXMLInputFactory());
    ConcurrentTestUtils.testThreadSafety(new Action() {

        public void execute() throws Exception {
            String text = String.valueOf((int) (Math.random() * 10000));
            String xml = "<root>" + text + "</root>";
            XMLStreamReader reader = factory.createXMLStreamReader(new StringReader(xml));
            assertEquals(XMLStreamReader.START_DOCUMENT, reader.getEventType());
            assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
            assertEquals(XMLStreamReader.CHARACTERS, reader.next());
            assertEquals(text, reader.getText());
            assertEquals(XMLStreamReader.END_ELEMENT, reader.next());
            assertEquals(XMLStreamReader.END_DOCUMENT, reader.next());
            reader.close();
        }
    });
}
Also used : Action(org.apache.axiom.testutils.concurrent.Action) XMLStreamReader(javax.xml.stream.XMLStreamReader) StringReader(java.io.StringReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 69 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project webservices-axiom by apache.

the class TestDisallowDoctypeDeclWithInternalSubset method runTest.

protected void runTest() throws Throwable {
    XMLInputFactory factory = staxImpl.newNormalizedXMLInputFactory();
    factory = staxImpl.getDialect().disallowDoctypeDecl(factory);
    boolean gotException = false;
    boolean reachedDocumentElement = false;
    try {
        XMLStreamReader reader = factory.createXMLStreamReader(new StringReader("<?xml version='1.0'?><!DOCTYPE root []><root/>"));
        try {
            while (reader.hasNext()) {
                if (reader.next() == XMLStreamConstants.START_ELEMENT) {
                    reachedDocumentElement = true;
                }
            }
        } finally {
            reader.close();
        }
    } catch (XMLStreamException ex) {
        gotException = true;
    } catch (RuntimeException ex) {
        gotException = true;
    }
    assertTrue("Expected exception", gotException);
    assertFalse("The parser failed to throw an exception before reaching the document element", reachedDocumentElement);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 70 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project webservices-axiom by apache.

the class TestGetAttributeNamespaceWithNoPrefix method runTest.

protected void runTest() throws Throwable {
    XMLInputFactory factory = staxImpl.newNormalizedXMLInputFactory();
    XMLStreamReader reader = factory.createXMLStreamReader(new StringReader("<root attr=\"test\"><child xmlns=\"urn:ns\" attr=\"test\"/></root>"));
    int eventType;
    while ((eventType = reader.next()) != XMLStreamReader.END_DOCUMENT) {
        if (eventType == XMLStreamReader.START_ELEMENT) {
            for (int i = 0; i < reader.getAttributeCount(); i++) {
                assertNull(reader.getAttributeNamespace(i));
            }
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StringReader(java.io.StringReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

XMLStreamReader (javax.xml.stream.XMLStreamReader)243 XMLInputFactory (javax.xml.stream.XMLInputFactory)98 StringReader (java.io.StringReader)85 XMLStreamException (javax.xml.stream.XMLStreamException)78 InputStream (java.io.InputStream)61 IOException (java.io.IOException)43 OMElement (org.apache.axiom.om.OMElement)37 ByteArrayInputStream (java.io.ByteArrayInputStream)27 Test (org.junit.Test)25 JAXBException (javax.xml.bind.JAXBException)16 QName (javax.xml.namespace.QName)16 StAXSource (javax.xml.transform.stax.StAXSource)16 StreamSource (javax.xml.transform.stream.StreamSource)16 FileInputStream (java.io.FileInputStream)14 OMFactory (org.apache.axiom.om.OMFactory)14 Unmarshaller (javax.xml.bind.Unmarshaller)13 InputStreamReader (java.io.InputStreamReader)12 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)12 Source (javax.xml.transform.Source)11 InputSource (org.xml.sax.InputSource)11