Search in sources :

Example 11 with NamespaceContext

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

the class NamespaceResolver method getNamespaceContexts.

private void getNamespaceContexts() {
    // Determine the OSGi bundle context for the NamespaceResolver
    if (this.bundleContext == null) {
        LOGGER.debug("Setting bundleContext");
        this.bundleContext = BundleReference.class.cast(this.getClass().getClassLoader()).getBundle().getBundleContext();
    }
    namespaceContexts = new ArrayList<NamespaceContext>();
    if (bundleContext != null) {
        ServiceReference[] refs = null;
        try {
            // Retrieve all of the namespace mappings from the OSGi Service Registry
            refs = bundleContext.getServiceReferences(NamespaceContext.class.getName(), null);
            LOGGER.debug("num NamespaceContexts service refs found = {}", refs.length);
        } catch (InvalidSyntaxException e) {
            LOGGER.debug("Invalid NamespaceContext syntax", e);
        }
        // If no NamespaceMaps found, nothing further to be done
        if (refs == null || refs.length == 0) {
            LOGGER.debug("No NamespaceContext services found");
        } else {
            // maintained for prefix-to-uri and uri=to-prefix
            for (ServiceReference ref : refs) {
                NamespaceContext namespaceContext = (NamespaceContext) bundleContext.getService(ref);
                if (namespaceContext != null) {
                    namespaceContexts.add(namespaceContext);
                } else {
                    LOGGER.debug("NamespaceContext for ServiceReference was null");
                }
            }
        }
    } else {
        LOGGER.debug("BundleContext is NULL");
    }
}
Also used : NamespaceContext(javax.xml.namespace.NamespaceContext) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleReference(org.osgi.framework.BundleReference) ServiceReference(org.osgi.framework.ServiceReference)

Example 12 with NamespaceContext

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

the class NamespaceResolver method getNamespaceURI.

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

use of javax.xml.namespace.NamespaceContext in project camel by apache.

the class XAdESSignaturePropertiesTest method getXpath.

static XPathExpression getXpath(String xpathString, final Map<String, String> prefix2Namespace) throws XPathExpressionException {
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    NamespaceContext nc = new NamespaceContext() {

        @SuppressWarnings("rawtypes")
        @Override
        public Iterator getPrefixes(String namespaceURI) {
            return null;
        }

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

        @Override
        public String getNamespaceURI(String prefix) {
            return prefix2Namespace.get(prefix);
        }
    };
    xpath.setNamespaceContext(nc);
    XPathExpression expr = xpath.compile(xpathString);
    return expr;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) NamespaceContext(javax.xml.namespace.NamespaceContext)

Example 14 with NamespaceContext

use of javax.xml.namespace.NamespaceContext in project camel by apache.

the class XmlSignatureTest method checkXpath.

private Object checkXpath(MockEndpoint mock, String xpathString, final Map<String, String> prefix2Namespace) throws XPathExpressionException, SAXException, IOException, ParserConfigurationException {
    Message mess = getMessage(mock);
    InputStream body = mess.getBody(InputStream.class);
    assertNotNull(body);
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    NamespaceContext nc = new NamespaceContext() {

        @SuppressWarnings("rawtypes")
        @Override
        public Iterator getPrefixes(String namespaceURI) {
            return null;
        }

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

        @Override
        public String getNamespaceURI(String prefix) {
            return prefix2Namespace.get(prefix);
        }
    };
    xpath.setNamespaceContext(nc);
    XPathExpression expr = xpath.compile(xpathString);
    Object result = expr.evaluate(XmlSignatureHelper.newDocumentBuilder(true).parse(body), XPathConstants.NODE);
    assertNotNull("The xpath " + xpathString + " returned a null value", result);
    return result;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) Message(org.apache.camel.Message) XmlSignature2Message(org.apache.camel.component.xmlsecurity.api.XmlSignature2Message) InputStream(java.io.InputStream) NamespaceContext(javax.xml.namespace.NamespaceContext)

Example 15 with NamespaceContext

use of javax.xml.namespace.NamespaceContext in project openhab1-addons by openhab.

the class IhcResourceInteractionService method parseList.

private NodeList parseList(String xml, String xpathExpression) throws XPathExpressionException, UnsupportedEncodingException {
    InputStream is = new ByteArrayInputStream(xml.getBytes("UTF8"));
    XPath xpath = XPathFactory.newInstance().newXPath();
    InputSource inputSource = new InputSource(is);
    xpath.setNamespaceContext(new NamespaceContext() {

        @Override
        public String getNamespaceURI(String prefix) {
            if (prefix == null) {
                throw new NullPointerException("Null prefix");
            } else if ("SOAP-ENV".equals(prefix)) {
                return "http://schemas.xmlsoap.org/soap/envelope/";
            } else if ("ns1".equals(prefix)) {
                return "utcs";
            } else if ("ns2".equals(prefix)) {
                return "utcs.values";
            }
            return null;
        }

        @Override
        public String getPrefix(String uri) {
            return null;
        }

        @Override
        @SuppressWarnings("rawtypes")
        public Iterator getPrefixes(String uri) {
            throw new UnsupportedOperationException();
        }
    });
    return (NodeList) xpath.evaluate(xpathExpression, inputSource, XPathConstants.NODESET);
}
Also used : XPath(javax.xml.xpath.XPath) InputSource(org.xml.sax.InputSource) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NamespaceContext(javax.xml.namespace.NamespaceContext) NodeList(org.w3c.dom.NodeList) Iterator(java.util.Iterator)

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