Search in sources :

Example 1 with OMXMLStreamReaderConfiguration

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

the class TestGetXMLStreamReaderWithPreserveNamespaceContext method runTest.

@Override
protected void runTest() throws Throwable {
    InputStream in = TestGetXMLStreamReaderWithPreserveNamespaceContext.class.getResourceAsStream("AXIOM-114.xml");
    OMElement root = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), in).getDocumentElement();
    root.declareNamespace("http://example.org", "p");
    OMXMLStreamReaderConfiguration configuration = new OMXMLStreamReaderConfiguration();
    configuration.setPreserveNamespaceContext(preserveNamespaceContext);
    XMLStreamReader reader = root.getFirstElement().getFirstElement().getXMLStreamReader(cache, configuration);
    assertThat(reader.next()).isEqualTo(XMLStreamReader.START_ELEMENT);
    Set<String> prefixes = new HashSet<>();
    for (int i = 0; i < reader.getNamespaceCount(); i++) {
        prefixes.add(reader.getNamespacePrefix(i));
    }
    if (preserveNamespaceContext) {
        assertThat(prefixes).containsExactly("soapenv", "xsd", "xsi", "ns", "p");
    } else {
        assertThat(prefixes).containsExactly("ns");
    }
    // Make sure that we start pulling events directly from the underlying parser.
    reader.nextTag();
    // The following assertions are true irrespective of the value of preserveNamespaceContext.
    assertThat(reader.getNamespaceURI("xsd")).isEqualTo("http://www.w3.org/2001/XMLSchema");
    // Namespace declarations added programmatically on an ancestor should also be visible.
    assertThat(reader.getNamespaceURI("p")).isEqualTo("http://example.org");
    NamespaceContext nc = reader.getNamespaceContext();
    assertThat(nc.getPrefix("http://www.w3.org/2001/XMLSchema")).isEqualTo("xsd");
    assertThat(nc.getPrefix("http://example.org")).isEqualTo("p");
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStream(java.io.InputStream) NamespaceContext(javax.xml.namespace.NamespaceContext) OMElement(org.apache.axiom.om.OMElement) OMXMLStreamReaderConfiguration(org.apache.axiom.om.OMXMLStreamReaderConfiguration) HashSet(java.util.HashSet)

Example 2 with OMXMLStreamReaderConfiguration

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

the class TestGetXMLStreamReaderWithNamespaceURIInterning method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    // Use "new String" to create String objects that are not interned
    OMNamespace ns1 = factory.createOMNamespace(new String("urn:ns1"), "p");
    OMNamespace ns2 = factory.createOMNamespace(new String("urn:ns2"), "q");
    OMElement root = factory.createOMElement("root", ns1);
    root.addAttribute("attr", "value", ns2);
    factory.createOMElement("child", ns2, root);
    OMXMLStreamReaderConfiguration configuration = new OMXMLStreamReaderConfiguration();
    configuration.setNamespaceURIInterning(true);
    XMLStreamReader reader = root.getXMLStreamReader(true, configuration);
    reader.nextTag();
    assertInterned(reader.getNamespaceURI());
    assertInterned(reader.getAttributeNamespace(0));
    for (int i = 0; i < reader.getNamespaceCount(); i++) {
        assertInterned(reader.getNamespaceURI(i));
    }
    reader.nextTag();
    assertInterned(reader.getNamespaceURI("p"));
    NamespaceContext nc = reader.getNamespaceContext();
    assertInterned(nc.getNamespaceURI("p"));
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNamespace(org.apache.axiom.om.OMNamespace) XMLStreamReader(javax.xml.stream.XMLStreamReader) NamespaceContext(javax.xml.namespace.NamespaceContext) OMElement(org.apache.axiom.om.OMElement) OMXMLStreamReaderConfiguration(org.apache.axiom.om.OMXMLStreamReaderConfiguration)

Example 3 with OMXMLStreamReaderConfiguration

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

the class TestGetXMLStreamReaderWithPreserveNamespaceContext2 method runTest.

@Override
protected void runTest() throws Throwable {
    OMElement root = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), TestGetXMLStreamReaderWithPreserveNamespaceContext2.class.getResourceAsStream("AXIOM-485.xml")).getDocumentElement();
    OMElement child = root.getFirstElement();
    OMXMLStreamReaderConfiguration configuration = new OMXMLStreamReaderConfiguration();
    configuration.setPreserveNamespaceContext(true);
    XMLStreamReader reader = child.getXMLStreamReader(true, configuration);
    reader.next();
    assertThat(reader.isStartElement()).isTrue();
    assertThat(reader.getNamespaceCount()).isEqualTo(1);
    assertThat(reader.getNamespacePrefix(0)).isEqualTo("p");
    assertThat(reader.getNamespaceURI(0)).isEqualTo("urn:ns2");
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) OMElement(org.apache.axiom.om.OMElement) OMXMLStreamReaderConfiguration(org.apache.axiom.om.OMXMLStreamReaderConfiguration)

Aggregations

XMLStreamReader (javax.xml.stream.XMLStreamReader)3 OMElement (org.apache.axiom.om.OMElement)3 OMXMLStreamReaderConfiguration (org.apache.axiom.om.OMXMLStreamReaderConfiguration)3 NamespaceContext (javax.xml.namespace.NamespaceContext)2 InputStream (java.io.InputStream)1 HashSet (java.util.HashSet)1 OMFactory (org.apache.axiom.om.OMFactory)1 OMNamespace (org.apache.axiom.om.OMNamespace)1