Search in sources :

Example 91 with OMElement

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

the class TestSerializeAndConsumeWithIncompleteDescendant method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMElement incompleteElement = OMXMLBuilderFactory.createOMBuilder(factory, new StringReader("<elem>text</elem>")).getDocumentElement(true);
    OMDocument document = factory.createOMDocument();
    OMElement root = factory.createOMElement("root", null, document);
    root.addChild(incompleteElement);
    StringWriter out = new StringWriter();
    document.serializeAndConsume(out);
    assertAbout(xml()).that(out.toString()).hasSameContentAs("<root><elem>text</elem></root>");
    assertConsumed(incompleteElement);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) OMElement(org.apache.axiom.om.OMElement) OMDocument(org.apache.axiom.om.OMDocument)

Example 92 with OMElement

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

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

the class TestDetachWithDOM method runTest.

@Override
protected void runTest() throws Throwable {
    Document document = DOMImplementation.XERCES.newDocument();
    Element root = document.createElementNS("", "root");
    root.appendChild(document.createElementNS("", "a"));
    OMXMLParserWrapper builder;
    if (useDOMSource) {
        builder = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), new DOMSource(root));
    } else {
        builder = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), root, false);
    }
    OMElement omRoot = builder.getDocumentElement();
    builder.detach();
    root.appendChild(document.createElementNS("", "b"));
    assertThat(omRoot).hasNumberOfChildren(1);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) OMElement(org.apache.axiom.om.OMElement) Element(org.w3c.dom.Element) OMElement(org.apache.axiom.om.OMElement) Document(org.w3c.dom.Document) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper)

Example 94 with OMElement

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

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

Aggregations

OMElement (org.apache.axiom.om.OMElement)414 OMFactory (org.apache.axiom.om.OMFactory)202 OMNamespace (org.apache.axiom.om.OMNamespace)108 QName (javax.xml.namespace.QName)97 StringReader (java.io.StringReader)63 OMNode (org.apache.axiom.om.OMNode)43 OMText (org.apache.axiom.om.OMText)41 XMLStreamReader (javax.xml.stream.XMLStreamReader)37 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)32 OMAttribute (org.apache.axiom.om.OMAttribute)26 StringWriter (java.io.StringWriter)24 OMXMLParserWrapper (org.apache.axiom.om.OMXMLParserWrapper)21 DataHandler (javax.activation.DataHandler)20 OMDocument (org.apache.axiom.om.OMDocument)19 PullOMDataSource (org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource)18 Element (org.w3c.dom.Element)18 InputStream (java.io.InputStream)16 OMSourcedElement (org.apache.axiom.om.OMSourcedElement)16 ByteArrayInputStream (java.io.ByteArrayInputStream)13 OMException (org.apache.axiom.om.OMException)13