Search in sources :

Example 31 with OMNode

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

the class TestStringOMDataSource method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    String localName = "myPayload";
    String payload1 = "<tns:myPayload xmlns:tns=\"urn://test\">Payload One</tns:myPayload>";
    OMNamespace ns = factory.createOMNamespace("urn://test", "tns");
    StringOMDataSource somds = new StringOMDataSource(payload1);
    OMElement parent = factory.createOMElement("root", null);
    OMSourcedElement omse = factory.createOMElement(somds, localName, ns);
    parent.addChild(omse);
    OMNode firstChild = parent.getFirstOMChild();
    assertTrue("Expected OMSourcedElement child", firstChild instanceof OMSourcedElement);
    OMSourcedElement child = (OMSourcedElement) firstChild;
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    assertThat(child.getDataSource()).isInstanceOf(StringOMDataSource.class);
    // A StringOMDataSource does not consume the backing object when read.
    // Thus getting the XMLStreamReader of the StringOMDataSource should not 
    // cause expansion of the OMSourcedElement.
    XMLStreamReader reader = child.getXMLStreamReader();
    reader.next();
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    // Likewise, a StringOMDataSource does not consume the backing object when 
    // written.  Thus serializing the OMSourcedElement should not cause the expansion
    // of the OMSourcedElement.
    StringWriter out = new StringWriter();
    parent.serialize(out);
    //        System.out.println(output);
    assertTrue("The payload was not present in the output", out.toString().indexOf(payload1) > 0);
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    // Test getting the raw content from the StringOMDataSource.
    StringOMDataSource ds = (StringOMDataSource) child.getDataSource();
    assertThat(ds.getObject()).isEqualTo(payload1);
    // Validate close
    ds.close();
    assertTrue("Close should free the resource", ds.getObject() == null);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNode(org.apache.axiom.om.OMNode) OMNamespace(org.apache.axiom.om.OMNamespace) XMLStreamReader(javax.xml.stream.XMLStreamReader) StringWriter(java.io.StringWriter) StringOMDataSource(org.apache.axiom.om.ds.StringOMDataSource) OMElement(org.apache.axiom.om.OMElement) OMSourcedElement(org.apache.axiom.om.OMSourcedElement)

Example 32 with OMNode

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

the class TestInsertSiblingBeforeSameParent method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory fac = metaFactory.getOMFactory();
    OMElement parent = fac.createOMElement("test", null);
    OMText text1 = fac.createOMText("text1");
    OMText text2 = fac.createOMText("text2");
    OMText text3 = fac.createOMText("text3");
    parent.addChild(text1);
    parent.addChild(text2);
    parent.addChild(text3);
    text2.insertSiblingBefore(text3);
    assertSame(parent, text3.getParent());
    Iterator<OMNode> it = parent.getChildren();
    assertSame(text1, it.next());
    assertSame(text3, it.next());
    assertSame(text2, it.next());
    assertFalse(it.hasNext());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNode(org.apache.axiom.om.OMNode) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement)

Example 33 with OMNode

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

the class TestBlobOMDataSource method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    String localName = "myPayload";
    String encoding = "utf-8";
    String payload = "<tns:myPayload xmlns:tns=\"urn://test\">Payload One</tns:myPayload>";
    OMNamespace ns = factory.createOMNamespace("urn://test", "tns");
    BlobOMDataSource ds = new BlobOMDataSource(Blobs.createBlob(payload.getBytes(encoding)), encoding);
    OMElement parent = factory.createOMElement("root", null);
    OMSourcedElement omse = factory.createOMElement(ds, localName, ns);
    parent.addChild(omse);
    OMNode firstChild = parent.getFirstOMChild();
    assertTrue("Expected OMSourcedElement child", firstChild instanceof OMSourcedElement);
    OMSourcedElement child = (OMSourcedElement) firstChild;
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    assertThat(child.getDataSource()).isSameAs(ds);
    // A BlobOMDataSource does not consume the backing object when read.
    // Thus getting the XMLStreamReader of the BlobOMDataSource should not 
    // cause expansion of the OMSourcedElement.
    XMLStreamReader reader = child.getXMLStreamReader();
    reader.next();
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    // Likewise, a BlobOMDataSource does not consume the backing object when 
    // written.  Thus serializing the OMSourcedElement should not cause the expansion
    // of the OMSourcedElement.
    assertTrue("The payload was not present in the output", parent.toString().indexOf(payload) > 0);
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    // If a consumer calls build or buildWithAttachments on the tree, the 
    // tree should not be expanded.
    parent.build();
    assertTrue("OMSourcedElement is expanded after build().  This is unexpected", !child.isExpanded());
    parent.buildWithAttachments();
    assertTrue("OMSourcedElement is expanded after buildWithAttachments().  This is unexpected", !child.isExpanded());
    // Test getting the raw bytes from the BlobOMDataSource.
    assertThat(child.getDataSource()).isSameAs(ds);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNode(org.apache.axiom.om.OMNode) OMNamespace(org.apache.axiom.om.OMNamespace) XMLStreamReader(javax.xml.stream.XMLStreamReader) BlobOMDataSource(org.apache.axiom.om.ds.BlobOMDataSource) OMElement(org.apache.axiom.om.OMElement) OMSourcedElement(org.apache.axiom.om.OMSourcedElement)

Example 34 with OMNode

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

the class TestGetNextOMSiblingAfterDiscard method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMElement element = OMXMLBuilderFactory.createOMBuilder(factory, new StringReader("<element><!--comment--><a/><!--comment--></element>")).getDocumentElement();
    OMNode child = element.getFirstOMChild();
    element.discard();
    try {
        child.getNextOMSibling();
        fail("Expected NodeUnavailableException");
    } catch (NodeUnavailableException ex) {
    // Expected
    }
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNode(org.apache.axiom.om.OMNode) StringReader(java.io.StringReader) NodeUnavailableException(org.apache.axiom.om.NodeUnavailableException) OMElement(org.apache.axiom.om.OMElement)

Example 35 with OMNode

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

the class TestDetachFirstChild method runTest.

@Override
protected void runTest() throws Throwable {
    OMElement root = AXIOMUtil.stringToOM(metaFactory.getOMFactory(), "<root><a/><b/></root>");
    if (build) {
        root.build();
    } else {
        assertFalse(root.isComplete());
    }
    OMNode oldFirstChild = root.getFirstOMChild();
    assertNotNull(oldFirstChild);
    oldFirstChild.detach();
    OMNode newFirstChild = root.getFirstOMChild();
    assertNotNull(newFirstChild);
    assertNotSame(oldFirstChild, newFirstChild);
}
Also used : OMNode(org.apache.axiom.om.OMNode) OMElement(org.apache.axiom.om.OMElement)

Aggregations

OMNode (org.apache.axiom.om.OMNode)70 OMElement (org.apache.axiom.om.OMElement)43 StringReader (java.io.StringReader)16 OMFactory (org.apache.axiom.om.OMFactory)14 OMText (org.apache.axiom.om.OMText)11 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)11 OMDocument (org.apache.axiom.om.OMDocument)9 OMSourcedElement (org.apache.axiom.om.OMSourcedElement)7 XMLStreamReader (javax.xml.stream.XMLStreamReader)6 OMComment (org.apache.axiom.om.OMComment)6 OMXMLParserWrapper (org.apache.axiom.om.OMXMLParserWrapper)6 QName (javax.xml.namespace.QName)5 SOAPHeader (org.apache.axiom.soap.SOAPHeader)5 OMAttribute (org.apache.axiom.om.OMAttribute)4 OMNamespace (org.apache.axiom.om.OMNamespace)4 SOAPBody (org.apache.axiom.soap.SOAPBody)4 IOException (java.io.IOException)3 StringWriter (java.io.StringWriter)3 OMProcessingInstruction (org.apache.axiom.om.OMProcessingInstruction)3 SOAPFault (org.apache.axiom.soap.SOAPFault)3