Search in sources :

Example 16 with OMNode

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

the class TestSetTextQNameWithExistingChildren method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMElement element = factory.createOMElement("TestElement", null);
    // Add some children of various types
    factory.createOMText(element, "some text");
    factory.createOMText(element, "cdata section", OMNode.CDATA_SECTION_NODE);
    factory.createOMComment(element, "comment");
    factory.createOMProcessingInstruction(element, "piTarget", "piData");
    factory.createOMElement("child", null, element);
    QName qname = new QName("urn:ns1", "test", "ns");
    element.setText(qname);
    assertEquals("ns:test", element.getText());
    // Check that OMElement#setText() has created the expected nodes
    OMNode child = element.getFirstOMChild();
    assertTrue(child instanceof OMText);
    assertSame(element, child.getParent());
    assertEquals("ns:test", ((OMText) child).getText());
    assertNull(child.getNextOMSibling());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNode(org.apache.axiom.om.OMNode) QName(javax.xml.namespace.QName) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement)

Example 17 with OMNode

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

the class TestCreateOMDocument method runTest.

@Override
protected void runTest() throws Throwable {
    OMDocument document = metaFactory.getOMFactory().createOMDocument();
    assertNotNull(document);
    assertNull(document.getFirstOMChild());
    // OMDocument doesn't extend OMNode. Therefore, the OMDocument implementation
    // should not implement OMNode either. This is a regression test for AXIOM-385.
    assertFalse(document instanceof OMNode);
}
Also used : OMNode(org.apache.axiom.om.OMNode) OMDocument(org.apache.axiom.om.OMDocument)

Example 18 with OMNode

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

the class TestClose method runTest.

@Override
protected void runTest() throws Throwable {
    OMElement rootElement = XMLSample.SIMPLE.getAdapter(XMLSampleAdapter.class).getDocumentElement(metaFactory);
    // get the first OMElement child
    OMNode omnode = rootElement.getFirstOMChild();
    while (!(omnode instanceof OMElement)) {
        omnode = omnode.getNextOMSibling();
    }
    // Close the element after building the element
    OMElement omElement = (OMElement) omnode;
    omElement.close(true);
    Iterator<OMNode> children = ((OMElement) omnode).getChildren();
    int childrenCount = 0;
    while (children.hasNext()) {
        if (children.next() instanceof OMElement) {
            childrenCount++;
        }
    }
    assertThat(childrenCount).isEqualTo(2);
}
Also used : OMNode(org.apache.axiom.om.OMNode) OMElement(org.apache.axiom.om.OMElement) XMLSampleAdapter(org.apache.axiom.ts.om.XMLSampleAdapter)

Example 19 with OMNode

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

the class TestSetDataSource method runTest.

@Override
protected void runTest() throws Throwable {
    String payload1 = "<tns:myPayload xmlns:tns=\"urn://test\">Payload One</tns:myPayload>";
    String payload2 = "<tns:myPayload xmlns:tns=\"urn://test\">Payload Two</tns:myPayload>";
    OMDataSource nonDestructiveOMDataSource1 = new PullOMDataSource(payload1, false);
    OMDataSource nonDestructiveOMDataSource2 = new PullOMDataSource(payload2, false);
    OMDataSource destructiveOMDataSource1 = new PullOMDataSource(payload1, true);
    OMDataSource destructiveOMDataSource2 = new PullOMDataSource(payload2, true);
    OMFactory factory = metaFactory.getOMFactory();
    OMElement parent = factory.createOMElement("parent", null);
    OMSourcedElement omse = factory.createOMElement(nonDestructiveOMDataSource1, "myPayload", factory.createOMNamespace("urn://test", "tns"));
    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(nonDestructiveOMDataSource1);
    // Write out the body
    StringWriter sw = new StringWriter();
    parent.serialize(sw);
    String output = sw.toString();
    //        System.out.println(output);
    assertTrue("The payload was not present in the output", output.indexOf(payload1) > 0);
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    // Replace with payload2.  
    // Important note, it is legal to replace the OMDataSource, but
    // the namespace and local name of the OMSourcedElement cannot be changed.
    child.setDataSource(nonDestructiveOMDataSource2);
    // Write out the body
    sw = new StringWriter();
    parent.serialize(sw);
    output = sw.toString();
    //        System.out.println(output);
    assertTrue("The payload was not present in the output", output.indexOf(payload2) > 0);
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    // Now Replace with payload1 from an destructiveOMDataSource1
    child.setDataSource(destructiveOMDataSource1);
    sw = new StringWriter();
    parent.serialize(sw);
    output = sw.toString();
    //        System.out.println(output);
    assertTrue("The payload was not present in the output", output.indexOf(payload1) > 0);
    // Now Replace with payload2 from an destructiveOMDataSource2.
    // Note at this point, the child's tree is expanded.
    child.setDataSource(destructiveOMDataSource2);
    sw = new StringWriter();
    parent.serialize(sw);
    output = sw.toString();
    //        System.out.println(output);
    assertTrue("The payload was not present in the output", output.indexOf(payload2) > 0);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNode(org.apache.axiom.om.OMNode) PullOMDataSource(org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource) StringWriter(java.io.StringWriter) PullOMDataSource(org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource) OMDataSource(org.apache.axiom.om.OMDataSource) OMElement(org.apache.axiom.om.OMElement) OMSourcedElement(org.apache.axiom.om.OMSourcedElement)

Example 20 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)

Aggregations

OMNode (org.apache.axiom.om.OMNode)69 OMElement (org.apache.axiom.om.OMElement)42 StringReader (java.io.StringReader)16 OMFactory (org.apache.axiom.om.OMFactory)14 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)11 OMText (org.apache.axiom.om.OMText)10 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 SOAPHeaderBlock (org.apache.axiom.soap.SOAPHeaderBlock)4 OMProcessingInstruction (org.apache.axiom.om.OMProcessingInstruction)3 SOAPFault (org.apache.axiom.soap.SOAPFault)3 SOAPFaultCode (org.apache.axiom.soap.SOAPFaultCode)3