Search in sources :

Example 71 with OMElement

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

the class TestGetDataHandlerFromElement method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    RandomDataSource orgDS = new RandomDataSource(64 * 1024);
    OMElement orgRoot = factory.createOMElement(new QName("root"));
    OMElement orgChild = factory.createOMElement(new QName("child"), orgRoot);
    orgChild.addChild(factory.createOMText(new DataHandler(orgDS), false));
    OMElement root = OMXMLBuilderFactory.createOMBuilder(factory, new StringReader(orgRoot.toString())).getDocumentElement();
    XMLStreamReader reader = root.getXMLStreamReader(cache);
    assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
    assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
    DataSource ds = XMLStreamReaderUtils.getDataHandlerFromElement(reader).getDataSource();
    IOTestUtils.compareStreams(orgDS.getInputStream(), ds.getInputStream());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) StringReader(java.io.StringReader) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) DataSource(javax.activation.DataSource)

Example 72 with OMElement

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

the class TestGetElementTextFromParser method runTest.

@Override
protected void runTest() throws Throwable {
    // Note: We test getElementText on a child element ("b") of the element from which we request
    //       the XMLStreamReader ("a"). This is to make sure that the XMLStreamReader implementation actually
    //       delegates to the underlying parser (which is not necessarily the case on "a").
    OMXMLParserWrapper builder = builderFactory.getBuilder(metaFactory, new InputSource(new StringReader("<a><b>AB<!--comment text-->CD</b></a>")));
    OMElement element = builder.getDocumentElement();
    // Build a certain number of descendants. This is used to test scenarios where the XMLStreamReader
    // needs to switch to pull through mode in the middle of the element from which we attempt to
    // get the text.
    Iterator<OMNode> it = element.getDescendants(true);
    for (int i = 0; i < build; i++) {
        it.next();
    }
    XMLStreamReader reader = element.getXMLStreamReader(cache);
    assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
    assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
    assertEquals("ABCD", reader.getElementText());
}
Also used : OMNode(org.apache.axiom.om.OMNode) 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)

Example 73 with OMElement

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

the class TestGetNamespaceContext method runTest.

@Override
protected void runTest() throws Throwable {
    OMElement element = AXIOMUtil.stringToOM(metaFactory.getOMFactory(), "<a xmlns='urn:ns1' xmlns:ns2='urn:ns2'><b xmlns:ns3='urn:ns3'/></a>");
    XMLStreamReader stream = cache ? element.getXMLStreamReader() : element.getXMLStreamReaderWithoutCaching();
    stream.next();
    assertEquals(XMLStreamReader.START_ELEMENT, stream.next());
    assertEquals("b", stream.getLocalName());
    NamespaceContext context = stream.getNamespaceContext();
    assertEquals("urn:ns1", context.getNamespaceURI(""));
    assertEquals("urn:ns2", context.getNamespaceURI("ns2"));
    assertEquals("urn:ns3", context.getNamespaceURI("ns3"));
    assertEquals("ns2", context.getPrefix("urn:ns2"));
    element.close(false);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) NamespaceContext(javax.xml.namespace.NamespaceContext) OMElement(org.apache.axiom.om.OMElement)

Example 74 with OMElement

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

the class TestSerializeAndConsumePartiallyBuilt method runTest.

@Override
protected void runTest() throws Throwable {
    String xml = "<root><child><grandchild1>text</grandchild1></child><child><grandchild2>text</grandchild2></child></root>";
    OMElement root = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), new StringReader(xml)).getDocumentElement();
    // Partially build the tree
    root.getFirstElement().getFirstElement();
    StringWriter out = new StringWriter();
    root.serializeAndConsume(out);
    assertEquals(xml, out.toString());
}
Also used : StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) OMElement(org.apache.axiom.om.OMElement)

Example 75 with OMElement

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

the class TestSetText method runTest.

@Override
protected void runTest() throws Throwable {
    OMElement element = metaFactory.getOMFactory().createOMElement("test", null);
    String text = "The quick brown fox jumps over the lazy dog";
    element.setText(text);
    // Check that OMElement#getText() returns a matching value
    assertEquals("Text value mismatch", text, element.getText());
    // Check that OMElement#setText() has created the expected nodes
    OMNode child = element.getFirstOMChild();
    assertTrue(child instanceof OMText);
    assertSame(element, child.getParent());
    assertEquals(text, ((OMText) child).getText());
    assertNull(child.getNextOMSibling());
}
Also used : OMNode(org.apache.axiom.om.OMNode) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement)

Aggregations

OMElement (org.apache.axiom.om.OMElement)379 OMFactory (org.apache.axiom.om.OMFactory)189 OMNamespace (org.apache.axiom.om.OMNamespace)101 QName (javax.xml.namespace.QName)90 StringReader (java.io.StringReader)63 OMNode (org.apache.axiom.om.OMNode)42 OMText (org.apache.axiom.om.OMText)40 XMLStreamReader (javax.xml.stream.XMLStreamReader)37 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)28 OMAttribute (org.apache.axiom.om.OMAttribute)26 StringWriter (java.io.StringWriter)23 OMXMLParserWrapper (org.apache.axiom.om.OMXMLParserWrapper)21 OMDocument (org.apache.axiom.om.OMDocument)19 PullOMDataSource (org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource)18 Element (org.w3c.dom.Element)18 DataHandler (javax.activation.DataHandler)17 OMSourcedElement (org.apache.axiom.om.OMSourcedElement)16 InputStream (java.io.InputStream)15 OMException (org.apache.axiom.om.OMException)13 SOAPBody (org.apache.axiom.soap.SOAPBody)13