Search in sources :

Example 91 with OMFactory

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

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

the class TestDiscardDocumentElement method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMDocument document = OMXMLBuilderFactory.createOMBuilder(factory, new StringReader("<root><a>text</a></root>")).getDocument();
    OMElement element = document.getOMDocumentElement();
    element.discard();
    assertNull(document.getFirstOMChild());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) StringReader(java.io.StringReader) OMElement(org.apache.axiom.om.OMElement) OMDocument(org.apache.axiom.om.OMDocument)

Example 93 with OMFactory

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

the class TestPushOMDataSourceExpansion method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    PushOMDataSource ds = new PushOMDataSource();
    OMElement element = factory.createOMElement(ds);
    Iterator<Map.Entry<String, String>> it = scenario.getNamespaceContext().entrySet().iterator();
    if (it.hasNext()) {
        Map.Entry<String, String> binding = it.next();
        OMElement parent = factory.createOMElement("parent", factory.createOMNamespace(binding.getValue(), binding.getKey()));
        while (it.hasNext()) {
            binding = it.next();
            parent.declareNamespace(factory.createOMNamespace(binding.getValue(), binding.getKey()));
        }
        parent.addChild(element);
    }
    scenario.validate(element, true);
    assertTrue("Invalid test case: validation didn't expand the OMSourcedElement", ds.isExpanded());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) AbstractPushOMDataSource(org.apache.axiom.om.ds.AbstractPushOMDataSource) OMElement(org.apache.axiom.om.OMElement) Map(java.util.Map)

Example 94 with OMFactory

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

the class TestSerialize method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMSourcedElement element = TestDocument.DOCUMENT1.createOMSourcedElement(factory, push, destructive);
    OMDataSource ds = element.getDataSource();
    OMContainer parent = elementContext.wrap(element);
    boolean parentComplete = parent != null && parent.isComplete();
    expansionStrategy.apply(element);
    boolean consuming = expansionStrategy.isConsumedAfterSerialization(push, destructive, serializationStrategy);
    for (int iteration = 0; iteration < count; iteration++) {
        boolean expectException = iteration != 0 && (consuming || serializeParent && !serializationStrategy.isCaching() && !parentComplete);
        XML result;
        try {
            result = serializationStrategy.serialize(serializeParent ? parent : element);
            if (expectException) {
                fail("Expected exception");
            }
        } catch (Exception ex) {
            if (!expectException) {
                throw ex;
            } else {
                continue;
            }
        }
        InputSource expectedXML = new InputSource(new StringReader(TestDocument.DOCUMENT1.getContent()));
        if (serializeParent) {
            expectedXML = elementContext.getControl(expectedXML);
        }
        assertAbout(xml()).that(result.getInputSource()).hasSameContentAs(expectedXML);
        // the sourced element should be expanded.
        if (expansionStrategy.isExpandedAfterSerialization(push, destructive, serializationStrategy)) {
            assertTrue(element.isExpanded());
            assertEquals("OMSourcedElement completion status", !consuming, element.isComplete());
        } else {
            assertFalse(element.isExpanded());
        }
        if (parent != null && !serializeParent) {
            // Operations on the OMSourcedElement should have no impact on the parent
            assertEquals("Parent completion status", parentComplete, parent.isComplete());
        }
    }
    if (ds instanceof PullOMDataSource) {
        assertFalse(((PullOMDataSource) ds).hasUnclosedReaders());
    }
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) InputSource(org.xml.sax.InputSource) PullOMDataSource(org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource) AbstractPullOMDataSource(org.apache.axiom.om.ds.AbstractPullOMDataSource) PullOMDataSource(org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource) OMDataSource(org.apache.axiom.om.OMDataSource) AbstractPullOMDataSource(org.apache.axiom.om.ds.AbstractPullOMDataSource) AbstractPushOMDataSource(org.apache.axiom.om.ds.AbstractPushOMDataSource) XML(org.apache.axiom.ts.dimension.serialization.XML) StringReader(java.io.StringReader) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) OMContainer(org.apache.axiom.om.OMContainer)

Example 95 with OMFactory

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

the class TestSerializeModifiedOMSEWithNonDestructiveDataSource method runTest.

@Override
protected void runTest() throws Throwable {
    OMDataSourceExt ds = new StringOMDataSource("<element><child/></element>");
    assertFalse(ds.isDestructiveWrite());
    OMFactory f = metaFactory.getOMFactory();
    OMElement element = f.createOMElement(ds, "element", null);
    element.getFirstElement().setText("TEST");
    StringWriter sw = new StringWriter();
    element.serialize(sw);
    assertTrue(sw.toString().indexOf("TEST") != -1);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    element.serialize(baos);
    assertTrue(new String(baos.toByteArray(), "UTF-8").indexOf("TEST") != -1);
    assertTrue(element.toString().indexOf("TEST") != -1);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) StringWriter(java.io.StringWriter) StringOMDataSource(org.apache.axiom.om.ds.StringOMDataSource) OMElement(org.apache.axiom.om.OMElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OMDataSourceExt(org.apache.axiom.om.OMDataSourceExt)

Aggregations

OMFactory (org.apache.axiom.om.OMFactory)270 OMElement (org.apache.axiom.om.OMElement)202 OMNamespace (org.apache.axiom.om.OMNamespace)101 QName (javax.xml.namespace.QName)71 StringReader (java.io.StringReader)37 OMText (org.apache.axiom.om.OMText)35 OMSourcedElement (org.apache.axiom.om.OMSourcedElement)32 OMAttribute (org.apache.axiom.om.OMAttribute)31 PullOMDataSource (org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource)26 StringWriter (java.io.StringWriter)18 OMDocument (org.apache.axiom.om.OMDocument)18 DataHandler (javax.activation.DataHandler)14 XMLStreamReader (javax.xml.stream.XMLStreamReader)14 OMNode (org.apache.axiom.om.OMNode)14 OMException (org.apache.axiom.om.OMException)12 DataSource (javax.activation.DataSource)11 StringOMDataSource (org.apache.axiom.om.ds.StringOMDataSource)9 RandomDataSource (org.apache.axiom.testutils.activation.RandomDataSource)9 OMXMLParserWrapper (org.apache.axiom.om.OMXMLParserWrapper)8 Element (org.w3c.dom.Element)8