Search in sources :

Example 1 with CustomBuilder

use of org.apache.axiom.om.ds.custombuilder.CustomBuilder 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 2 with CustomBuilder

use of org.apache.axiom.om.ds.custombuilder.CustomBuilder in project webservices-axiom by apache.

the class FirstElementNameWithParserTestCase method runTest.

@Override
protected final void runTest() throws Throwable {
    SOAPEnvelope orgEnvelope = soapFactory.getDefaultEnvelope();
    orgEnvelope.getBody().addChild(soapFactory.createOMElement(qname.getLocalPart(), qname.getNamespaceURI(), qname.getPrefix()));
    SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, new StringReader(orgEnvelope.toString()));
    SOAPBody body = builder.getSOAPEnvelope().getBody();
    runTest(body);
    if (supportsOptimization) {
        // The expectation is that even after looking at the payload element name, registering
        // a custom builder still transforms the element.
        ((CustomBuilderSupport) builder).registerCustomBuilder(CustomBuilder.Selector.PAYLOAD, new CustomBuilder() {

            @Override
            public OMDataSource create(OMElement element) throws OMException {
                try {
                    element.getXMLStreamReaderWithoutCaching().close();
                } catch (XMLStreamException ex) {
                    throw new OMException(ex);
                }
                return new AbstractPushOMDataSource() {

                    @Override
                    public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException {
                        xmlWriter.writeEmptyElement(qname.getPrefix(), qname.getLocalPart(), qname.getNamespaceURI());
                    }

                    @Override
                    public boolean isDestructiveWrite() {
                        return false;
                    }
                };
            }
        });
        assertThat(body.getFirstElement()).isInstanceOf(OMSourcedElement.class);
    }
}
Also used : OMDataSource(org.apache.axiom.om.OMDataSource) AbstractPushOMDataSource(org.apache.axiom.om.ds.AbstractPushOMDataSource) CustomBuilderSupport(org.apache.axiom.om.ds.custombuilder.CustomBuilderSupport) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) CustomBuilder(org.apache.axiom.om.ds.custombuilder.CustomBuilder) SOAPBody(org.apache.axiom.soap.SOAPBody) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StringReader(java.io.StringReader) AbstractPushOMDataSource(org.apache.axiom.om.ds.AbstractPushOMDataSource) SOAPModelBuilder(org.apache.axiom.soap.SOAPModelBuilder) OMException(org.apache.axiom.om.OMException)

Aggregations

OMDataSource (org.apache.axiom.om.OMDataSource)2 CustomBuilder (org.apache.axiom.om.ds.custombuilder.CustomBuilder)2 StringReader (java.io.StringReader)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 CoreModelException (org.apache.axiom.core.CoreModelException)1 OMElement (org.apache.axiom.om.OMElement)1 OMException (org.apache.axiom.om.OMException)1 AbstractPushOMDataSource (org.apache.axiom.om.ds.AbstractPushOMDataSource)1 CustomBuilderSupport (org.apache.axiom.om.ds.custombuilder.CustomBuilderSupport)1 OMNamespaceImpl (org.apache.axiom.om.impl.common.OMNamespaceImpl)1 AxiomElement (org.apache.axiom.om.impl.intf.AxiomElement)1 AxiomSourcedElement (org.apache.axiom.om.impl.intf.AxiomSourcedElement)1 SOAPBody (org.apache.axiom.soap.SOAPBody)1 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)1 SOAPModelBuilder (org.apache.axiom.soap.SOAPModelBuilder)1 AxiomSOAP11HeaderBlock (org.apache.axiom.soap.impl.intf.AxiomSOAP11HeaderBlock)1 AxiomSOAP12HeaderBlock (org.apache.axiom.soap.impl.intf.AxiomSOAP12HeaderBlock)1 AxiomSOAPElement (org.apache.axiom.soap.impl.intf.AxiomSOAPElement)1 AxiomSOAPHeaderBlock (org.apache.axiom.soap.impl.intf.AxiomSOAPHeaderBlock)1