Search in sources :

Example 1 with PullOMDataSource

use of org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource 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 2 with PullOMDataSource

use of org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource 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 3 with PullOMDataSource

use of org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource 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 4 with PullOMDataSource

use of org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource in project webservices-axiom by apache.

the class TestSetLocalName method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMSourcedElement element = factory.createOMElement(new PullOMDataSource("<p:root xmlns:p='urn:test'><child/></p:root>"), "root", factory.createOMNamespace("urn:test", "p"));
    if (expand) {
        element.getFirstOMChild();
    }
    element.setLocalName("newroot");
    assertAbout(xml()).that(element.toString()).hasSameContentAs("<p:newroot xmlns:p='urn:test'><child/></p:newroot>");
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) PullOMDataSource(org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource) OMSourcedElement(org.apache.axiom.om.OMSourcedElement)

Example 5 with PullOMDataSource

use of org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource in project webservices-axiom by apache.

the class TestAddAttribute method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMSourcedElement element = factory.createOMElement(new PullOMDataSource("<root attr='orgvalue'><child/></root>"), "root", null);
    // Add an attribute before expansion
    OMAttribute attr = strategy.addAttribute(element, "attr", null, "newvalue");
    // Force expansion; this should not overwrite the attribute we just added
    assertThat(element.getFirstOMChild()).isNotNull();
    OMAttribute attr2 = element.getAttribute(new QName("attr"));
    assertThat(attr2).isSameAs(attr);
    assertThat(attr2.getAttributeValue()).isEqualTo("newvalue");
    Iterator<OMAttribute> it = element.getAllAttributes();
    assertThat(it.hasNext()).isTrue();
    assertThat(it.next()).isSameAs(attr);
    assertThat(it.hasNext()).isFalse();
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) PullOMDataSource(org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource) QName(javax.xml.namespace.QName) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Aggregations

PullOMDataSource (org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource)29 OMFactory (org.apache.axiom.om.OMFactory)26 OMElement (org.apache.axiom.om.OMElement)18 OMNamespace (org.apache.axiom.om.OMNamespace)16 OMSourcedElement (org.apache.axiom.om.OMSourcedElement)14 StringWriter (java.io.StringWriter)13 QName (javax.xml.namespace.QName)4 OMAttribute (org.apache.axiom.om.OMAttribute)3 OMDataSource (org.apache.axiom.om.OMDataSource)3 StringReader (java.io.StringReader)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)2 OMNode (org.apache.axiom.om.OMNode)2 AbstractPullOMDataSource (org.apache.axiom.om.ds.AbstractPullOMDataSource)2 OMContainer (org.apache.axiom.om.OMContainer)1 AbstractPushOMDataSource (org.apache.axiom.om.ds.AbstractPushOMDataSource)1 XML (org.apache.axiom.ts.dimension.serialization.XML)1 PushOMDataSource (org.apache.axiom.ts.om.sourcedelement.util.PushOMDataSource)1 InputSource (org.xml.sax.InputSource)1