Search in sources :

Example 1 with XPathSyntaxException

use of org.javarosa.xpath.parser.XPathSyntaxException in project collect by opendatakit.

the class ItemsetWidget method getSelectionArgs.

private String[] getSelectionArgs(List<String> arguments, String nodesetStr, FormController formController) {
    // +1 is for the list_name
    String[] selectionArgs = new String[arguments.size() + 1];
    // parse out the list name, between the ''
    String listName = nodesetStr.substring(nodesetStr.indexOf('\'') + 1, nodesetStr.lastIndexOf('\''));
    // first argument is always listname
    selectionArgs[0] = listName;
    if (formController == null) {
        Timber.w("Can't instantiate ItemsetWidget with a null FormController.");
        return null;
    }
    // loop through the arguments, evaluate any expressions and build the query string for the DB
    for (int i = 0; i < arguments.size(); i++) {
        XPathExpression xpr;
        try {
            xpr = parseTool.parseXPath(arguments.get(i));
        } catch (XPathSyntaxException e) {
            Timber.e(e);
            TextView error = new TextView(getContext());
            error.setText(String.format(getContext().getString(R.string.parser_exception), arguments.get(i)));
            addAnswerView(error);
            break;
        }
        if (xpr != null) {
            FormDef form = formController.getFormDef();
            TreeElement treeElement = form.getMainInstance().resolveReference(formEntryPrompt.getIndex().getReference());
            EvaluationContext ec = new EvaluationContext(form.getEvaluationContext(), treeElement.getRef());
            Object value = xpr.eval(form.getMainInstance(), ec);
            if (value == null) {
                return null;
            } else {
                if (value instanceof XPathNodeset) {
                    value = ((XPathNodeset) value).getValAt(0);
                }
                selectionArgs[i + 1] = value.toString();
            }
        }
    }
    return selectionArgs;
}
Also used : XPathExpression(org.javarosa.xpath.expr.XPathExpression) XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException) FormDef(org.javarosa.core.model.FormDef) TextView(android.widget.TextView) XPathNodeset(org.javarosa.xpath.XPathNodeset) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) SuppressLint(android.annotation.SuppressLint) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 2 with XPathSyntaxException

use of org.javarosa.xpath.parser.XPathSyntaxException in project collect by opendatakit.

the class ExternalDataUtil method getSearchXPathExpression.

public static XPathFuncExpr getSearchXPathExpression(String appearance) {
    if (appearance == null) {
        appearance = "";
    }
    appearance = appearance.trim();
    Matcher matcher = SEARCH_FUNCTION_REGEX.matcher(appearance);
    if (matcher.find()) {
        String function = matcher.group(0);
        try {
            XPathExpression xpathExpression = XPathParseTool.parseXPath(function);
            if (XPathFuncExpr.class.isAssignableFrom(xpathExpression.getClass())) {
                XPathFuncExpr xpathFuncExpr = (XPathFuncExpr) xpathExpression;
                if (xpathFuncExpr.id.name.equalsIgnoreCase(ExternalDataHandlerSearch.HANDLER_NAME)) {
                    // also check that the args are either 1, 4 or 6.
                    if (xpathFuncExpr.args.length == 1 || xpathFuncExpr.args.length == 4 || xpathFuncExpr.args.length == 6) {
                        return xpathFuncExpr;
                    } else {
                        Toast.makeText(Collect.getInstance(), Collect.getInstance().getString(R.string.ext_search_wrong_arguments_error), Toast.LENGTH_SHORT).show();
                        Timber.i(Collect.getInstance().getString(R.string.ext_search_wrong_arguments_error));
                        return null;
                    }
                } else {
                    // this might mean a problem in the regex above. Unit tests required.
                    Toast.makeText(Collect.getInstance(), Collect.getInstance().getString(R.string.ext_search_wrong_function_error, xpathFuncExpr.id.name), Toast.LENGTH_SHORT).show();
                    Timber.i(Collect.getInstance().getString(R.string.ext_search_wrong_function_error, xpathFuncExpr.id.name));
                    return null;
                }
            } else {
                // this might mean a problem in the regex above. Unit tests required.
                Toast.makeText(Collect.getInstance(), Collect.getInstance().getString(R.string.ext_search_bad_function_error, function), Toast.LENGTH_SHORT).show();
                Timber.i(Collect.getInstance().getString(R.string.ext_search_bad_function_error, function));
                return null;
            }
        } catch (XPathSyntaxException e) {
            Toast.makeText(Collect.getInstance(), Collect.getInstance().getString(R.string.ext_search_generic_error, appearance), Toast.LENGTH_SHORT).show();
            Timber.i(Collect.getInstance().getString(R.string.ext_search_generic_error, appearance));
            return null;
        }
    } else {
        return null;
    }
}
Also used : XPathExpression(org.javarosa.xpath.expr.XPathExpression) XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException) Matcher(java.util.regex.Matcher) XPathFuncExpr(org.javarosa.xpath.expr.XPathFuncExpr)

Example 3 with XPathSyntaxException

use of org.javarosa.xpath.parser.XPathSyntaxException in project javarosa by opendatakit.

the class XPathEvalTest method testEval.

private void testEval(String expr, FormInstance model, EvaluationContext ec, Object expected) {
    XPathExpression xpe = null;
    boolean exceptionExpected = (expected instanceof XPathException);
    if (ec == null) {
        ec = new EvaluationContext(model);
    }
    try {
        xpe = XPathParseTool.parseXPath(expr);
    } catch (XPathSyntaxException xpse) {
        System.out.println("Parsing " + expr + " failed with " + xpse.toString());
    }
    if (xpe == null) {
        fail("Null expression or syntax error");
    }
    try {
        Object result = xpe.eval(model, ec);
        if (exceptionExpected) {
            fail("Expected exception, expression : " + expr);
        } else if ((result instanceof Double && expected instanceof Double)) {
            if (!expected.equals(result)) {
                System.out.println(String.format("%s result %s differed from expected %s", expr, result, expected));
            }
            assertEquals((Double) expected, (Double) result, 1e-12);
        } else if (result instanceof XPathNodeset && expected instanceof XPathNodeset) {
            XPathNodeset expectedAsXPathNodeset = (XPathNodeset) expected;
            XPathNodeset resultAsXPathNodeset = (XPathNodeset) result;
            assertEquals(expectedAsXPathNodeset.size(), resultAsXPathNodeset.size());
            assertEquals(expectedAsXPathNodeset.getRefAt(0), resultAsXPathNodeset.getRefAt(0));
            assertEquals(expectedAsXPathNodeset.unpack(), resultAsXPathNodeset.unpack());
            assertEquals(expectedAsXPathNodeset.toArgList().length, resultAsXPathNodeset.toArgList().length);
            for (int i = 0; i < expectedAsXPathNodeset.toArgList().length; i++) {
                assertEquals(expectedAsXPathNodeset.toArgList()[i], resultAsXPathNodeset.toArgList()[i]);
            }
        } else {
            assertEquals(expected, result);
        }
    } catch (XPathException xpex) {
        if (!exceptionExpected) {
            fail("Unexpected exception: " + xpex);
        } else if (xpex.getClass() != expected.getClass()) {
            fail("Did not get expected exception type");
        }
    }
}
Also used : XPathExpression(org.javarosa.xpath.expr.XPathExpression) XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException) XPathException(org.javarosa.xpath.XPathException) XPathNodeset(org.javarosa.xpath.XPathNodeset) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext)

Example 4 with XPathSyntaxException

use of org.javarosa.xpath.parser.XPathSyntaxException in project javarosa by opendatakit.

the class StandardBindAttributesProcessor method buildCondition.

private Condition buildCondition(String xpath, String type, IDataReference contextRef) {
    final int trueAction;
    final int falseAction;
    final String prettyType;
    switch(type) {
        case "relevant":
            prettyType = "display";
            trueAction = Condition.ACTION_SHOW;
            falseAction = Condition.ACTION_HIDE;
            break;
        case "required":
            prettyType = "require";
            trueAction = Condition.ACTION_REQUIRE;
            falseAction = Condition.ACTION_DONT_REQUIRE;
            break;
        case "readonly":
            prettyType = "readonly";
            trueAction = Condition.ACTION_DISABLE;
            falseAction = Condition.ACTION_ENABLE;
            break;
        default:
            throw new XFormParseException("Unsupported type " + type + " passed to buildCondition");
    }
    final XPathConditional xPathConditional;
    try {
        xPathConditional = new XPathConditional(xpath);
    } catch (XPathSyntaxException xse) {
        String errorMessage = "Encountered a problem with " + prettyType + " condition for node [" + contextRef.getReference().toString() + "] at line: " + xpath + ", " + xse.getMessage();
        reporter.error(errorMessage);
        throw new XFormParseException(errorMessage);
    }
    return new Condition(xPathConditional, trueAction, falseAction, FormInstance.unpackReference(contextRef));
}
Also used : Condition(org.javarosa.core.model.condition.Condition) XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException) XPathConditional(org.javarosa.xpath.XPathConditional)

Example 5 with XPathSyntaxException

use of org.javarosa.xpath.parser.XPathSyntaxException in project javarosa by opendatakit.

the class XFormParser method parseOutput.

private String parseOutput(Element e) {
    List<String> usedAtts = new ArrayList<>();
    usedAtts.add(REF_ATTR);
    usedAtts.add(VALUE);
    String xpath = e.getAttributeValue(null, REF_ATTR);
    if (xpath == null) {
        xpath = e.getAttributeValue(null, VALUE);
    }
    if (xpath == null) {
        throw new XFormParseException("XForm Parse: <output> without 'ref' or 'value'", e);
    }
    XPathConditional expr = null;
    try {
        expr = new XPathConditional(xpath);
    } catch (XPathSyntaxException xse) {
        reporter.error("Invalid XPath expression in <output> [" + xpath + "]! " + xse.getMessage());
        return "";
    }
    int index = -1;
    if (_f.getOutputFragments().contains(expr)) {
        index = _f.getOutputFragments().indexOf(expr);
    } else {
        index = _f.getOutputFragments().size();
        _f.getOutputFragments().add(expr);
    }
    if (XFormUtils.showUnusedAttributeWarning(e, usedAtts)) {
        reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(e, usedAtts), getVagueLocation(e));
    }
    return String.valueOf(index);
}
Also used : XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException) ArrayList(java.util.ArrayList) XPathConditional(org.javarosa.xpath.XPathConditional)

Aggregations

XPathSyntaxException (org.javarosa.xpath.parser.XPathSyntaxException)10 XPathExpression (org.javarosa.xpath.expr.XPathExpression)6 XPathConditional (org.javarosa.xpath.XPathConditional)3 XPathException (org.javarosa.xpath.XPathException)3 DataBinding (org.javarosa.core.model.DataBinding)2 IDataReference (org.javarosa.core.model.IDataReference)2 Condition (org.javarosa.core.model.condition.Condition)2 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)2 XPathReference (org.javarosa.model.xform.XPathReference)2 XPathNodeset (org.javarosa.xpath.XPathNodeset)2 SuppressLint (android.annotation.SuppressLint)1 TextView (android.widget.TextView)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Action (org.javarosa.core.model.Action)1 FormDef (org.javarosa.core.model.FormDef)1 SetValueAction (org.javarosa.core.model.actions.SetValueAction)1 Recalculate (org.javarosa.core.model.condition.Recalculate)1 TreeElement (org.javarosa.core.model.instance.TreeElement)1 TreeReference (org.javarosa.core.model.instance.TreeReference)1