Search in sources :

Example 21 with NamespaceContext

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

use of javax.xml.namespace.NamespaceContext in project simba-os by cegeka.

the class Utils method query.

/**
     * Extracts a node from the DOMDocument
     *
     * @param dom     The DOMDocument
     * @param query   Xpath Expresion
     * @param context Context Node (DomElement)
     * @return DOMNodeList The queried node
     * @throws XPathExpressionException
     */
public static NodeList query(Document dom, String query, Node context) throws XPathExpressionException {
    NodeList nodeList;
    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(new NamespaceContext() {

        public String getNamespaceURI(String prefix) {
            String result = null;
            switch(prefix) {
                case "samlp":
                case "samlp2":
                    result = SAMLConstants.NS_SAMLP;
                    break;
                case "saml":
                case "saml2":
                    result = SAMLConstants.NS_SAML;
                    break;
                case "ds":
                    result = SAMLConstants.NS_DS;
                    break;
                case "xenc":
                    result = SAMLConstants.NS_XENC;
                    break;
            }
            return result;
        }

        public String getPrefix(String namespaceURI) {
            return null;
        }

        @SuppressWarnings("rawtypes")
        public Iterator getPrefixes(String namespaceURI) {
            return null;
        }
    });
    if (context == null)
        nodeList = (NodeList) xpath.evaluate(query, dom, XPathConstants.NODESET);
    else
        nodeList = (NodeList) xpath.evaluate(query, context, XPathConstants.NODESET);
    return nodeList;
}
Also used : NamespaceContext(javax.xml.namespace.NamespaceContext) Iterator(java.util.Iterator)

Example 23 with NamespaceContext

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

the class NamespaceResolver method getPrefix.

/**
     * Retrieve the namespace prefix for the given namespace URI. URI is retrieved from the list of
     * namespace URIs and prefixes mapped from NamespaceMap entries in the OSGi Service Registry.
     *
     * @parameter namespace the namespace URI to look up the prefix for
     *
     * @return the namespace prefix for the given namespace URI
     */
public String getPrefix(String namespace) {
    String methodName = "getPrefix";
    LOGGER.trace("ENTERING: {},   namespace = {}", methodName, namespace);
    getNamespaceContexts();
    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