Search in sources :

Example 81 with XMLStreamReader

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

the class TestIOExceptionInGetText method runTest.

@Override
protected void runTest() throws Throwable {
    // Construct a stream that will throw an exception in the middle of a text node.
    // We need to create a very large document, because some parsers (such as some
    // versions of XLXP) have a large input buffer and would throw an exception already
    // when the XMLStreamReader is created.
    StringBuffer xml = new StringBuffer("<root>");
    for (int i = 0; i < 100000; i++) {
        xml.append('x');
    }
    InputStream in = new ExceptionInputStream(new ByteArrayInputStream(xml.toString().getBytes("ASCII")));
    XMLStreamReader originalReader = StAXUtils.createXMLStreamReader(in);
    InvocationCounter invocationCounter = new InvocationCounter();
    XMLStreamReader reader = (XMLStreamReader) invocationCounter.createProxy(originalReader);
    OMElement element = OMXMLBuilderFactory.createStAXOMBuilder(metaFactory.getOMFactory(), reader).getDocumentElement();
    try {
        element.getNextOMSibling();
        fail("Expected exception");
    } catch (Exception ex) {
    // Expected
    }
    assertTrue(invocationCounter.getInvocationCount() > 0);
    invocationCounter.reset();
    try {
        element.getNextOMSibling();
        fail("Expected exception");
    } catch (Exception ex) {
    // Expected
    }
    assertEquals(0, invocationCounter.getInvocationCount());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ExceptionInputStream(org.apache.axiom.testutils.io.ExceptionInputStream) InputStream(java.io.InputStream) ExceptionInputStream(org.apache.axiom.testutils.io.ExceptionInputStream) OMElement(org.apache.axiom.om.OMElement) InvocationCounter(org.apache.axiom.testutils.InvocationCounter)

Example 82 with XMLStreamReader

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

the class TestCreateStAXOMBuilderIncorrectState method runTest.

@Override
protected void runTest() throws Throwable {
    XMLStreamReader reader = StAXUtils.createXMLStreamReader(new StringReader("<root>text</root>"));
    // Position the reader on a CHARACTERS event
    while (reader.getEventType() != XMLStreamReader.CHARACTERS) {
        reader.next();
    }
    try {
        OMXMLBuilderFactory.createStAXOMBuilder(metaFactory.getOMFactory(), reader);
        fail("Expected OMException");
    } catch (OMException ex) {
    // Expected
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StringReader(java.io.StringReader) OMException(org.apache.axiom.om.OMException)

Example 83 with XMLStreamReader

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

the class TestCreateStAXOMBuilderFromFragment method runTest.

@Override
protected void runTest() throws Throwable {
    XMLStreamReader reader = StAXUtils.createXMLStreamReader(new StringReader("<a><b>text</b></a>"));
    // Position the reader on the event for <b>
    while (reader.getEventType() != XMLStreamReader.START_ELEMENT || !reader.getLocalName().equals("b")) {
        reader.next();
    }
    // Check that the builder only builds the part of the document corresponding to <b>text</b>
    OMElement element = OMXMLBuilderFactory.createStAXOMBuilder(metaFactory.getOMFactory(), reader).getDocumentElement();
    assertEquals("b", element.getLocalName());
    OMNode child = element.getFirstOMChild();
    assertTrue(child instanceof OMText);
    assertNull(element.getNextOMSibling());
    // Check that the original reader is now positioned on the event just following </b>
    assertEquals(XMLStreamReader.END_ELEMENT, reader.getEventType());
    assertEquals("a", reader.getLocalName());
}
Also used : OMNode(org.apache.axiom.om.OMNode) XMLStreamReader(javax.xml.stream.XMLStreamReader) StringReader(java.io.StringReader) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement)

Example 84 with XMLStreamReader

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

the class TestRootPartStreaming method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    // Programmatically create the message
    OMElement orgRoot = factory.createOMElement("root", null);
    for (int i = 0; i < 10000; i++) {
        factory.createOMElement("child", null, orgRoot).setText("Some text content");
    }
    // Serialize the message as XOP even if there will be no attachment parts
    OMOutputFormat format = new OMOutputFormat();
    format.setDoOptimize(true);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    orgRoot.serialize(baos, format);
    // Parse the message and monitor the number of bytes read
    InstrumentedInputStream in = new InstrumentedInputStream(new ByteArrayInputStream(baos.toByteArray()));
    OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(factory, StAXParserConfiguration.DEFAULT, MultipartBody.builder().setInputStream(in).setContentType(format.getContentType()).build());
    OMElement root = builder.getDocumentElement();
    long count1 = in.getCount();
    XMLStreamReader reader = root.getXMLStreamReader(false);
    while (reader.hasNext()) {
        reader.next();
    }
    long count2 = in.getCount();
    // We expect that after requesting the document element, only a small part (corresponding to
    // the size of the parser buffer) should have been read:
    assertTrue(count1 < count2 / 2);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) XMLStreamReader(javax.xml.stream.XMLStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InstrumentedInputStream(org.apache.axiom.testutils.io.InstrumentedInputStream) OMElement(org.apache.axiom.om.OMElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper)

Example 85 with XMLStreamReader

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

the class TestCommentEvent method runTest.

@Override
protected void runTest() throws Throwable {
    OMXMLParserWrapper builder = builderFactory.getBuilder(metaFactory, new InputSource(new StringReader("<a><!--comment text--></a>")));
    OMElement element = builder.getDocumentElement();
    XMLStreamReader reader = element.getXMLStreamReader(cache);
    assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
    assertEquals(XMLStreamReader.COMMENT, reader.next());
    assertEquals("comment text", reader.getText());
    assertEquals("comment text", new String(reader.getTextCharacters(), reader.getTextStart(), reader.getTextLength()));
    StringBuffer text = new StringBuffer();
    char[] buf = new char[5];
    for (int sourceStart = 0; ; sourceStart += buf.length) {
        int nCopied = reader.getTextCharacters(sourceStart, buf, 0, buf.length);
        text.append(buf, 0, nCopied);
        if (nCopied < buf.length) {
            break;
        }
    }
    assertEquals("comment text", text.toString());
    element.close(false);
}
Also used : InputSource(org.xml.sax.InputSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) StringReader(java.io.StringReader) OMElement(org.apache.axiom.om.OMElement) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper)

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