Search in sources :

Example 66 with OMNamespace

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

the class TestDeclareNamespaceWithGeneratedPrefix3 method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMElement element = factory.createOMElement(new QName("test"));
    OMNamespace ns = element.declareNamespace("urn:ns", null);
    assertEquals("urn:ns", ns.getNamespaceURI());
    assertNotNull(ns.getPrefix());
    assertTrue(ns.getPrefix().length() > 0);
    Iterator<OMNamespace> it = element.getAllDeclaredNamespaces();
    assertTrue(it.hasNext());
    OMNamespace ns2 = it.next();
    assertEquals(ns, ns2);
    assertFalse(it.hasNext());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNamespace(org.apache.axiom.om.OMNamespace) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement)

Example 67 with OMNamespace

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

the class TestFindNamespaceByPrefix method runTest.

@Override
protected void runTest() throws Throwable {
    OMElement root = AXIOMUtil.stringToOM(metaFactory.getOMFactory(), "<a:root xmlns:a='urn:a'><child/></a:root>");
    OMNamespace ns = root.getFirstElement().findNamespace(null, "a");
    assertNotNull(ns);
    assertEquals("urn:a", ns.getNamespaceURI());
    root.close(false);
}
Also used : OMNamespace(org.apache.axiom.om.OMNamespace) OMElement(org.apache.axiom.om.OMElement)

Example 68 with OMNamespace

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

the class TestName1DefaultPrefix method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory f = metaFactory.getOMFactory();
    // Create OMSE with an unknown prefix
    OMNamespace rootNS = f.createOMNamespace("http://sampleroot", "rootPrefix");
    OMNamespace ns = f.createOMNamespace("http://www.sosnoski.com/uwjws/library", null);
    OMElement element = f.createOMElement(new PullOMDataSource(TestDocument.DOCUMENT1.getContent()), "library", ns);
    OMElement root = f.createOMElement("root", rootNS);
    root.addChild(element);
    // Test getting the local name and namespace URI. This should used not result in expansion
    assertTrue(element.getLocalName().equals("library"));
    assertTrue(element.getNamespace().getNamespaceURI().equals("http://www.sosnoski.com/uwjws/library"));
    // Serialize and cache.  This should cause expansion.  The prefix should be updated to match the testDocument string
    StringWriter writer = new StringWriter();
    root.serialize(writer);
    String result = writer.toString();
    assertTrue(element.getLocalName().equals("library"));
    assertTrue(element.getNamespace().getNamespaceURI().equals("http://www.sosnoski.com/uwjws/library"));
    assertTrue(element.getNamespace().getPrefix().equals(""));
    assertTrue(element.getDefaultNamespace() != null);
    assertTrue("Serialized text error" + result, result.indexOf("1930110111") > 0);
    // Serialize again
    writer = new StringWriter();
    root.serialize(writer);
    result = writer.toString();
    assertTrue(element.getLocalName().equals("library"));
    assertTrue(element.getNamespace().getNamespaceURI().equals("http://www.sosnoski.com/uwjws/library"));
    assertTrue(element.getNamespace().getPrefix().equals(""));
    assertTrue(element.getDefaultNamespace() != null);
    assertTrue("Serialized text error" + result, result.indexOf("1930110111") > 0);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNamespace(org.apache.axiom.om.OMNamespace) PullOMDataSource(org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource) StringWriter(java.io.StringWriter) OMElement(org.apache.axiom.om.OMElement)

Example 69 with OMNamespace

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

Example 70 with OMNamespace

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

Aggregations

OMNamespace (org.apache.axiom.om.OMNamespace)171 OMElement (org.apache.axiom.om.OMElement)108 OMFactory (org.apache.axiom.om.OMFactory)101 QName (javax.xml.namespace.QName)34 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)22 OMAttribute (org.apache.axiom.om.OMAttribute)18 PullOMDataSource (org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource)16 StringWriter (java.io.StringWriter)15 SOAPHeaderBlock (org.apache.axiom.soap.SOAPHeaderBlock)13 SOAPHeader (org.apache.axiom.soap.SOAPHeader)12 OMSourcedElement (org.apache.axiom.om.OMSourcedElement)9 XMLStreamReader (javax.xml.stream.XMLStreamReader)8 OMText (org.apache.axiom.om.OMText)7 Iterator (java.util.Iterator)6 SOAPBody (org.apache.axiom.soap.SOAPBody)6 StringReader (java.io.StringReader)5 StringOMDataSource (org.apache.axiom.om.ds.StringOMDataSource)5 HashSet (java.util.HashSet)4 OMDataSource (org.apache.axiom.om.OMDataSource)4 OMNode (org.apache.axiom.om.OMNode)4