Search in sources :

Example 91 with OMNamespace

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

the class ParserInputStreamDataSourceTests method _testPEDS.

/**
     * @param behavior Behavior (DESTRUCTIVE, NOT_DESTRUCTIVE, ONE_USE_UNSAFE)
     * @param markSupported (indicates if InputStream should be markable)
     * @param scnenario Scenario
     * @return numReads
     * @throws Exception
     */
public int _testPEDS(int behavior, boolean markSupported, int scenario) throws Exception {
    // Create an InputStream
    InputStream is = null;
    if (markSupported) {
        is = new ByteArrayInputStream(payloadText.getBytes("UTF-8"));
    } else {
        is = new NotMarkableInputStream(payloadText.getBytes("UTF-8"));
    }
    // Create a PEDS with the indicated behavior
    ParserInputStreamDataSource peds = new ParserInputStreamDataSource(is, "UTF-8", behavior);
    // Create a OM tree with a root that contains an OMSourcedElement with a PADS
    OMFactory factory = OMAbstractFactory.getOMFactory();
    OMNamespace ns = factory.createOMNamespace("urn://sample", "my");
    OMElement om = factory.createOMElement(peds, "payload", ns);
    QName rootQName = new QName("urn://root", "root", "pre");
    OMElement root = factory.createOMElement(rootQName);
    root.addChild(om);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // Apply the scenario
    if (scenario == Scenario.SER) {
        root.serialize(baos);
        String result = baos.toString("UTF-8");
        assertResult(result);
    } else if (scenario == Scenario.SER_SER) {
        root.serialize(baos);
        String result = baos.toString("UTF-8");
        assertResult(result);
        baos.reset();
        root.serialize(baos);
        result = baos.toString("UTF-8");
        assertResult(result);
    } else if (scenario == Scenario.SAC) {
        root.serializeAndConsume(baos);
        String result = baos.toString("UTF-8");
        assertResult(result);
    } else if (scenario == Scenario.SER_SAC) {
        root.serialize(baos);
        String result = baos.toString("UTF-8");
        assertResult(result);
        baos.reset();
        root.serializeAndConsume(baos);
        result = baos.toString("UTF-8");
        assertResult(result);
    } else if (scenario == Scenario.SAC_SAC) {
        root.serializeAndConsume(baos);
        String result = baos.toString("UTF-8");
        assertResult(result);
        baos.reset();
        root.serializeAndConsume(baos);
    // This second serializeAndConsume is expected to throw an exception.
    }
    return peds.numReads();
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNamespace(org.apache.axiom.om.OMNamespace) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 92 with OMNamespace

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

the class TestAddAttributeWithExistingNamespaceDeclarationOnSameElement method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMElement element = factory.createOMElement(new QName("test"));
    OMNamespace ns = factory.createOMNamespace("urn:ns", "p");
    element.declareNamespace(ns);
    strategy.addAttribute(element, "test", ns, "test");
    Iterator<OMNamespace> it = element.getAllDeclaredNamespaces();
    assertTrue(it.hasNext());
    assertEquals(ns, it.next());
    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 93 with OMNamespace

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

the class TestAddAttributeWithInvalidNamespace2 method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMElement element = factory.createOMElement(new QName("test"));
    OMNamespace ns = factory.createOMNamespace("urn:test", "");
    try {
        element.addAttribute("attr", "value", ns);
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException ex) {
        assertEquals("Cannot create an unprefixed attribute with a namespace", ex.getMessage());
        // No namespace declaration should have been added before throwing the exception
        assertFalse(element.getAllDeclaredNamespaces().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 94 with OMNamespace

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

the class TestAddAttributeWithInvalidNamespace1 method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMElement element = factory.createOMElement(new QName("test"));
    OMNamespace ns = factory.createOMNamespace("", "p");
    try {
        element.addAttribute("attr", "value", ns);
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException ex) {
        assertEquals("Cannot create a prefixed attribute with an empty namespace name", ex.getMessage());
        // No namespace declaration should have been added before throwing the exception
        assertFalse(element.getAllDeclaredNamespaces().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 95 with OMNamespace

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

the class TestAddAttributeWithMaskedNamespaceDeclaration method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMNamespace ns1 = factory.createOMNamespace("urn:ns1", "p");
    OMNamespace ns2 = factory.createOMNamespace("urn:ns2", "p");
    OMElement element1 = factory.createOMElement(new QName("a"));
    element1.declareNamespace(ns1);
    OMElement element2 = factory.createOMElement(new QName("b"), element1);
    element2.declareNamespace(ns2);
    OMElement element3 = factory.createOMElement(new QName("c"), element2);
    strategy.addAttribute(element3, "attr", ns1, "test");
    Iterator<OMNamespace> it = element3.getAllDeclaredNamespaces();
    assertTrue(it.hasNext());
    assertEquals(ns1, it.next());
    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)

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