Search in sources :

Example 1 with XPathSupport

use of org.intellij.plugins.xpathView.support.XPathSupport in project intellij-community by JetBrains.

the class XPathEvalAction method evaluateExpression.

private boolean evaluateExpression(EvalExpressionDialog.Context context, XmlElement contextNode, Editor editor, Config cfg) {
    final Project project = editor.getProject();
    try {
        final XPathSupport support = XPathSupport.getInstance();
        final XPath xpath = support.createXPath((XmlFile) contextNode.getContainingFile(), context.input.expression, context.input.namespaces);
        xpath.setVariableContext(new CachedVariableContext(context.input.variables, xpath, contextNode));
        // evaluate the expression on the whole document
        final Object result = xpath.evaluate(contextNode);
        LOG.debug("result = " + result);
        LOG.assertTrue(result != null, "null result?");
        if (result instanceof List<?>) {
            final List<?> list = (List<?>) result;
            if (!list.isEmpty()) {
                if (cfg.HIGHLIGHT_RESULTS) {
                    highlightResult(contextNode, editor, list);
                }
                if (cfg.SHOW_USAGE_VIEW) {
                    showUsageView(editor, xpath, contextNode, list);
                }
                if (!cfg.SHOW_USAGE_VIEW && !cfg.HIGHLIGHT_RESULTS) {
                    final String s = StringUtil.pluralize("match", list.size());
                    Messages.showInfoMessage(project, "Expression produced " + list.size() + " " + s, "XPath Result");
                }
            } else {
                return Messages.showOkCancelDialog(project, "Sorry, your expression did not return any result", "XPath Result", "OK", "Edit Expression", Messages.getInformationIcon()) != Messages.OK;
            }
        } else if (result instanceof String) {
            Messages.showMessageDialog("'" + result.toString() + "'", "XPath result (String)", Messages.getInformationIcon());
        } else if (result instanceof Number) {
            Messages.showMessageDialog(result.toString(), "XPath result (Number)", Messages.getInformationIcon());
        } else if (result instanceof Boolean) {
            Messages.showMessageDialog(result.toString(), "XPath result (Boolean)", Messages.getInformationIcon());
        } else {
            LOG.error("Unknown XPath result: " + result);
        }
    } catch (XPathSyntaxException e) {
        LOG.debug(e);
        // TODO: Better layout of the error message with non-fixed size fonts
        return Messages.showOkCancelDialog(project, e.getMultilineMessage(), "XPath syntax error", "Edit Expression", "Cancel", Messages.getErrorIcon()) == Messages.OK;
    } catch (SAXPathException e) {
        LOG.debug(e);
        Messages.showMessageDialog(project, e.getMessage(), "XPath error", Messages.getErrorIcon());
    }
    return false;
}
Also used : XPath(org.jaxen.XPath) Project(com.intellij.openapi.project.Project) CachedVariableContext(org.intellij.plugins.xpathView.util.CachedVariableContext) XPathSyntaxException(org.jaxen.XPathSyntaxException) XPathSupport(org.intellij.plugins.xpathView.support.XPathSupport) SAXPathException(org.jaxen.saxpath.SAXPathException) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

Project (com.intellij.openapi.project.Project)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 XPathSupport (org.intellij.plugins.xpathView.support.XPathSupport)1 CachedVariableContext (org.intellij.plugins.xpathView.util.CachedVariableContext)1 XPath (org.jaxen.XPath)1 XPathSyntaxException (org.jaxen.XPathSyntaxException)1 SAXPathException (org.jaxen.saxpath.SAXPathException)1