Search in sources :

Example 6 with OMContainer

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

the class TestDetach method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMContainer root;
    if (document) {
        root = OMXMLBuilderFactory.createOMBuilder(factory, new StringReader("<!--a--><b/><!--c-->")).getDocument();
    } else {
        root = OMXMLBuilderFactory.createOMBuilder(factory, new StringReader("<root><!--a--><b/><!--c--></root>")).getDocumentElement();
    }
    if (build) {
        root.build();
    } else {
        assertFalse(root.isComplete());
    }
    OMComment a = (OMComment) root.getFirstOMChild();
    assertEquals("a", a.getValue());
    OMElement b = (OMElement) a.getNextOMSibling();
    assertEquals("b", b.getLocalName());
    OMNode returnValue = b.detach();
    // Detach is expected to do a "return this"
    assertSame(b, returnValue);
    assertNull(b.getParent());
    assertNull(b.getPreviousOMSibling());
    assertNull(b.getNextOMSibling());
    OMComment c = (OMComment) a.getNextOMSibling();
    assertEquals("c", c.getValue());
    assertSame(c, a.getNextOMSibling());
    assertSame(a, c.getPreviousOMSibling());
    root.close(false);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNode(org.apache.axiom.om.OMNode) OMComment(org.apache.axiom.om.OMComment) StringReader(java.io.StringReader) OMElement(org.apache.axiom.om.OMElement) OMContainer(org.apache.axiom.om.OMContainer)

Example 7 with OMContainer

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

the class TestGetDescendants method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMContainer root = containerFactory.create(factory);
    OMElement child1 = factory.createOMElement("child", null, root);
    OMProcessingInstruction child2 = factory.createOMProcessingInstruction(root, "test", "data");
    OMText grandchild1 = factory.createOMText(child1, "text");
    OMComment grandchild2 = factory.createOMComment(child1, "text");
    Iterator<? extends OMSerializable> it = root.getDescendants(includeSelf);
    if (includeSelf) {
        assertThat(it.hasNext()).isTrue();
        assertThat(it.next()).isEqualTo(root);
    }
    assertThat(it.hasNext()).isTrue();
    assertThat(it.next()).isEqualTo(child1);
    assertThat(it.hasNext()).isTrue();
    assertThat(it.next()).isEqualTo(grandchild1);
    assertThat(it.hasNext()).isTrue();
    assertThat(it.next()).isEqualTo(grandchild2);
    assertThat(it.hasNext()).isTrue();
    assertThat(it.next()).isEqualTo(child2);
    assertThat(it.hasNext()).isFalse();
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMProcessingInstruction(org.apache.axiom.om.OMProcessingInstruction) OMComment(org.apache.axiom.om.OMComment) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) OMContainer(org.apache.axiom.om.OMContainer)

Example 8 with OMContainer

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

the class TestGetBuilderNull method runTest.

@Override
protected void runTest() throws Throwable {
    OMContainer container = containerFactory.create(metaFactory.getOMFactory());
    assertThat(container.getBuilder()).isNull();
}
Also used : OMContainer(org.apache.axiom.om.OMContainer)

Example 9 with OMContainer

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

the class TestSerialize method runTest.

@Override
protected void runTest() throws Throwable {
    OMXMLParserWrapper builder = file.getAdapter(XMLSampleAdapter.class).getBuilder(metaFactory);
    try {
        OMContainer container = containerExtractor.getContainer(builder);
        // We need to clone the InputSource objects so that we can dump their contents
        // if the test fails
        InputSource[] control = duplicateInputSource(containerExtractor.getControl(file.getInputStream()));
        XML actual = serializationStrategy.serialize(container);
        try {
            // Configure the InputSources such that external entities can be resolved
            String systemId = new URL(file.getUrl(), "dummy.xml").toString();
            control[0].setSystemId(systemId);
            InputSource actualIS = actual.getInputSource();
            actualIS.setSystemId(systemId);
            assertAbout(xml()).that(actualIS).ignoringElementContentWhitespace().hasSameContentAs(control[0]);
        } catch (Throwable ex) {
            System.out.println("Control:");
            dumpInputSource(control[1]);
            System.out.println("Actual:");
            actual.dump(System.out);
            throw ex;
        }
        if (serializationStrategy.isCaching()) {
            assertTrue(container.isComplete());
        } else {
            // TODO: need to investigate why assertConsumed is not working here
            assertFalse(container.isComplete());
        //                assertConsumed(element);
        }
    } finally {
        builder.close();
    }
}
Also used : InputSource(org.xml.sax.InputSource) XML(org.apache.axiom.ts.dimension.serialization.XML) XMLSampleAdapter(org.apache.axiom.ts.om.XMLSampleAdapter) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper) OMContainer(org.apache.axiom.om.OMContainer) URL(java.net.URL)

Example 10 with OMContainer

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

the class DocumentNavigator method getNamespaceAxisIterator.

/**
     * Retrieves an <code>Iterator</code> matching the <code>namespace</code> XPath axis.
     *
     * @param contextNode the original context node
     * @return Returns an Iterator capable of traversing the axis, not null.
     * @throws UnsupportedAxisException if the semantics of the namespace axis are not supported by
     *                                  this object model
     */
@Override
public Iterator<?> getNamespaceAxisIterator(Object contextNode) throws UnsupportedAxisException {
    if (!(contextNode instanceof OMContainer && contextNode instanceof OMElement)) {
        return JaxenConstants.EMPTY_ITERATOR;
    }
    OMContainer omContextNode = (OMContainer) contextNode;
    List<OMNamespaceEx> nsList = new ArrayList<OMNamespaceEx>();
    Set<String> prefixes = new HashSet<String>();
    for (OMContainer context = omContextNode; context != null && !(context instanceof OMDocument); context = ((OMElement) context).getParent()) {
        OMElement element = (OMElement) context;
        List<OMNamespace> declaredNS = new ArrayList<OMNamespace>();
        Iterator<OMNamespace> i = element.getAllDeclaredNamespaces();
        while (i != null && i.hasNext()) {
            declaredNS.add(i.next());
        }
        declaredNS.add(element.getNamespace());
        for (Iterator<OMAttribute> iter = element.getAllAttributes(); iter != null && iter.hasNext(); ) {
            OMAttribute attr = iter.next();
            OMNamespace namespace = attr.getNamespace();
            if (namespace != null) {
                declaredNS.add(namespace);
            }
        }
        for (OMNamespace namespace : declaredNS) {
            if (namespace != null) {
                String prefix = namespace.getPrefix();
                if (prefix != null && !prefixes.contains(prefix)) {
                    prefixes.add(prefix);
                    nsList.add(new OMNamespaceEx(namespace, context));
                }
            }
        }
    }
    nsList.add(new OMNamespaceEx(omContextNode.getOMFactory().createOMNamespace("http://www.w3.org/XML/1998/namespace", "xml"), omContextNode));
    return nsList.iterator();
}
Also used : OMNamespace(org.apache.axiom.om.OMNamespace) ArrayList(java.util.ArrayList) OMElement(org.apache.axiom.om.OMElement) OMDocument(org.apache.axiom.om.OMDocument) OMAttribute(org.apache.axiom.om.OMAttribute) OMContainer(org.apache.axiom.om.OMContainer) HashSet(java.util.HashSet)

Aggregations

OMContainer (org.apache.axiom.om.OMContainer)11 OMElement (org.apache.axiom.om.OMElement)6 OMFactory (org.apache.axiom.om.OMFactory)4 StringReader (java.io.StringReader)3 OMNamespace (org.apache.axiom.om.OMNamespace)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 OMComment (org.apache.axiom.om.OMComment)2 OMDocument (org.apache.axiom.om.OMDocument)2 OMNode (org.apache.axiom.om.OMNode)2 XML (org.apache.axiom.ts.dimension.serialization.XML)2 InputSource (org.xml.sax.InputSource)2 URL (java.net.URL)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 StreamException (org.apache.axiom.core.stream.StreamException)1 XmlHandler (org.apache.axiom.core.stream.XmlHandler)1 XmlHandlerWrapper (org.apache.axiom.core.stream.XmlHandlerWrapper)1