Search in sources :

Example 1 with AbstractPushOMDataSource

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

the class TestGetSAXSourceWithPushOMDataSource method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMSourcedElement sourcedElement = factory.createOMElement(new AbstractPushOMDataSource() {

        @Override
        public void serialize(XMLStreamWriter writer) throws XMLStreamException {
            scenario.serialize(writer);
        }

        @Override
        public boolean isDestructiveWrite() {
            return false;
        }
    });
    Iterator<Map.Entry<String, String>> it = scenario.getNamespaceContext().entrySet().iterator();
    OMElement parent;
    if (it.hasNext()) {
        Map.Entry<String, String> binding = it.next();
        parent = factory.createOMElement("parent", factory.createOMNamespace(binding.getValue(), binding.getKey()));
        while (it.hasNext()) {
            binding = it.next();
            parent.declareNamespace(factory.createOMNamespace(binding.getValue(), binding.getKey()));
        }
    } else {
        parent = factory.createOMElement("parent", null);
    }
    parent.addChild(sourcedElement);
    SAXSource saxSource = (serializeParent ? parent : sourcedElement).getSAXSource(true);
    OMElement element = OMXMLBuilderFactory.createOMBuilder(factory, saxSource, false).getDocumentElement();
    if (serializeParent) {
        element = element.getFirstElement();
    }
    scenario.validate(element, false);
}
Also used : OMElement(org.apache.axiom.om.OMElement) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) OMFactory(org.apache.axiom.om.OMFactory) SAXSource(javax.xml.transform.sax.SAXSource) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) AbstractPushOMDataSource(org.apache.axiom.om.ds.AbstractPushOMDataSource) Map(java.util.Map)

Example 2 with AbstractPushOMDataSource

use of org.apache.axiom.om.ds.AbstractPushOMDataSource 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)

Example 3 with AbstractPushOMDataSource

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

the class TestGetSAXSourceWithPushOMDataSourceThrowingException method runTest.

@Override
protected void runTest() throws Throwable {
    OMElement element = metaFactory.getOMFactory().createOMElement(new AbstractPushOMDataSource() {

        @Override
        public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException {
            throw new XMLStreamException("TEST");
        }

        @Override
        public boolean isDestructiveWrite() {
            return false;
        }
    });
    SAXSource saxSource = element.getSAXSource(true);
    XMLReader reader = saxSource.getXMLReader();
    reader.setContentHandler(SAX.createNullContentHandler());
    try {
        reader.parse(saxSource.getInputSource());
        fail("Expected SAXException");
    } catch (SAXException ex) {
        Throwable cause = ex.getCause();
        assertThat(cause).isInstanceOf(XMLStreamException.class);
        assertThat(cause.getMessage()).isEqualTo("TEST");
    }
}
Also used : SAXSource(javax.xml.transform.sax.SAXSource) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) AbstractPushOMDataSource(org.apache.axiom.om.ds.AbstractPushOMDataSource) OMElement(org.apache.axiom.om.OMElement) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Aggregations

XMLStreamException (javax.xml.stream.XMLStreamException)3 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)3 OMElement (org.apache.axiom.om.OMElement)3 AbstractPushOMDataSource (org.apache.axiom.om.ds.AbstractPushOMDataSource)3 SAXSource (javax.xml.transform.sax.SAXSource)2 StringReader (java.io.StringReader)1 Map (java.util.Map)1 OMDataSource (org.apache.axiom.om.OMDataSource)1 OMException (org.apache.axiom.om.OMException)1 OMFactory (org.apache.axiom.om.OMFactory)1 OMSourcedElement (org.apache.axiom.om.OMSourcedElement)1 CustomBuilder (org.apache.axiom.om.ds.custombuilder.CustomBuilder)1 CustomBuilderSupport (org.apache.axiom.om.ds.custombuilder.CustomBuilderSupport)1 SOAPBody (org.apache.axiom.soap.SOAPBody)1 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)1 SOAPModelBuilder (org.apache.axiom.soap.SOAPModelBuilder)1 SAXException (org.xml.sax.SAXException)1 XMLReader (org.xml.sax.XMLReader)1