Search in sources :

Example 6 with OMXMLParserWrapper

use of org.apache.axiom.om.OMXMLParserWrapper in project webservices-axiom by apache.

the class TestDetachWithSAXSource method runTest.

@Override
protected void runTest() throws Throwable {
    DummyXMLReader xmlReader = new DummyXMLReader();
    OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), new SAXSource(xmlReader, new InputSource()), false);
    assertThat(xmlReader.isParsed()).isFalse();
    builder.detach();
    assertThat(xmlReader.isParsed()).isTrue();
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper)

Example 7 with OMXMLParserWrapper

use of org.apache.axiom.om.OMXMLParserWrapper in project webservices-axiom by apache.

the class TestGetDocumentElement method runTest.

@Override
protected void runTest() throws Throwable {
    OMXMLParserWrapper builder = builderFactory.getBuilder(metaFactory, new InputSource(new StringReader("<!--comment1--><root/><!--comment2-->")));
    OMElement element;
    if (discardDocument == null) {
        element = builder.getDocumentElement();
    } else {
        element = builder.getDocumentElement(discardDocument.booleanValue());
    }
    assertNotNull("Document element can not be null", element);
    assertEquals("Name of the document element is wrong", "root", element.getLocalName());
    if (Boolean.TRUE.equals(discardDocument)) {
        if (builderFactory.isDeferredParsing()) {
            assertFalse(element.isComplete());
        }
        assertNull(element.getParent());
        // Note: we can't test getNextOMSibling here because this would build the element
        assertNull(element.getPreviousOMSibling());
        OMElement newParent = element.getOMFactory().createOMElement("newParent", null);
        newParent.addChild(element);
        if (builderFactory.isDeferredParsing()) {
            assertFalse(element.isComplete());
            assertFalse(builder.isCompleted());
        }
        assertAbout(xml()).that(xml(OMElement.class, newParent)).hasSameContentAs("<newParent><root/></newParent>");
        assertTrue(element.isComplete());
        // Since we discarded the document, the nodes in the epilog will not be accessible.
        // Therefore we expect that when the document element changes its completion status,
        // the builder will consume the epilog and change its completion status as well.
        // This gives the underlying parser a chance to release some resources.
        assertTrue(builder.isCompleted());
    } else {
        // The getDocumentElement doesn't detach the document element from the document:
        assertSame(builder.getDocument(), element.getParent());
        assertSame(builder.getDocument().getOMDocumentElement(), element);
        assertTrue(element.getPreviousOMSibling() instanceof OMComment);
        assertTrue(element.getNextOMSibling() instanceof OMComment);
    }
}
Also used : InputSource(org.xml.sax.InputSource) OMComment(org.apache.axiom.om.OMComment) StringReader(java.io.StringReader) OMElement(org.apache.axiom.om.OMElement) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper)

Example 8 with OMXMLParserWrapper

use of org.apache.axiom.om.OMXMLParserWrapper in project webservices-axiom by apache.

the class TestGetDocumentElementWithDiscardDocumentIllFormedEpilog method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(factory, new StringReader("<root/> there shouldn't be text here!"));
    OMElement element = builder.getDocumentElement(true);
    try {
        element.build();
        fail("Expected OMException");
    } catch (OMException ex) {
    // Expected
    }
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) StringReader(java.io.StringReader) OMElement(org.apache.axiom.om.OMElement) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper) OMException(org.apache.axiom.om.OMException)

Example 9 with OMXMLParserWrapper

use of org.apache.axiom.om.OMXMLParserWrapper 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 10 with OMXMLParserWrapper

use of org.apache.axiom.om.OMXMLParserWrapper 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

OMXMLParserWrapper (org.apache.axiom.om.OMXMLParserWrapper)37 OMElement (org.apache.axiom.om.OMElement)21 StringReader (java.io.StringReader)12 XMLStreamReader (javax.xml.stream.XMLStreamReader)8 OMFactory (org.apache.axiom.om.OMFactory)8 InputSource (org.xml.sax.InputSource)8 OMNode (org.apache.axiom.om.OMNode)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 InputStream (java.io.InputStream)4 QName (javax.xml.namespace.QName)3 OMAttribute (org.apache.axiom.om.OMAttribute)3 OMDocument (org.apache.axiom.om.OMDocument)3 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)3 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)3 DOMSource (javax.xml.transform.dom.DOMSource)2 SAXSource (javax.xml.transform.sax.SAXSource)2 NodeUnavailableException (org.apache.axiom.om.NodeUnavailableException)2 OMException (org.apache.axiom.om.OMException)2 OMNamespace (org.apache.axiom.om.OMNamespace)2