Example usage for javax.xml.xpath XPath setNamespaceContext

List of usage examples for javax.xml.xpath XPath setNamespaceContext

Introduction

In this page you can find the example usage for javax.xml.xpath XPath setNamespaceContext.

Prototype

public void setNamespaceContext(NamespaceContext nsContext);

Source Link

Document

Establish a namespace context.

Usage

From source file:Main.java

public static Object evaluateXpath(String expression, Object node, QName returnType, NamespaceContext nsContext)
        throws XPathExpressionException {
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    if (nsContext != null) {
        xpath.setNamespaceContext(nsContext);
    }/*from w  w w .  ja  v a 2  s  .  c o m*/
    return xpath.evaluate(expression, node, returnType);
    // XPathExpression xpathExpr = xpath.compile(expression);
    // return xpathExpr.evaluate(node, returnType);
}

From source file:Main.java

/**
 * Construct a xPath expression instance with given expression string and namespace context.
 * If namespace context is not specified a default context is built from the XML node
 * that is evaluated against./*from   w  w  w .ja va  2 s.co  m*/
 * @param xPathExpression
 * @param nsContext
 * @return
 * @throws XPathExpressionException
 */
private static XPathExpression buildExpression(String xPathExpression, NamespaceContext nsContext)
        throws XPathExpressionException {
    XPath xpath = xPathFactory.newXPath();

    if (nsContext != null) {
        xpath.setNamespaceContext(nsContext);
    }

    return xpath.compile(xPathExpression);
}

From source file:Main.java

/**
 * @return/*from   w w  w .j  a v a 2  s. c  om*/
 */
public static XPath getCruxPagesXPath() {
    XPathFactory factory = XPathFactory.newInstance();
    XPath findPath = factory.newXPath();
    findPath.setNamespaceContext(new NamespaceContext() {
        public String getNamespaceURI(String prefix) {
            if (prefix.equals("c")) {
                return "http://www.cruxframework.org/crux";
            } else if (prefix.equals("v")) {
                return "http://www.cruxframework.org/view";
            }

            return "";
        }

        public String getPrefix(String namespaceURI) {
            if (namespaceURI.equals("http://www.cruxframework.org/crux")) {
                return "c";
            } else if (namespaceURI.equals("http://www.cruxframework.org/view")) {
                return "v";
            }
            return "";
        }

        public Iterator<?> getPrefixes(String namespaceURI) {
            List<String> prefixes = new ArrayList<String>();
            prefixes.add("c");
            prefixes.add("v");

            return prefixes.iterator();
        }
    });

    return findPath;
}

From source file:Main.java

public static XPathExpression buildXPath(String path, Map<String, String> map) {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    if (map != null)
        xpath.setNamespaceContext(new NamespaceContext() {

            public Iterator getPrefixes(String namespaceURI) {
                throw new UnsupportedOperationException();
            }// ww w. j a  va 2 s . c  om

            public String getPrefix(String namespaceURI) {
                throw new UnsupportedOperationException();
            }

            public String getNamespaceURI(String prefix) {
                Objects.requireNonNull(prefix);
                if (map.containsKey(prefix))
                    return map.get(prefix);
                return XMLConstants.NULL_NS_URI;
            }
        });

    try {
        return xpath.compile(path);
    } catch (XPathExpressionException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

private static XPath createXPath(NamespaceContext namespaceContext, XPathFunctionResolver functionResolver) {
    XPath xPath = XPathFactory.newInstance().newXPath();
    if (namespaceContext != null) {
        xPath.setNamespaceContext(namespaceContext);
    }/*from  ww w . ja  v  a  2s .  c  o m*/
    if (functionResolver != null) {
        xPath.setXPathFunctionResolver(functionResolver);
    }
    return xPath;
}

From source file:Main.java

public static String getProcessIdFromBpmn(final String bpmn) {
    String processId = null;//from w w w  . j  a v a2  s .co m
    try {
        final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true);
        final Document doc = domFactory.newDocumentBuilder()
                .parse(new ByteArrayInputStream(bpmn.getBytes("UTF-8")));
        final XPath xpath = XPathFactory.newInstance().newXPath();
        xpath.setNamespaceContext(new NamespaceContext() {
            @Override
            public Iterator<?> getPrefixes(final String namespaceURI) {
                // Not used in this context.
                return null;
            }

            @Override
            public String getPrefix(final String namespaceURI) {
                // Not used in this context.
                return null;
            }

            @Override
            public String getNamespaceURI(final String prefix) {
                // Only require the URI for the bpmn2 NS.
                return BPMN2_NAMESPACE_URI;
            }
        });
        final XPathExpression expr = xpath.compile(BPMN_PROCESS_ID_XPATH_EXPR);

        final Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
        processId = node.getAttributes().getNamedItem(BPMN_PROCESS_ID_ATTR).getNodeValue();
    } catch (final Exception e) {
        e.printStackTrace();
    }
    return processId;
}

From source file:Main.java

private static XPathExpression createXPathExpression(String expression, NamespaceContext namespaceContext)
        throws XPathExpressionException {
    XPath xpath = getFactory().newXPath();
    if (namespaceContext != null) {
        xpath.setNamespaceContext(namespaceContext);
    }/* w w  w  .  j a v a 2 s .c o m*/
    XPathExpression expr = xpath.compile(expression);
    return expr;
}

From source file:Main.java

public static XPathExpression newXPath(String xpath, final Map<String, String> namespaces)
        throws XPathExpressionException {
    final XPath xp = newXPathFactory().newXPath();
    xp.setNamespaceContext(new NamespaceContext() {
        @Override//from  ww w  .j  a v  a 2s .c  o m
        public String getNamespaceURI(String prefix) {
            return namespaces != null ? namespaces.get(prefix) : null;
        }

        @Override
        public String getPrefix(String namespaceURI) {
            if (namespaces == null) {
                return null;
            } else {
                final Iterator i = getPrefixes(namespaceURI);
                if (i.hasNext()) {
                    return (String) i.next();
                } else {
                    return null;
                }
            }
        }

        @Override
        public Iterator getPrefixes(String namespaceURI) {
            if (namespaces == null) {
                return null;
            } else {
                ArrayList<String> list = new ArrayList<String>();
                for (Map.Entry<String, String> entry : namespaces.entrySet()) {
                    if (entry.getValue().equals(namespaceURI)) {
                        list.add(entry.getKey());
                    }
                }
                return list.iterator();
            }
        }
    });
    return xp.compile(xpath);
}

From source file:Main.java

private static XPathExpression createXPathExpression(String xpathString) {
    /* XPath *//*from w  w  w  . j  a v a 2  s. co m*/
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(new NamespaceContext() {

        @Override
        public Iterator<?> getPrefixes(String namespaceURI) {
            throw new RuntimeException();
        }

        @Override
        public String getPrefix(String namespaceURI) {
            throw new RuntimeException();
        }

        @Override
        public String getNamespaceURI(String prefix) {
            if ("ds".equals(prefix)) {
                return XMLSignature.XMLNS;
            } else if ("xades".equals(prefix)) {
                return "http://uri.etsi.org/01903/v1.3.2#";
            } else if ("xades141".equals(prefix)) {
                return "http://uri.etsi.org/01903/v1.4.1#";
            } else if ("xades111".equals(prefix)) {
                return "http://uri.etsi.org/01903/v1.1.1#";
            }
            throw new RuntimeException("Prefix not recognized : " + prefix);
        }
    });
    try {
        XPathExpression expr = xpath.compile(xpathString);
        return expr;
    } catch (XPathExpressionException ex) {
        throw new RuntimeException(ex);
    }

}

From source file:Main.java

/**
 * Returns a compiled XPath expression.//w  w  w .j a v a2  s.c o m
 *
 * @param expression                The XPath expression as a String.
 * @param namespaceContext          The namespace context used by the XPath expression.
 * @return                          The compiled XPathExpression.
 * @throws XPathExpressionException If a parsing error occurs.
 */
public static XPathExpression compile(String expression, NamespaceContext namespaceContext)
        throws XPathExpressionException {
    XPath compiler = XPathFactory.newInstance().newXPath();
    if (namespaceContext != null)
        compiler.setNamespaceContext(namespaceContext);
    return compiler.compile(expression);
}