Search in sources :

Example 1 with OMContainer

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

the class AxiomTraverser method next.

@Override
public Event next() throws TraverserException {
    if (node == null) {
        if (root instanceof OMDocument) {
            node = ((OMDocument) root).getFirstOMChild();
        } else {
            node = (OMElement) root;
        }
    } else if (!visited && node instanceof OMElement) {
        OMNode firstChild = ((OMElement) node).getFirstOMChild();
        if (firstChild != null) {
            node = firstChild;
        } else {
            visited = true;
        }
    } else {
        OMNode nextSibling = node.getNextOMSibling();
        if (node == root) {
            return null;
        } else if (nextSibling != null) {
            node = nextSibling;
            visited = false;
        } else {
            OMContainer parent = node.getParent();
            if (parent instanceof OMDocument) {
                return null;
            } else {
                node = (OMElement) parent;
                visited = true;
            }
        }
    }
    switch(node.getType()) {
        case OMNode.DTD_NODE:
            return Event.DOCUMENT_TYPE;
        case OMNode.ELEMENT_NODE:
            return visited ? Event.END_ELEMENT : Event.START_ELEMENT;
        case OMNode.TEXT_NODE:
            return Event.TEXT;
        case OMNode.SPACE_NODE:
            return Event.WHITESPACE;
        case OMNode.ENTITY_REFERENCE_NODE:
            if (expandEntityReferences) {
                throw new UnsupportedOperationException();
            } else {
                return Event.ENTITY_REFERENCE;
            }
        case OMNode.COMMENT_NODE:
            return Event.COMMENT;
        case OMNode.CDATA_SECTION_NODE:
            return Event.CDATA_SECTION;
        case OMNode.PI_NODE:
            return Event.PROCESSING_INSTRUCTION;
        default:
            throw new IllegalStateException();
    }
}
Also used : OMNode(org.apache.axiom.om.OMNode) OMElement(org.apache.axiom.om.OMElement) OMContainer(org.apache.axiom.om.OMContainer) OMDocument(org.apache.axiom.om.OMDocument)

Example 2 with OMContainer

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

the class TestAddChildWithIncompleteSibling method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMContainer container = containerFactory.create(factory);
    container.addChild(OMXMLBuilderFactory.createOMBuilder(factory, new StringReader("<a>test</a>")).getDocumentElement(true));
    assertThat(container.isComplete()).isFalse();
    container.addChild(factory.createOMText("test"));
    assertThat(container).hasNumberOfChildren(2);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) StringReader(java.io.StringReader) OMContainer(org.apache.axiom.om.OMContainer)

Example 3 with OMContainer

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

the class TestRegisterCustomBuilder method runTest.

@Override
protected void runTest() throws Throwable {
    SOAPModelBuilder builder = SOAPSampleSet.WSA.getMessage(spec).getAdapter(SOAPSampleAdapter.class).getBuilder(metaFactory);
    ((CustomBuilderSupport) builder).registerCustomBuilder(new CustomBuilder.Selector() {

        @Override
        public boolean accepts(OMContainer parent, int depth, String namespaceURI, String localName) {
            return depth == 3 && namespaceURI.equals("http://www.w3.org/2005/08/addressing") && localName.equals("To");
        }
    }, new BlobOMDataSourceCustomBuilder(MemoryBlob.FACTORY, "utf-8"));
    SOAPHeader header = builder.getSOAPEnvelope().getHeader();
    ArrayList al = header.getHeaderBlocksWithNSURI("http://www.w3.org/2005/08/addressing");
    assertEquals(al.size(), 4);
    for (int i = 0; i < al.size(); i++) {
        SOAPHeaderBlock shb = (SOAPHeaderBlock) al.get(i);
        if ("To".equals(shb.getLocalName())) {
            assertNotNull(shb.getDataSource());
        }
    }
}
Also used : ArrayList(java.util.ArrayList) CustomBuilderSupport(org.apache.axiom.om.ds.custombuilder.CustomBuilderSupport) SOAPHeaderBlock(org.apache.axiom.soap.SOAPHeaderBlock) BlobOMDataSourceCustomBuilder(org.apache.axiom.om.ds.custombuilder.BlobOMDataSourceCustomBuilder) CustomBuilder(org.apache.axiom.om.ds.custombuilder.CustomBuilder) SOAPSampleAdapter(org.apache.axiom.ts.soap.SOAPSampleAdapter) SOAPModelBuilder(org.apache.axiom.soap.SOAPModelBuilder) BlobOMDataSourceCustomBuilder(org.apache.axiom.om.ds.custombuilder.BlobOMDataSourceCustomBuilder) OMContainer(org.apache.axiom.om.OMContainer) SOAPHeader(org.apache.axiom.soap.SOAPHeader)

Example 4 with OMContainer

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

the class PushOMDataSourceReader method proceed.

@Override
public boolean proceed() throws StreamException {
    // TODO: we might want to unwrap the NamespaceRepairingFilter (and some other filters) here
    XmlHandler handler = this.handler;
    OMOutputFormat format = null;
    XmlHandler current = handler;
    while (current instanceof XmlHandlerWrapper) {
        if (current instanceof XmlDeclarationRewriterHandler) {
            format = ((XmlDeclarationRewriterHandler) current).getFormat();
            break;
        }
        current = ((XmlHandlerWrapper) current).getParent();
    }
    if (format == null) {
        // This is for the OMSourcedElement expansion case
        format = new OMOutputFormat();
        format.setDoOptimize(true);
        handler = new PushOMDataSourceXOPHandler(handler);
    }
    try {
        XMLStreamWriter writer = new XmlHandlerStreamWriter(handler, null, AxiomXMLStreamWriterExtensionFactory.INSTANCE);
        // Seed the namespace context with the namespace context from the parent
        OMContainer parent = root.getParent();
        if (parent instanceof OMElement) {
            for (Iterator<OMNamespace> it = ((OMElement) parent).getNamespacesInScope(); it.hasNext(); ) {
                OMNamespace ns = it.next();
                writer.setPrefix(ns.getPrefix(), ns.getNamespaceURI());
            }
        }
        handler.startFragment();
        dataSource.serialize(new MTOMXMLStreamWriterImpl(new PushOMDataSourceStreamWriter(writer), format));
        handler.completed();
    } catch (XMLStreamException ex) {
        Throwable cause = ex.getCause();
        if (cause instanceof StreamException) {
            throw (StreamException) cause;
        } else {
            throw new StreamException(ex);
        }
    }
    return true;
}
Also used : OMNamespace(org.apache.axiom.om.OMNamespace) OMElement(org.apache.axiom.om.OMElement) XmlHandlerStreamWriter(org.apache.axiom.core.stream.stax.push.XmlHandlerStreamWriter) StreamException(org.apache.axiom.core.stream.StreamException) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamException(javax.xml.stream.XMLStreamException) XmlHandlerWrapper(org.apache.axiom.core.stream.XmlHandlerWrapper) XmlHandler(org.apache.axiom.core.stream.XmlHandler) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) XmlDeclarationRewriterHandler(org.apache.axiom.om.impl.stream.XmlDeclarationRewriterHandler) OMContainer(org.apache.axiom.om.OMContainer)

Example 5 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 {
    OMFactory factory = metaFactory.getOMFactory();
    OMSourcedElement element = TestDocument.DOCUMENT1.createOMSourcedElement(factory, push, destructive);
    OMDataSource ds = element.getDataSource();
    OMContainer parent = elementContext.wrap(element);
    boolean parentComplete = parent != null && parent.isComplete();
    expansionStrategy.apply(element);
    boolean consuming = expansionStrategy.isConsumedAfterSerialization(push, destructive, serializationStrategy);
    for (int iteration = 0; iteration < count; iteration++) {
        boolean expectException = iteration != 0 && (consuming || serializeParent && !serializationStrategy.isCaching() && !parentComplete);
        XML result;
        try {
            result = serializationStrategy.serialize(serializeParent ? parent : element);
            if (expectException) {
                fail("Expected exception");
            }
        } catch (Exception ex) {
            if (!expectException) {
                throw ex;
            } else {
                continue;
            }
        }
        InputSource expectedXML = new InputSource(new StringReader(TestDocument.DOCUMENT1.getContent()));
        if (serializeParent) {
            expectedXML = elementContext.getControl(expectedXML);
        }
        assertAbout(xml()).that(result.getInputSource()).hasSameContentAs(expectedXML);
        // the sourced element should be expanded.
        if (expansionStrategy.isExpandedAfterSerialization(push, destructive, serializationStrategy)) {
            assertTrue(element.isExpanded());
            assertEquals("OMSourcedElement completion status", !consuming, element.isComplete());
        } else {
            assertFalse(element.isExpanded());
        }
        if (parent != null && !serializeParent) {
            // Operations on the OMSourcedElement should have no impact on the parent
            assertEquals("Parent completion status", parentComplete, parent.isComplete());
        }
    }
    if (ds instanceof PullOMDataSource) {
        assertFalse(((PullOMDataSource) ds).hasUnclosedReaders());
    }
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) InputSource(org.xml.sax.InputSource) PullOMDataSource(org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource) AbstractPullOMDataSource(org.apache.axiom.om.ds.AbstractPullOMDataSource) PullOMDataSource(org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource) OMDataSource(org.apache.axiom.om.OMDataSource) AbstractPullOMDataSource(org.apache.axiom.om.ds.AbstractPullOMDataSource) AbstractPushOMDataSource(org.apache.axiom.om.ds.AbstractPushOMDataSource) XML(org.apache.axiom.ts.dimension.serialization.XML) StringReader(java.io.StringReader) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) OMContainer(org.apache.axiom.om.OMContainer)

Aggregations

OMContainer (org.apache.axiom.om.OMContainer)14 OMElement (org.apache.axiom.om.OMElement)8 OMNamespace (org.apache.axiom.om.OMNamespace)5 OMFactory (org.apache.axiom.om.OMFactory)4 StringReader (java.io.StringReader)3 Iterator (java.util.Iterator)3 OMDocument (org.apache.axiom.om.OMDocument)3 OMNode (org.apache.axiom.om.OMNode)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 OMComment (org.apache.axiom.om.OMComment)2 XML (org.apache.axiom.ts.dimension.serialization.XML)2 InputSource (org.xml.sax.InputSource)2 URL (java.net.URL)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