Search in sources :

Example 6 with OMDataSource

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

the class TestGetDocumentFromBuilder method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMDataSource ds = new StringOMDataSource("<root><a/></root>");
    OMSourcedElement element = factory.createOMElement(ds);
    // Force expansion
    element.getFirstOMChild();
    OMXMLParserWrapper builder = element.getBuilder();
    try {
        builder.getDocument();
        fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException ex) {
    // Expected
    }
    try {
        builder.getDocumentElement();
        fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException ex) {
    // Expected
    }
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMDataSource(org.apache.axiom.om.OMDataSource) StringOMDataSource(org.apache.axiom.om.ds.StringOMDataSource) StringOMDataSource(org.apache.axiom.om.ds.StringOMDataSource) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper)

Example 7 with OMDataSource

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

the class CustomBuilderManager method getAction.

private Runnable getAction(CoreNode node, int depth, int firstCustomBuilder) {
    lastCandidateElement = null;
    lastCandidateDepth = -1;
    if (node instanceof AxiomElement && (node instanceof AxiomSOAPHeaderBlock || !(node instanceof AxiomSOAPElement))) {
        final AxiomElement element = (AxiomElement) node;
        if (registrations != null) {
            for (int i = firstCustomBuilder; i < registrations.size(); i++) {
                CustomBuilderRegistration registration = registrations.get(i);
                final String namespaceURI = element.coreGetNamespaceURI();
                final String localName = element.coreGetLocalName();
                if (registration.getSelector().accepts(element.getParent(), depth, namespaceURI, localName)) {
                    final CustomBuilder customBuilder = registration.getCustomBuilder();
                    if (log.isDebugEnabled()) {
                        log.debug("Custom builder " + customBuilder + " accepted element {" + namespaceURI + "}" + localName + " at depth " + depth);
                    }
                    return new Runnable() {

                        @Override
                        public void run() {
                            if (log.isDebugEnabled()) {
                                log.debug("Invoking custom builder " + customBuilder);
                            }
                            OMDataSource dataSource = customBuilder.create(element);
                            Class<? extends AxiomSourcedElement> type;
                            if (element instanceof AxiomSOAP11HeaderBlock) {
                                type = AxiomSOAP11HeaderBlock.class;
                            } else if (element instanceof AxiomSOAP12HeaderBlock) {
                                type = AxiomSOAP12HeaderBlock.class;
                            } else {
                                type = AxiomSourcedElement.class;
                            }
                            if (log.isDebugEnabled()) {
                                log.debug("Replacing element with new sourced element of type " + type);
                            }
                            AxiomSourcedElement newElement = element.coreCreateNode(type);
                            newElement.init(localName, new OMNamespaceImpl(namespaceURI, null), dataSource);
                            try {
                                element.coreReplaceWith(newElement, AxiomSemantics.INSTANCE);
                            } catch (CoreModelException ex) {
                                throw AxiomExceptionTranslator.translate(ex);
                            }
                        }
                    };
                }
            }
        }
        // Save a reference to the element so that we can process it when another custom builder is registered
        lastCandidateElement = element;
        lastCandidateDepth = depth;
    }
    return null;
}
Also used : OMDataSource(org.apache.axiom.om.OMDataSource) AxiomSOAPHeaderBlock(org.apache.axiom.soap.impl.intf.AxiomSOAPHeaderBlock) CustomBuilder(org.apache.axiom.om.ds.custombuilder.CustomBuilder) AxiomSOAP12HeaderBlock(org.apache.axiom.soap.impl.intf.AxiomSOAP12HeaderBlock) AxiomSOAPElement(org.apache.axiom.soap.impl.intf.AxiomSOAPElement) AxiomSourcedElement(org.apache.axiom.om.impl.intf.AxiomSourcedElement) CoreModelException(org.apache.axiom.core.CoreModelException) AxiomElement(org.apache.axiom.om.impl.intf.AxiomElement) AxiomSOAP11HeaderBlock(org.apache.axiom.soap.impl.intf.AxiomSOAP11HeaderBlock) OMNamespaceImpl(org.apache.axiom.om.impl.common.OMNamespaceImpl)

Example 8 with OMDataSource

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

the class StreamingOMSerializer method serializeNode.

protected void serializeNode(XMLStreamReader reader, XMLStreamWriter writer, boolean startAtNext) throws XMLStreamException {
    // TODO We get the StAXWriter at this point and uses it hereafter 
    // assuming that this is the only entry point to this class.
    // If there can be other classes calling methodes of this we might 
    // need to change methode signatures to OMOutputer
    // If not startAtNext, then seed the processing with the current element.
    boolean useCurrentEvent = !startAtNext;
    while (reader.hasNext() || useCurrentEvent) {
        int event = 0;
        OMDataSource ds = null;
        if (useCurrentEvent) {
            event = reader.getEventType();
            useCurrentEvent = false;
        } else {
            event = reader.next();
        }
        // DataSource
        if (reader instanceof OMXMLStreamReaderEx) {
            ds = ((OMXMLStreamReaderEx) reader).getDataSource();
        }
        if (ds != null) {
            ds.serialize(writer);
        } else {
            switch(event) {
                case START_ELEMENT:
                    serializeElement(reader, writer);
                    depth++;
                    break;
                case ATTRIBUTE:
                    serializeAttributes(reader, writer);
                    break;
                case CHARACTERS:
                    if (dataHandlerReader != null && dataHandlerReader.isBinary()) {
                        serializeDataHandler();
                        break;
                    }
                // Fall through
                case SPACE:
                    serializeText(reader, writer);
                    break;
                case COMMENT:
                    serializeComment(reader, writer);
                    break;
                case CDATA:
                    serializeCData(reader, writer);
                    break;
                case PROCESSING_INSTRUCTION:
                    serializeProcessingInstruction(reader, writer);
                    break;
                case END_ELEMENT:
                    serializeEndElement(writer);
                    depth--;
                    break;
                case START_DOCUMENT:
                    //if a start document is found then increment the depth
                    depth++;
                    break;
                case END_DOCUMENT:
                    //for the end document - reduce the depth
                    if (depth != 0)
                        depth--;
                    try {
                        serializeEndElement(writer);
                    } catch (Exception e) {
                    //TODO: log exceptions
                    }
                    break;
                case DTD:
                    serializeDTD(reader, writer);
                    break;
                case ENTITY_REFERENCE:
                    writer.writeEntityRef(reader.getLocalName());
            }
        }
        if (depth == 0) {
            break;
        }
    }
}
Also used : OMXMLStreamReaderEx(org.apache.axiom.om.impl.OMXMLStreamReaderEx) OMDataSource(org.apache.axiom.om.OMDataSource) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 9 with OMDataSource

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

the class TestCloneNonDestructive method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMDataSource ds = new WrappedTextNodeOMDataSourceFromDataSource(new QName("wrapper"), new ByteArrayDataSource("test".getBytes("utf-8")), Charset.forName("utf-8"));
    OMSourcedElement element = factory.createOMElement(ds);
    OMCloneOptions options = new OMCloneOptions();
    options.setCopyOMDataSources(copyOMDataSources);
    OMElement clone = (OMElement) element.clone(options);
    if (copyOMDataSources) {
        assertTrue(clone instanceof OMSourcedElement);
        assertFalse(element.isExpanded());
    } else {
        assertFalse(clone instanceof OMSourcedElement);
        assertTrue(clone.isComplete());
    }
    assertEquals("test", clone.getText());
    assertEquals("wrapper", clone.getLocalName());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMDataSource(org.apache.axiom.om.OMDataSource) QName(javax.xml.namespace.QName) OMCloneOptions(org.apache.axiom.om.OMCloneOptions) WrappedTextNodeOMDataSourceFromDataSource(org.apache.axiom.om.ds.WrappedTextNodeOMDataSourceFromDataSource) OMElement(org.apache.axiom.om.OMElement) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource)

Example 10 with OMDataSource

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

the class TestCloneWithSourcedElement2 method runTest.

@Override
protected void runTest() throws Throwable {
    SOAPEnvelope sourceEnv = soapFactory.getDefaultEnvelope();
    SOAPBody body = sourceEnv.getBody();
    SOAPHeader header = sourceEnv.getHeader();
    // Create a header OMSE
    OMDataSource dsHdr = new StringOMDataSource("<hdr:myheader xmlns:hdr=\"urn://test\">Hello World</hdr:myheader>");
    OMNamespace hdrNS = header.getOMFactory().createOMNamespace("urn://test", "hdr");
    SOAPFactory sf = (SOAPFactory) header.getOMFactory();
    SOAPHeaderBlock shb = sf.createSOAPHeaderBlock("myheader", hdrNS, dsHdr);
    // test setting processing flag
    shb.setProcessed();
    header.addChild(shb);
    // Create a payload
    OMDataSource ds = new StringOMDataSource("<tns:payload xmlns:tns=\"urn://test\">Hello World</tns:payload>");
    OMNamespace ns = body.getOMFactory().createOMNamespace("urn://test", "tns");
    OMSourcedElement omse = body.getOMFactory().createOMElement(ds, "payload", ns);
    body.addChild(omse);
    copyAndCheck(sourceEnv);
    // The source SOAPHeaderBlock should not be expanded in the process
    assertFalse(shb.isExpanded());
}
Also used : SOAPBody(org.apache.axiom.soap.SOAPBody) OMNamespace(org.apache.axiom.om.OMNamespace) OMDataSource(org.apache.axiom.om.OMDataSource) StringOMDataSource(org.apache.axiom.om.ds.StringOMDataSource) StringOMDataSource(org.apache.axiom.om.ds.StringOMDataSource) SOAPHeaderBlock(org.apache.axiom.soap.SOAPHeaderBlock) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) SOAPHeader(org.apache.axiom.soap.SOAPHeader) SOAPFactory(org.apache.axiom.soap.SOAPFactory)

Aggregations

OMDataSource (org.apache.axiom.om.OMDataSource)12 OMSourcedElement (org.apache.axiom.om.OMSourcedElement)8 OMFactory (org.apache.axiom.om.OMFactory)6 OMElement (org.apache.axiom.om.OMElement)4 OMNamespace (org.apache.axiom.om.OMNamespace)4 StringOMDataSource (org.apache.axiom.om.ds.StringOMDataSource)4 StringReader (java.io.StringReader)3 SOAPBody (org.apache.axiom.soap.SOAPBody)3 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)3 PullOMDataSource (org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource)3 QName (javax.xml.namespace.QName)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 OMCloneOptions (org.apache.axiom.om.OMCloneOptions)2 AbstractPushOMDataSource (org.apache.axiom.om.ds.AbstractPushOMDataSource)2 CustomBuilder (org.apache.axiom.om.ds.custombuilder.CustomBuilder)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Random (java.util.Random)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 ByteArrayDataSource (org.apache.axiom.attachments.ByteArrayDataSource)1