Search in sources :

Example 6 with OMNamespace

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

the class CharacterEncodingTest method runTest.

public void runTest(String value, String expected) throws XMLStreamException, FactoryConfigurationError, IOException {
    SOAPFactory factory = OMAbstractFactory.getSOAP12Factory();
    SOAPEnvelope envelope = factory.getDefaultEnvelope();
    String ns = "http://testuri.org";
    OMNamespace namespace = factory.createOMNamespace(ns, "tst");
    String ln = "Child";
    OMElement bodyChild = factory.createOMElement(ln, namespace);
    bodyChild.addChild(factory.createOMText(value));
    envelope.getBody().addChild(bodyChild);
    ByteArrayOutputStream byteOutStr = new ByteArrayOutputStream();
    OMOutputFormat outputFormat = new OMOutputFormat();
    outputFormat.setCharSetEncoding(UTF_16);
    envelope.serialize(byteOutStr, outputFormat);
    ByteArrayInputStream byteInStr = new ByteArrayInputStream(byteOutStr.toByteArray());
    SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(byteInStr, UTF_16);
    SOAPEnvelope resultEnv = builder.getSOAPEnvelope();
    OMElement bodyChildResult = resultEnv.getBody().getFirstElement();
    assertNotNull("No child in body element", bodyChildResult);
    String result = bodyChildResult.getText();
    assertNotNull("No value for testParam param", result);
    assertEquals("Expected result not received.", expected, result);
    builder.close();
}
Also used : OMNamespace(org.apache.axiom.om.OMNamespace) ByteArrayInputStream(java.io.ByteArrayInputStream) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SOAPModelBuilder(org.apache.axiom.soap.SOAPModelBuilder) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) SOAPFactory(org.apache.axiom.soap.SOAPFactory)

Example 7 with OMNamespace

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

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

the class OMAttributeHelperTest method testImportOMAttribute.

public void testImportOMAttribute() {
    //Convert from OM to DOOM.
    OMFactory omf = OMAbstractFactory.getOMFactory();
    OMNamespace ns1 = omf.createOMNamespace("http://nsurl", "prefix");
    OMAttribute attr1 = omf.createOMAttribute("attr1", ns1, "attr1value");
    OMFactory doomf = OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM).getOMFactory();
    OMElement ome1 = doomf.createOMElement("element", ns1.getNamespaceURI(), ns1.getPrefix());
    AttributeHelper.importOMAttribute(attr1, ome1);
    assertNotSame(attr1, ome1.getAttribute(attr1.getQName()));
    assertEquals(attr1.getAttributeValue(), ome1.getAttribute(attr1.getQName()).getAttributeValue());
    //Convert from DOOM to OM.
    OMNamespace ns2 = doomf.createOMNamespace("http://nsurl", "prefix");
    OMAttribute attr2 = doomf.createOMAttribute("attr2", ns2, "attr2value");
    OMElement ome2 = omf.createOMElement("element", ns2.getNamespaceURI(), ns2.getPrefix());
    AttributeHelper.importOMAttribute(attr2, ome2);
    assertNotSame(attr2, ome2.getAttribute(attr2.getQName()));
    assertEquals(attr2.getAttributeValue(), ome2.getAttribute(attr2.getQName()).getAttributeValue());
    //OM only.
    OMNamespace ns3 = omf.createOMNamespace("http://nsurl", "prefix");
    OMAttribute attr3 = omf.createOMAttribute("attr3", ns3, "attr3value");
    OMElement ome3 = omf.createOMElement("element", ns3.getNamespaceURI(), ns3.getPrefix());
    AttributeHelper.importOMAttribute(attr3, ome3);
    assertSame(attr3, ome3.getAttribute(attr3.getQName()));
    //DOOM only.
    OMNamespace ns4 = doomf.createOMNamespace("http://nsurl", "prefix");
    OMAttribute attr4 = doomf.createOMAttribute("attr4", ns4, "attr4value");
    OMElement ome4 = doomf.createOMElement("element", ns4.getNamespaceURI(), ns4.getPrefix());
    AttributeHelper.importOMAttribute(attr4, ome4);
    assertSame(attr4, ome4.getAttribute(attr4.getQName()));
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNamespace(org.apache.axiom.om.OMNamespace) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 9 with OMNamespace

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

the class OMAttributeHelperTest method testDetachedElement.

public void testDetachedElement() {
    OMNamespace top = OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM).getOMFactory().createOMNamespace("urn:test1", "t1");
    OMElement ome = OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM).getOMFactory().createOMElement("test", top);
    OMElement child = OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM).getOMFactory().createOMElement(new QName("test"), ome);
    OMAttribute oma = child.addAttribute("attr", "value", top);
    OMElement target = OMAbstractFactory.getOMFactory().createOMElement("test2", "urn:test2", "t2");
    AttributeHelper.importOMAttribute(oma, target);
}
Also used : OMNamespace(org.apache.axiom.om.OMNamespace) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 10 with OMNamespace

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

the class TestSetValueOnNamespaceDeclaration method runTest.

@Override
protected void runTest() throws Throwable {
    Document doc = ((DOMMetaFactory) metaFactory).newDocumentBuilderFactory().newDocumentBuilder().newDocument();
    Element element = doc.createElementNS("", "test");
    Attr attr = doc.createAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:attr");
    element.setAttributeNodeNS(attr);
    attr.setValue("urn:test");
    Iterator<OMNamespace> it = ((OMElement) element).getAllDeclaredNamespaces();
    assertThat(it.hasNext()).isTrue();
    OMNamespace ns = it.next();
    assertThat(ns.getNamespaceURI()).isEqualTo("urn:test");
    assertThat(it.hasNext()).isFalse();
}
Also used : DOMMetaFactory(org.apache.axiom.om.dom.DOMMetaFactory) OMNamespace(org.apache.axiom.om.OMNamespace) OMElement(org.apache.axiom.om.OMElement) Element(org.w3c.dom.Element) OMElement(org.apache.axiom.om.OMElement) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr)

Aggregations

OMNamespace (org.apache.axiom.om.OMNamespace)164 OMElement (org.apache.axiom.om.OMElement)101 OMFactory (org.apache.axiom.om.OMFactory)94 QName (javax.xml.namespace.QName)33 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)20 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