Search in sources :

Example 11 with OMSourcedElement

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

the class TestSerializeOMDataSourceWritingToOutputStream method runTest.

@Override
protected void runTest() throws Throwable {
    OMDataSourceImpl ds = new OMDataSourceImpl();
    OMFactory factory = metaFactory.getOMFactory();
    OMSourcedElement element = factory.createOMElement(ds);
    OMElement elementToSerialize;
    if (serializeParent) {
        OMElement parent = factory.createOMElement("root", null);
        parent.addChild(element);
        elementToSerialize = parent;
    } else {
        elementToSerialize = element;
    }
    assertAbout(xml()).that(serializationStrategy.serialize(elementToSerialize).getInputSource()).hasSameContentAs(serializeParent ? "<root><test xmlns='urn:test'/></root>" : "<test xmlns='urn:test'/>");
    assertThat(ds.isOutputStreamUsed()).isEqualTo(serializationStrategy instanceof SerializeToOutputStream);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) SerializeToOutputStream(org.apache.axiom.ts.dimension.serialization.SerializeToOutputStream) OMElement(org.apache.axiom.om.OMElement) OMSourcedElement(org.apache.axiom.om.OMSourcedElement)

Example 12 with OMSourcedElement

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

the class TestGetObject method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    DataSource ds = new BlobDataSource(Blobs.createBlob("test".getBytes("utf-8")), "text/plain; charset=utf-8");
    OMSourcedElement element = factory.createOMElement(new WrappedTextNodeOMDataSourceFromDataSource(new QName("wrapper"), ds, Charset.forName("utf-8")));
    // getObject returns null if the data source is not of the expected type
    assertNull(element.getObject(StringOMDataSource.class));
    // Test with the right data source type
    assertSame(ds, element.getObject(WrappedTextNodeOMDataSourceFromDataSource.class));
    assertSame(ds, element.getObject(WrappedTextNodeOMDataSource.class));
    // Now modify the content of the element
    factory.createOMComment(element, "comment");
    assertNull(element.getObject(WrappedTextNodeOMDataSourceFromDataSource.class));
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) BlobDataSource(org.apache.axiom.blob.BlobDataSource) QName(javax.xml.namespace.QName) StringOMDataSource(org.apache.axiom.om.ds.StringOMDataSource) WrappedTextNodeOMDataSource(org.apache.axiom.om.ds.WrappedTextNodeOMDataSource) WrappedTextNodeOMDataSourceFromDataSource(org.apache.axiom.om.ds.WrappedTextNodeOMDataSourceFromDataSource) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) WrappedTextNodeOMDataSourceFromDataSource(org.apache.axiom.om.ds.WrappedTextNodeOMDataSourceFromDataSource) DataSource(javax.activation.DataSource) StringOMDataSource(org.apache.axiom.om.ds.StringOMDataSource) BlobDataSource(org.apache.axiom.blob.BlobDataSource) WrappedTextNodeOMDataSource(org.apache.axiom.om.ds.WrappedTextNodeOMDataSource)

Example 13 with OMSourcedElement

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

the class TestGetReaderException method runTest.

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

        @Override
        public XMLStreamReader getReader() throws XMLStreamException {
            throw new XMLStreamException("Test exception");
        }

        @Override
        public boolean isDestructiveRead() {
            return true;
        }
    });
    try {
        element.getLocalName();
        fail("Expected OMException");
    } catch (OMException ex) {
        Throwable cause = ex.getCause();
        assertTrue(cause instanceof XMLStreamException);
        assertEquals("Test exception", cause.getMessage());
    }
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) AbstractPullOMDataSource(org.apache.axiom.om.ds.AbstractPullOMDataSource) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) OMException(org.apache.axiom.om.OMException)

Example 14 with OMSourcedElement

use of org.apache.axiom.om.OMSourcedElement 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 15 with OMSourcedElement

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

the class TestStringOMDataSource method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    String localName = "myPayload";
    String payload1 = "<tns:myPayload xmlns:tns=\"urn://test\">Payload One</tns:myPayload>";
    OMNamespace ns = factory.createOMNamespace("urn://test", "tns");
    StringOMDataSource somds = new StringOMDataSource(payload1);
    OMElement parent = factory.createOMElement("root", null);
    OMSourcedElement omse = factory.createOMElement(somds, localName, ns);
    parent.addChild(omse);
    OMNode firstChild = parent.getFirstOMChild();
    assertTrue("Expected OMSourcedElement child", firstChild instanceof OMSourcedElement);
    OMSourcedElement child = (OMSourcedElement) firstChild;
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    assertThat(child.getDataSource()).isInstanceOf(StringOMDataSource.class);
    // A StringOMDataSource does not consume the backing object when read.
    // Thus getting the XMLStreamReader of the StringOMDataSource should not 
    // cause expansion of the OMSourcedElement.
    XMLStreamReader reader = child.getXMLStreamReader();
    reader.next();
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    // Likewise, a StringOMDataSource does not consume the backing object when 
    // written.  Thus serializing the OMSourcedElement should not cause the expansion
    // of the OMSourcedElement.
    StringWriter out = new StringWriter();
    parent.serialize(out);
    //        System.out.println(output);
    assertTrue("The payload was not present in the output", out.toString().indexOf(payload1) > 0);
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    // Test getting the raw content from the StringOMDataSource.
    StringOMDataSource ds = (StringOMDataSource) child.getDataSource();
    assertThat(ds.getObject()).isEqualTo(payload1);
    // Validate close
    ds.close();
    assertTrue("Close should free the resource", ds.getObject() == null);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNode(org.apache.axiom.om.OMNode) OMNamespace(org.apache.axiom.om.OMNamespace) XMLStreamReader(javax.xml.stream.XMLStreamReader) StringWriter(java.io.StringWriter) StringOMDataSource(org.apache.axiom.om.ds.StringOMDataSource) OMElement(org.apache.axiom.om.OMElement) OMSourcedElement(org.apache.axiom.om.OMSourcedElement)

Aggregations

OMSourcedElement (org.apache.axiom.om.OMSourcedElement)44 OMFactory (org.apache.axiom.om.OMFactory)32 OMElement (org.apache.axiom.om.OMElement)16 PullOMDataSource (org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource)14 QName (javax.xml.namespace.QName)13 StringOMDataSource (org.apache.axiom.om.ds.StringOMDataSource)11 OMNamespace (org.apache.axiom.om.OMNamespace)9 OMDataSource (org.apache.axiom.om.OMDataSource)8 OMNode (org.apache.axiom.om.OMNode)7 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)7 JAXBContext (javax.xml.bind.JAXBContext)6 JAXBOMDataSource (org.apache.axiom.om.ds.jaxb.JAXBOMDataSource)6 XMLStreamReader (javax.xml.stream.XMLStreamReader)5 StringReader (java.io.StringReader)4 OMAttribute (org.apache.axiom.om.OMAttribute)4 DocumentBean (org.apache.axiom.ts.jaxb.beans.DocumentBean)4 DataSource (javax.activation.DataSource)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 BlobOMDataSource (org.apache.axiom.om.ds.BlobOMDataSource)3 WrappedTextNodeOMDataSourceFromDataSource (org.apache.axiom.om.ds.WrappedTextNodeOMDataSourceFromDataSource)3