Search in sources :

Example 6 with OMText

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

the class ElementSerializerTest method testDualNamespaces2.

public void testDualNamespaces2() throws Exception {
    OMFactory factory = OMAbstractFactory.getOMFactory();
    OMNamespace ns1 = factory.createOMNamespace("bar", "x");
    OMElement root = factory.createOMElement("root", ns1);
    OMNamespace ns2 = root.declareNamespace("bar", "y");
    OMElement elt1 = factory.createOMElement("foo", ns1);
    OMElement elt2 = factory.createOMElement("yuck", ns2);
    OMText txt1 = factory.createOMText(elt2, "blah");
    elt2.addChild(txt1);
    elt1.addChild(elt2);
    root.addChild(elt1);
    root.serialize(writer);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNamespace(org.apache.axiom.om.OMNamespace) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement)

Example 7 with OMText

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

the class TestDataHandlerSerializationWithMTOM method runTest.

@Override
protected void runTest() throws Throwable {
    SOAPFactory factory = metaFactory.getSOAP11Factory();
    JAXBContext context = JAXBContext.newInstance(DocumentBean.class);
    // Construct the original message
    DocumentBean object = new DocumentBean();
    object.setId("123456");
    object.setContent(new DataHandler("some content", "text/plain; charset=utf-8"));
    SOAPEnvelope orgEnvelope = factory.getDefaultEnvelope();
    OMSourcedElement element = factory.createOMElement(new JAXBOMDataSource(context, object));
    orgEnvelope.getBody().addChild(element);
    // Serialize the message
    OMOutputFormat format = new OMOutputFormat();
    format.setDoOptimize(true);
    MemoryBlob blob = Blobs.createMemoryBlob();
    OutputStream out = blob.getOutputStream();
    orgEnvelope.serialize(out, format);
    out.close();
    assertFalse(element.isExpanded());
    // Parse the serialized message
    MultipartBody mb = MultipartBody.builder().setInputStream(blob.getInputStream()).setContentType(format.getContentType()).build();
    assertEquals(2, mb.getPartCount());
    SOAPEnvelope envelope = OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, mb).getSOAPEnvelope();
    OMElement contentElement = envelope.getBody().getFirstElement().getFirstChildWithName(new QName("http://ws.apache.org/axiom/test/jaxb", "content"));
    OMText content = (OMText) contentElement.getFirstOMChild();
    assertTrue(content.isBinary());
    assertTrue(content.isOptimized());
    DataHandler dh = content.getDataHandler();
    assertEquals("some content", dh.getContent());
}
Also used : MemoryBlob(org.apache.axiom.blob.MemoryBlob) QName(javax.xml.namespace.QName) OutputStream(java.io.OutputStream) JAXBContext(javax.xml.bind.JAXBContext) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) SOAPFactory(org.apache.axiom.soap.SOAPFactory) JAXBOMDataSource(org.apache.axiom.om.ds.jaxb.JAXBOMDataSource) MultipartBody(org.apache.axiom.mime.MultipartBody) DocumentBean(org.apache.axiom.ts.jaxb.beans.DocumentBean) OMText(org.apache.axiom.om.OMText) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 8 with OMText

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

the class WriteCDataScenario method validate.

@Override
public void validate(OMElement element, boolean dataHandlersPreserved) throws Throwable {
    OMNode child = element.getFirstOMChild();
    Assert.assertTrue(child instanceof OMText);
    OMText text = (OMText) child;
    Assert.assertEquals(OMNode.CDATA_SECTION_NODE, text.getType());
    Assert.assertEquals("content", text.getText());
}
Also used : OMNode(org.apache.axiom.om.OMNode) OMText(org.apache.axiom.om.OMText)

Example 9 with OMText

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

the class TestBase64StreamingWithGetSAXSource method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMElement elem = factory.createOMElement("test", null);
    // Create a data source that would eat up all memory when loaded. If the test
    // doesn't fail with an OutOfMemoryError, we know that the OMText implementation
    // supports streaming.
    DataSource ds = new RandomDataSource(654321L, Runtime.getRuntime().maxMemory());
    OMText text = factory.createOMText(new DataHandler(ds), false);
    elem.addChild(text);
    SAXSource saxSource = elem.getSAXSource(true);
    XMLReader xmlReader = saxSource.getXMLReader();
    xmlReader.setContentHandler(new Base64Comparator(ds.getInputStream()));
    xmlReader.parse(saxSource.getInputSource());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) SAXSource(javax.xml.transform.sax.SAXSource) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) XMLReader(org.xml.sax.XMLReader) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) DataSource(javax.activation.DataSource)

Example 10 with OMText

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

the class TestGetTextCharactersFromDataHandler method runTest.

@Override
protected void runTest() throws Throwable {
    DataHandler dh = new DataHandler("test content", "text/plain; charset=utf-8");
    OMText text = metaFactory.getOMFactory().createOMText(dh, true);
    char[] chars = text.getTextCharacters();
    byte[] decoded = Base64.decodeBase64(new String(chars));
    assertEquals("test content", new String(decoded, "utf-8"));
}
Also used : OMText(org.apache.axiom.om.OMText) DataHandler(javax.activation.DataHandler)

Aggregations

OMText (org.apache.axiom.om.OMText)92 OMElement (org.apache.axiom.om.OMElement)62 OMFactory (org.apache.axiom.om.OMFactory)39 DataHandler (javax.activation.DataHandler)26 OMNode (org.apache.axiom.om.OMNode)21 OMNamespace (org.apache.axiom.om.OMNamespace)10 QName (javax.xml.namespace.QName)8 OMException (org.apache.axiom.om.OMException)8 IOException (java.io.IOException)7 Iterator (java.util.Iterator)7 InputStream (java.io.InputStream)6 OMAttribute (org.apache.axiom.om.OMAttribute)6 Entry (org.apache.synapse.config.Entry)6 ArrayList (java.util.ArrayList)5 DataSource (javax.activation.DataSource)5 ByteArrayDataSource (org.apache.axiom.attachments.ByteArrayDataSource)5 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)5 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)5 OutputStream (java.io.OutputStream)4 StringReader (java.io.StringReader)4