Search in sources :

Example 16 with OMSourcedElement

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

the class TestSetLocalName method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMSourcedElement element = factory.createOMElement(new PullOMDataSource("<p:root xmlns:p='urn:test'><child/></p:root>"), "root", factory.createOMNamespace("urn:test", "p"));
    if (expand) {
        element.getFirstOMChild();
    }
    element.setLocalName("newroot");
    assertAbout(xml()).that(element.toString()).hasSameContentAs("<p:newroot xmlns:p='urn:test'><child/></p:newroot>");
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) PullOMDataSource(org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource) OMSourcedElement(org.apache.axiom.om.OMSourcedElement)

Example 17 with OMSourcedElement

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

the class TestDataHandlerSerializationWithoutMTOM method runTest.

@Override
protected void runTest() throws Throwable {
    SOAPFactory factory = metaFactory.getSOAP11Factory();
    JAXBContext context = JAXBContext.newInstance(DocumentBean.class);
    // Construct the original message
    DocumentBean orgObject = new DocumentBean();
    orgObject.setId("123456");
    orgObject.setContent(new DataHandler("some content", "text/plain; charset=utf-8"));
    SOAPEnvelope orgEnvelope = factory.getDefaultEnvelope();
    OMSourcedElement element = factory.createOMElement(new JAXBOMDataSource(context, orgObject));
    orgEnvelope.getBody().addChild(element);
    // Serialize the message
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    orgEnvelope.serialize(out);
    assertFalse(element.isExpanded());
    SOAPEnvelope envelope = OMXMLBuilderFactory.createSOAPModelBuilder(new ByteArrayInputStream(out.toByteArray()), null).getSOAPEnvelope();
    DocumentBean object = (DocumentBean) context.createUnmarshaller().unmarshal(envelope.getBody().getFirstElement().getXMLStreamReader(false));
    assertEquals("some content", IOUtils.toString(object.getContent().getInputStream(), "utf-8"));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentBean(org.apache.axiom.ts.jaxb.beans.DocumentBean) JAXBContext(javax.xml.bind.JAXBContext) DataHandler(javax.activation.DataHandler) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SOAPFactory(org.apache.axiom.soap.SOAPFactory) JAXBOMDataSource(org.apache.axiom.om.ds.jaxb.JAXBOMDataSource)

Example 18 with OMSourcedElement

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

the class TestAddAttribute method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMSourcedElement element = factory.createOMElement(new PullOMDataSource("<root attr='orgvalue'><child/></root>"), "root", null);
    // Add an attribute before expansion
    OMAttribute attr = strategy.addAttribute(element, "attr", null, "newvalue");
    // Force expansion; this should not overwrite the attribute we just added
    assertThat(element.getFirstOMChild()).isNotNull();
    OMAttribute attr2 = element.getAttribute(new QName("attr"));
    assertThat(attr2).isSameAs(attr);
    assertThat(attr2.getAttributeValue()).isEqualTo("newvalue");
    Iterator<OMAttribute> it = element.getAllAttributes();
    assertThat(it.hasNext()).isTrue();
    assertThat(it.next()).isSameAs(attr);
    assertThat(it.hasNext()).isFalse();
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) PullOMDataSource(org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource) QName(javax.xml.namespace.QName) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 19 with OMSourcedElement

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

the class TestBlobOMDataSource method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    String localName = "myPayload";
    String encoding = "utf-8";
    String payload = "<tns:myPayload xmlns:tns=\"urn://test\">Payload One</tns:myPayload>";
    OMNamespace ns = factory.createOMNamespace("urn://test", "tns");
    BlobOMDataSource ds = new BlobOMDataSource(Blobs.createBlob(payload.getBytes(encoding)), encoding);
    OMElement parent = factory.createOMElement("root", null);
    OMSourcedElement omse = factory.createOMElement(ds, 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()).isSameAs(ds);
    // A BlobOMDataSource does not consume the backing object when read.
    // Thus getting the XMLStreamReader of the BlobOMDataSource should not 
    // cause expansion of the OMSourcedElement.
    XMLStreamReader reader = child.getXMLStreamReader();
    reader.next();
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    // Likewise, a BlobOMDataSource does not consume the backing object when 
    // written.  Thus serializing the OMSourcedElement should not cause the expansion
    // of the OMSourcedElement.
    assertTrue("The payload was not present in the output", parent.toString().indexOf(payload) > 0);
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    // If a consumer calls build or buildWithAttachments on the tree, the 
    // tree should not be expanded.
    parent.build();
    assertTrue("OMSourcedElement is expanded after build().  This is unexpected", !child.isExpanded());
    parent.buildWithAttachments();
    assertTrue("OMSourcedElement is expanded after buildWithAttachments().  This is unexpected", !child.isExpanded());
    // Test getting the raw bytes from the BlobOMDataSource.
    assertThat(child.getDataSource()).isSameAs(ds);
}
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) BlobOMDataSource(org.apache.axiom.om.ds.BlobOMDataSource) OMElement(org.apache.axiom.om.OMElement) OMSourcedElement(org.apache.axiom.om.OMSourcedElement)

Example 20 with OMSourcedElement

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

the class TestCloneUnknownName method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMDataSource ds = new StringOMDataSource("<p:element xmlns:p='urn:ns'>test</p:element>");
    OMSourcedElement element = factory.createOMElement(ds);
    OMCloneOptions options = new OMCloneOptions();
    options.setCopyOMDataSources(true);
    OMElement clone = (OMElement) element.clone(options);
    assertTrue(clone instanceof OMSourcedElement);
    assertFalse(element.isExpanded());
    OMNamespace expectedNS = factory.createOMNamespace("urn:ns", "p");
    assertEquals("element", element.getLocalName());
    assertEquals("element", clone.getLocalName());
    assertEquals(expectedNS, element.getNamespace());
    assertEquals(expectedNS, clone.getNamespace());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) 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) OMCloneOptions(org.apache.axiom.om.OMCloneOptions) 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