Search in sources :

Example 46 with OMElement

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

the class OMXMLParserWrapperImpl method getDocumentElement.

@Override
public final OMElement getDocumentElement(boolean discardDocument) {
    try {
        OMDocument document = getDocument();
        OMElement element = document.getOMDocumentElement();
        if (discardDocument) {
            element.detach();
            ((AxiomDocument) document).coreDiscard(false);
        }
        return element;
    } catch (CoreModelException ex) {
        throw AxiomExceptionTranslator.translate(ex);
    }
}
Also used : AxiomDocument(org.apache.axiom.om.impl.intf.AxiomDocument) CoreModelException(org.apache.axiom.core.CoreModelException) OMElement(org.apache.axiom.om.OMElement) OMDocument(org.apache.axiom.om.OMDocument)

Example 47 with OMElement

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

the class XMLComparator method compareChildren.

private void compareChildren(OMElement elementOne, OMElement elementTwo) throws XMLComparisonException {
    //ignore if the elements belong to any of the ignorable namespaces list
    if (isIgnorable(elementOne) || isIgnorable(elementTwo)) {
        return;
    }
    Iterator elementOneChildren = elementOne.getChildren();
    while (elementOneChildren.hasNext()) {
        OMNode omNode = (OMNode) elementOneChildren.next();
        if (omNode instanceof OMElement) {
            OMElement elementOneChild = (OMElement) omNode;
            OMElement elementTwoChild = null;
            //Do the comparison only if the element is not ignorable
            if (!isIgnorable(elementOneChild)) {
                elementTwoChild = elementTwo.getFirstChildWithName(elementOneChild.getQName());
                //Do the comparison only if the element is not ignorable
                if (!isIgnorable(elementTwoChild)) {
                    if (elementTwoChild == null) {
                        throw new XMLComparisonException(" There is no " + elementOneChild.getLocalName() + " element under " + elementTwo.getLocalName());
                    }
                }
            }
            compare(elementOneChild, elementTwoChild);
        }
    }
}
Also used : OMNode(org.apache.axiom.om.OMNode) XMLComparisonException(org.apache.axiom.om.impl.exception.XMLComparisonException) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement)

Example 48 with OMElement

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

the class OMChildrenWithSpecificAttributeIteratorTest method testChildrenRetrievalWithDetaching.

public void testChildrenRetrievalWithDetaching() {
    OMFactory factory = OMAbstractFactory.getOMFactory();
    OMNamespace testNamespace = factory.createOMNamespace("http://test.ws.org", "test");
    OMElement documentElement = getSampleDocumentElement(testNamespace);
    Iterator childrenIter = new OMChildrenWithSpecificAttributeIterator(documentElement.getFirstOMChild(), new QName(testNamespace.getNamespaceURI(), "myAttr", testNamespace.getPrefix()), "Axis2", true);
    int childCount = getChidrenCount(childrenIter);
    assertEquals("Iterator must return 5 children with the given attribute", childCount, 5);
    Iterator children = documentElement.getChildren();
    childCount = getChidrenCount(children);
    assertEquals("Iterator must return only one child, having detached the other children", childCount, 1);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNamespace(org.apache.axiom.om.OMNamespace) QName(javax.xml.namespace.QName) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement)

Example 49 with OMElement

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

the class TestGetXMLStreamReaderWithPreserveNamespaceContext method runTest.

@Override
protected void runTest() throws Throwable {
    InputStream in = TestGetXMLStreamReaderWithPreserveNamespaceContext.class.getResourceAsStream("AXIOM-114.xml");
    OMElement root = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), in).getDocumentElement();
    root.declareNamespace("http://example.org", "p");
    OMXMLStreamReaderConfiguration configuration = new OMXMLStreamReaderConfiguration();
    configuration.setPreserveNamespaceContext(preserveNamespaceContext);
    XMLStreamReader reader = root.getFirstElement().getFirstElement().getXMLStreamReader(cache, configuration);
    assertThat(reader.next()).isEqualTo(XMLStreamReader.START_ELEMENT);
    Set<String> prefixes = new HashSet<>();
    for (int i = 0; i < reader.getNamespaceCount(); i++) {
        prefixes.add(reader.getNamespacePrefix(i));
    }
    if (preserveNamespaceContext) {
        assertThat(prefixes).containsExactly("soapenv", "xsd", "xsi", "ns", "p");
    } else {
        assertThat(prefixes).containsExactly("ns");
    }
    // Make sure that we start pulling events directly from the underlying parser.
    reader.nextTag();
    // The following assertions are true irrespective of the value of preserveNamespaceContext.
    assertThat(reader.getNamespaceURI("xsd")).isEqualTo("http://www.w3.org/2001/XMLSchema");
    // Namespace declarations added programmatically on an ancestor should also be visible.
    assertThat(reader.getNamespaceURI("p")).isEqualTo("http://example.org");
    NamespaceContext nc = reader.getNamespaceContext();
    assertThat(nc.getPrefix("http://www.w3.org/2001/XMLSchema")).isEqualTo("xsd");
    assertThat(nc.getPrefix("http://example.org")).isEqualTo("p");
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStream(java.io.InputStream) NamespaceContext(javax.xml.namespace.NamespaceContext) OMElement(org.apache.axiom.om.OMElement) OMXMLStreamReaderConfiguration(org.apache.axiom.om.OMXMLStreamReaderConfiguration) HashSet(java.util.HashSet)

Example 50 with OMElement

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

the class TestGetXMLStreamReaderWithoutCachingPartiallyBuilt method runTest.

@Override
protected void runTest() throws Throwable {
    // Note: the problem described in AXIOM-393 specifically occurred with descendants
    //       having the same name as the root element
    OMElement root = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), new StringReader("<element><element><element/><element/></element></element>")).getDocumentElement();
    // Partially build the tree
    root.getFirstElement().getFirstElement();
    XMLStreamReader reader = root.getXMLStreamReaderWithoutCaching();
    int depth = 0;
    while (reader.hasNext()) {
        int event = reader.next();
        if (event == XMLStreamReader.START_ELEMENT) {
            depth++;
        } else if (event == XMLStreamReader.END_ELEMENT) {
            depth--;
        }
    }
    assertEquals(0, depth);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StringReader(java.io.StringReader) OMElement(org.apache.axiom.om.OMElement)

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