Search in sources :

Example 6 with NamespaceContext

use of javax.xml.namespace.NamespaceContext in project webservices-axiom by apache.

the class TestGetNamespaceContext method runTest.

@Override
protected void runTest() throws Throwable {
    OMElement element = AXIOMUtil.stringToOM(metaFactory.getOMFactory(), "<a xmlns='urn:ns1' xmlns:ns2='urn:ns2'><b xmlns:ns3='urn:ns3'/></a>");
    XMLStreamReader stream = cache ? element.getXMLStreamReader() : element.getXMLStreamReaderWithoutCaching();
    stream.next();
    assertEquals(XMLStreamReader.START_ELEMENT, stream.next());
    assertEquals("b", stream.getLocalName());
    NamespaceContext context = stream.getNamespaceContext();
    assertEquals("urn:ns1", context.getNamespaceURI(""));
    assertEquals("urn:ns2", context.getNamespaceURI("ns2"));
    assertEquals("urn:ns3", context.getNamespaceURI("ns3"));
    assertEquals("ns2", context.getPrefix("urn:ns2"));
    element.close(false);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) NamespaceContext(javax.xml.namespace.NamespaceContext) OMElement(org.apache.axiom.om.OMElement)

Example 7 with NamespaceContext

use of javax.xml.namespace.NamespaceContext in project webservices-axiom by apache.

the class OMSerializerUtil method isAssociated.

/**
     * @param prefix 
     * @param namespace
     * @param writer
     * @return true if the prefix is associated with the namespace in the current context
     */
public static boolean isAssociated(String prefix, String namespace, XMLStreamWriter writer) throws XMLStreamException {
    // of this issue.
    if ("xml".equals(prefix)) {
        return true;
    }
    // NOTE: Calling getNamespaceContext() on many XMLStreamWriter implementations is expensive.
    // Please use other writer methods first.
    // For consistency, convert null arguments.
    // This helps get around the parser implementation differences.
    // In addition, the getPrefix/getNamespace methods cannot be called with null parameters.
    prefix = (prefix == null) ? "" : prefix;
    namespace = (namespace == null) ? "" : namespace;
    if (namespace.length() > 0) {
        // QUALIFIED NAMESPACE
        // Get the namespace associated with the prefix
        String writerPrefix = writer.getPrefix(namespace);
        if (prefix.equals(writerPrefix)) {
            return true;
        }
        // So try getting the namespace as a second step.
        if (writerPrefix != null) {
            NamespaceContext nsContext = writer.getNamespaceContext();
            if (nsContext != null) {
                String writerNS = nsContext.getNamespaceURI(prefix);
                return namespace.equals(writerNS);
            }
        }
        return false;
    } else {
        // Neither XML 1.0 nor XML 1.1 allow to associate a prefix with an unqualified name (see also AXIOM-372).
        if (prefix.length() > 0) {
            throw new OMException("Invalid namespace declaration: Prefixed namespace bindings may not be empty.");
        }
        // protected
        try {
            String writerPrefix = writer.getPrefix("");
            if (writerPrefix != null && writerPrefix.length() == 0) {
                return true;
            }
        } catch (Throwable t) {
            if (log.isDebugEnabled()) {
                log.debug("Caught exception from getPrefix(\"\"). Processing continues: " + t);
            }
        }
        // Fallback to using the namespace context
        NamespaceContext nsContext = writer.getNamespaceContext();
        if (nsContext != null) {
            String writerNS = nsContext.getNamespaceURI("");
            if (writerNS != null && writerNS.length() > 0) {
                return false;
            }
        }
        return true;
    }
}
Also used : NamespaceContext(javax.xml.namespace.NamespaceContext) OMException(org.apache.axiom.om.OMException)

Example 8 with NamespaceContext

use of javax.xml.namespace.NamespaceContext 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 9 with NamespaceContext

use of javax.xml.namespace.NamespaceContext in project ddf by codice.

the class MockNamespaceResolver method getNamespaceURI.

public String getNamespaceURI(String prefix) {
    String methodName = "getNamespaceURI";
    LOGGER.trace("ENTERING: {}", methodName);
    String namespaceUri = null;
    for (NamespaceContext nc : namespaceContexts) {
        namespaceUri = nc.getNamespaceURI(prefix);
        if (namespaceUri != null) {
            break;
        }
    }
    LOGGER.trace("EXITING: {}    (namespaceUri = {} )", methodName, namespaceUri);
    return namespaceUri;
}
Also used : NamespaceContext(javax.xml.namespace.NamespaceContext)

Example 10 with NamespaceContext

use of javax.xml.namespace.NamespaceContext in project ddf by codice.

the class MockNamespaceResolver method getPrefix.

public String getPrefix(String namespace) {
    String methodName = "getPrefix";
    LOGGER.trace("ENTERING: {}   namespace = {}", methodName, namespace);
    String prefix = null;
    for (NamespaceContext nc : namespaceContexts) {
        prefix = nc.getPrefix(namespace);
        if (prefix != null) {
            break;
        }
    }
    LOGGER.trace("EXITING: {}    (prefix = {} )", methodName, prefix);
    return prefix;
}
Also used : NamespaceContext(javax.xml.namespace.NamespaceContext)

Aggregations

NamespaceContext (javax.xml.namespace.NamespaceContext)23 XPath (javax.xml.xpath.XPath)7 InputStream (java.io.InputStream)5 Iterator (java.util.Iterator)5 XPathExpression (javax.xml.xpath.XPathExpression)5 InputSource (org.xml.sax.InputSource)5 OMElement (org.apache.axiom.om.OMElement)4 Node (org.w3c.dom.Node)4 Charset (java.nio.charset.Charset)3 XMLStreamReader (javax.xml.stream.XMLStreamReader)3 XPathExpressionException (javax.xml.xpath.XPathExpressionException)3 XPathFactory (javax.xml.xpath.XPathFactory)3 IDataNamespaceContext (permafrost.tundra.xml.namespace.IDataNamespaceContext)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HashSet (java.util.HashSet)2 OMXMLStreamReaderConfiguration (org.apache.axiom.om.OMXMLStreamReaderConfiguration)2 NodeList (org.w3c.dom.NodeList)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Date (java.util.Date)1 QName (javax.xml.namespace.QName)1