Search in sources :

Example 31 with OMElement

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

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

the class TestAddChildFromForeignDocument method runTest.

@Override
protected void runTest() throws Throwable {
    DocumentBuilder db = ((DOMMetaFactory) metaFactory).newDocumentBuilderFactory().newDocumentBuilder();
    Document document1 = db.newDocument();
    Element element1 = document1.createElementNS(null, "element1");
    Document document2 = db.newDocument();
    Element element2 = document2.createElementNS(null, "element2");
    ((OMElement) element1).addChild((OMElement) element2);
    // Assert that the new child is not a copy, but the original element
    assertSame(element2, element1.getFirstChild());
    // Assert that the owner document of element2 was changed
    assertSame(document1, element2.getOwnerDocument());
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) OMElement(org.apache.axiom.om.OMElement) Element(org.w3c.dom.Element) OMElement(org.apache.axiom.om.OMElement) Document(org.w3c.dom.Document)

Example 33 with OMElement

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

the class TestAddAttributeReplace method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMElement element = factory.createOMElement("test", null);
    Document ownerDocument = ((Element) element).getOwnerDocument();
    assertNotNull(ownerDocument);
    OMAttribute attr1 = element.addAttribute("attr", "value1", null);
    assertSame(ownerDocument, ((Attr) attr1).getOwnerDocument());
    OMAttribute attr2 = factory.createOMAttribute("attr", null, "value2");
    element.addAttribute(attr2);
    Document newOwnerDocument = ((Attr) attr1).getOwnerDocument();
    assertNotNull(ownerDocument);
    assertNotSame(ownerDocument, newOwnerDocument);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMElement(org.apache.axiom.om.OMElement) Element(org.w3c.dom.Element) OMElement(org.apache.axiom.om.OMElement) Document(org.w3c.dom.Document) OMAttribute(org.apache.axiom.om.OMAttribute) Attr(org.w3c.dom.Attr)

Example 34 with OMElement

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

the class TestGetName method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMElement root = factory.createOMElement("root", null);
    OMSourcedElement el = factory.createOMElement(new PullOMDataSource("<p:el xmlns:p='urn:ns'>content</p:el>"), "el", factory.createOMNamespace("urn:ns", null));
    root.addChild(el);
    XMLStreamReader reader = root.getXMLStreamReader();
    assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
    assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
    QName name = reader.getName();
    assertEquals("p", name.getPrefix());
    assertEquals("urn:ns", name.getNamespaceURI());
    assertEquals("el", name.getLocalPart());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) PullOMDataSource(org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) OMSourcedElement(org.apache.axiom.om.OMSourcedElement)

Example 35 with OMElement

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

Aggregations

OMElement (org.apache.axiom.om.OMElement)414 OMFactory (org.apache.axiom.om.OMFactory)202 OMNamespace (org.apache.axiom.om.OMNamespace)108 QName (javax.xml.namespace.QName)97 StringReader (java.io.StringReader)63 OMNode (org.apache.axiom.om.OMNode)43 OMText (org.apache.axiom.om.OMText)41 XMLStreamReader (javax.xml.stream.XMLStreamReader)37 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)32 OMAttribute (org.apache.axiom.om.OMAttribute)26 StringWriter (java.io.StringWriter)24 OMXMLParserWrapper (org.apache.axiom.om.OMXMLParserWrapper)21 DataHandler (javax.activation.DataHandler)20 OMDocument (org.apache.axiom.om.OMDocument)19 PullOMDataSource (org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource)18 Element (org.w3c.dom.Element)18 InputStream (java.io.InputStream)16 OMSourcedElement (org.apache.axiom.om.OMSourcedElement)16 ByteArrayInputStream (java.io.ByteArrayInputStream)13 OMException (org.apache.axiom.om.OMException)13