Search in sources :

Example 1 with XPathVariableResolver

use of javax.xml.xpath.XPathVariableResolver in project jstl-api by eclipse-ee4j.

the class JSTLNodeList method valueOf.

/**
 * Evaluate an XPath expression to a String value.
 */
public String valueOf(Node contextNode, String xpathString) throws JspTagException {
    // p("******** valueOf(" + n + ", " + xpathString + ")");
    XPathVariableResolver jxvr = new JSTLXPathVariableResolver(pageContext);
    XPath xpath = XPATH_FACTORY.newXPath();
    xpath.setNamespaceContext(jstlXPathNamespaceContext);
    xpath.setXPathVariableResolver(jxvr);
    try {
        return xpath.evaluate(xpathString, contextNode);
    } catch (XPathExpressionException ex) {
        throw new JspTagException(ex.toString(), ex);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpressionException(javax.xml.xpath.XPathExpressionException) XPathVariableResolver(javax.xml.xpath.XPathVariableResolver) JspTagException(jakarta.servlet.jsp.JspTagException)

Example 2 with XPathVariableResolver

use of javax.xml.xpath.XPathVariableResolver in project jstl-api by eclipse-ee4j.

the class JSTLNodeList method selectSingleNode.

/**
 * Evaluate an XPath expression to a single node.
 */
public Node selectSingleNode(Node contextNode, String xpathString) throws JspTagException {
    // p("selectSingleNode of XPathUtil = passed node:" +
    // "xpathString => " + n + " : " + xpathString );
    XPathVariableResolver jxvr = new JSTLXPathVariableResolver(pageContext);
    try {
        XPath xpath = XPATH_FACTORY.newXPath();
        xpath.setNamespaceContext(jstlXPathNamespaceContext);
        xpath.setXPathVariableResolver(jxvr);
        return (Node) xpath.evaluate(xpathString, contextNode, XPathConstants.NODE);
    } catch (XPathExpressionException ex) {
        throw new JspTagException(ex.toString(), ex);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) XPathVariableResolver(javax.xml.xpath.XPathVariableResolver) JspTagException(jakarta.servlet.jsp.JspTagException)

Example 3 with XPathVariableResolver

use of javax.xml.xpath.XPathVariableResolver in project jstl-api by eclipse-ee4j.

the class JSTLNodeList method selectNodes.

/**
 * Evaluate an XPath expression to a List of nodes.
 */
public List<Node> selectNodes(Node contextNode, String xpathString) throws JspTagException {
    XPathVariableResolver jxvr = new JSTLXPathVariableResolver(pageContext);
    try {
        XPath xpath = XPATH_FACTORY.newXPath();
        xpath.setNamespaceContext(jstlXPathNamespaceContext);
        xpath.setXPathVariableResolver(jxvr);
        Object nl = xpath.evaluate(xpathString, contextNode, JSTLXPathConstants.OBJECT);
        return new JSTLNodeList(nl);
    } catch (XPathExpressionException ex) {
        throw new JspTagException(ex.toString(), ex);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpressionException(javax.xml.xpath.XPathExpressionException) XPathVariableResolver(javax.xml.xpath.XPathVariableResolver) JspTagException(jakarta.servlet.jsp.JspTagException)

Example 4 with XPathVariableResolver

use of javax.xml.xpath.XPathVariableResolver in project jvarkit by lindenb.

the class VcfFilterXPath method doWork.

@Override
public int doWork(List<String> args) {
    if (this.infoTag == null || this.infoTag.isEmpty()) {
        LOG.error("Info Tag is undefined");
        return -1;
    }
    if (this.xpathExpression == null || this.xpathExpression.isEmpty()) {
        LOG.error("XPath Expression is undefined");
        return -1;
    }
    for (final String s : __prefix2uri) {
        int eq = s.indexOf('=');
        if (eq <= 0) {
            LOG.error("'=' missing in " + s);
            return -1;
        }
        this.prefix2uri.put(s.substring(0, eq), s.substring(eq + 1));
    }
    try {
        XPathFactory xpf = XPathFactory.newInstance();
        XPath xpath = xpf.newXPath();
        xpath.setXPathVariableResolver(new XPathVariableResolver() {

            @Override
            public Object resolveVariable(QName qname) {
                return xpathVariableMap.get(qname);
            }
        });
        xpath.setNamespaceContext(new NamespaceContext() {

            @Override
            public Iterator<String> getPrefixes(String namespaceURI) {
                final List<String> L = new ArrayList<>();
                for (final String pfx : prefix2uri.keySet()) {
                    if (prefix2uri.get(pfx).equals(namespaceURI)) {
                        L.add(pfx);
                    }
                }
                return L.iterator();
            }

            @Override
            public String getPrefix(final String namespaceURI) {
                for (String pfx : prefix2uri.keySet()) {
                    if (prefix2uri.get(pfx).equals(namespaceURI)) {
                        return pfx;
                    }
                }
                return null;
            }

            @Override
            public String getNamespaceURI(String prefix) {
                return prefix2uri.get(prefix);
            }
        });
        this.xpathExpr = xpath.compile(this.xpathExpression);
        return doVcfToVcf(args, outputFile);
    } catch (Exception er) {
        LOG.error(er);
        return -1;
    }
}
Also used : XPath(javax.xml.xpath.XPath) QName(javax.xml.namespace.QName) XPathVariableResolver(javax.xml.xpath.XPathVariableResolver) XPathFactory(javax.xml.xpath.XPathFactory) NamespaceContext(javax.xml.namespace.NamespaceContext) VCFIterator(htsjdk.variant.vcf.VCFIterator) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with XPathVariableResolver

use of javax.xml.xpath.XPathVariableResolver in project jbpm by kiegroup.

the class XPATHReturnValueEvaluator method evaluate.

public Object evaluate(final ProcessContext context) throws Exception {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpathEvaluator = factory.newXPath();
    xpathEvaluator.setXPathFunctionResolver(new XPathFunctionResolver() {

        public XPathFunction resolveFunction(QName functionName, int arity) {
            String localName = functionName.getLocalPart();
            if ("getVariable".equals(localName)) {
                return new GetVariableData();
            } else {
                throw new IllegalArgumentException("Unknown BPMN function: " + functionName);
            }
        }

        class GetVariableData implements XPathFunction {

            public Object evaluate(List args) throws XPathFunctionException {
                String varname = (String) args.get(0);
                return context.getVariable(varname);
            }
        }
    });
    xpathEvaluator.setXPathVariableResolver(new XPathVariableResolver() {

        public Object resolveVariable(QName variableName) {
            return context.getVariable(variableName.getLocalPart());
        }
    });
    xpathEvaluator.setNamespaceContext(new NamespaceContext() {

        private static final String DROOLS_NAMESPACE_URI = "http://www.jboss.org/drools";

        private String[] prefixes = { "drools", "bpmn2" };

        @Override
        public Iterator getPrefixes(String namespaceURI) {
            return Arrays.asList(prefixes).iterator();
        }

        @Override
        public String getPrefix(String namespaceURI) {
            if (DROOLS_NAMESPACE_URI.equalsIgnoreCase(namespaceURI)) {
                return "bpmn2";
            }
            return null;
        }

        @Override
        public String getNamespaceURI(String prefix) {
            if ("bpmn2".equalsIgnoreCase(prefix)) {
                return DROOLS_NAMESPACE_URI;
            }
            return null;
        }
    });
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    return xpathEvaluator.evaluate(this.expression, builder.newDocument(), XPathConstants.BOOLEAN);
}
Also used : XPath(javax.xml.xpath.XPath) QName(javax.xml.namespace.QName) XPathFunction(javax.xml.xpath.XPathFunction) XPathVariableResolver(javax.xml.xpath.XPathVariableResolver) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NamespaceContext(javax.xml.namespace.NamespaceContext) XPathFunctionResolver(javax.xml.xpath.XPathFunctionResolver) Iterator(java.util.Iterator) List(java.util.List)

Aggregations

XPath (javax.xml.xpath.XPath)7 XPathVariableResolver (javax.xml.xpath.XPathVariableResolver)7 JspTagException (jakarta.servlet.jsp.JspTagException)4 XPathExpressionException (javax.xml.xpath.XPathExpressionException)4 Iterator (java.util.Iterator)3 List (java.util.List)3 NamespaceContext (javax.xml.namespace.NamespaceContext)3 QName (javax.xml.namespace.QName)3 XPathFactory (javax.xml.xpath.XPathFactory)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 XPathFunction (javax.xml.xpath.XPathFunction)2 XPathFunctionResolver (javax.xml.xpath.XPathFunctionResolver)2 VCFIterator (htsjdk.variant.vcf.VCFIterator)1 ArrayList (java.util.ArrayList)1 Node (org.w3c.dom.Node)1