Search in sources :

Example 56 with XMLInputFactory

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

the class TestGetVersion method runTest.

protected void runTest() throws Throwable {
    XMLInputFactory factory = staxImpl.newNormalizedXMLInputFactory();
    XMLStreamReader reader = factory.createXMLStreamReader(new StringReader("<?xml version='1.0'?><root/>"));
    assertEquals("1.0", reader.getVersion());
    reader.next();
    try {
        reader.getVersion();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException ex) {
    // Expected
    }
    reader.close();
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StringReader(java.io.StringReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 57 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory 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 58 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory 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 59 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory 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 60 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory 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)

Aggregations

XMLInputFactory (javax.xml.stream.XMLInputFactory)182 XMLStreamReader (javax.xml.stream.XMLStreamReader)114 XMLStreamException (javax.xml.stream.XMLStreamException)74 InputStream (java.io.InputStream)54 StringReader (java.io.StringReader)45 IOException (java.io.IOException)40 XMLEventReader (javax.xml.stream.XMLEventReader)36 ByteArrayInputStream (java.io.ByteArrayInputStream)30 Test (org.junit.Test)29 InputStreamReader (java.io.InputStreamReader)19 Unmarshaller (javax.xml.bind.Unmarshaller)18 XMLEvent (javax.xml.stream.events.XMLEvent)15 StAXSource (javax.xml.transform.stax.StAXSource)15 StreamSource (javax.xml.transform.stream.StreamSource)15 ArrayList (java.util.ArrayList)14 JAXBException (javax.xml.bind.JAXBException)14 HashMap (java.util.HashMap)13 DOMSource (javax.xml.transform.dom.DOMSource)12 StartElement (javax.xml.stream.events.StartElement)10 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)10