Search in sources :

Example 1 with XPathExpression

use of org.javarosa.xpath.expr.XPathExpression 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 XPathExpression

use of org.javarosa.xpath.expr.XPathExpression in project collect by opendatakit.

the class FormController method getQuestionPromptRequiredText.

public String getQuestionPromptRequiredText(FormIndex index) {
    // look for the text under the requiredMsg bind attribute
    String constraintText = getBindAttribute(index, XFormParser.NAMESPACE_JAVAROSA, "requiredMsg");
    if (constraintText != null) {
        XPathExpression xpathRequiredMsg;
        try {
            xpathRequiredMsg = XPathParseTool.parseXPath("string(" + constraintText + ")");
        } catch (Exception e) {
            // This is a string literal, so no need to evaluate anything.
            return constraintText;
        }
        if (xpathRequiredMsg != null) {
            try {
                FormDef form = formEntryController.getModel().getForm();
                TreeElement treeElement = form.getMainInstance().resolveReference(index.getReference());
                EvaluationContext ec = new EvaluationContext(form.getEvaluationContext(), treeElement.getRef());
                Object value = xpathRequiredMsg.eval(form.getMainInstance(), ec);
                if (!value.equals("")) {
                    return (String) value;
                }
                return null;
            } catch (Exception e) {
                Timber.e(e, "Error evaluating a valid-looking required xpath ");
                return constraintText;
            }
        } else {
            return constraintText;
        }
    }
    return null;
}
Also used : XPathExpression(org.javarosa.xpath.expr.XPathExpression) FormDef(org.javarosa.core.model.FormDef) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) IOException(java.io.IOException) JavaRosaException(org.odk.collect.android.exception.JavaRosaException) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 3 with XPathExpression

use of org.javarosa.xpath.expr.XPathExpression 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 4 with XPathExpression

use of org.javarosa.xpath.expr.XPathExpression in project javarosa by opendatakit.

the class TreeElement method tryBatchChildFetch.

@Override
public List<TreeReference> tryBatchChildFetch(String name, int mult, List<XPathExpression> predicates, EvaluationContext evalContext) {
    // Only do for predicates
    if (mult != TreeReference.INDEX_UNBOUND || predicates == null) {
        return null;
    }
    List<Integer> toRemove = new ArrayList<Integer>();
    List<TreeReference> selectedChildren = null;
    // Lazy init these until we've determined that our predicate is hintable
    HashMap<XPathPathExpr, String> indices = null;
    List<TreeElement> kids = null;
    predicate: for (int i = 0; i < predicates.size(); ++i) {
        XPathExpression xpe = predicates.get(i);
        // something we index with something static.
        if (xpe instanceof XPathEqExpr) {
            XPathExpression left = ((XPathEqExpr) xpe).a;
            XPathExpression right = ((XPathEqExpr) xpe).b;
            // handling attribute based referencing with very reasonable timing, but it's complex otherwise)
            if (left instanceof XPathPathExpr && right instanceof XPathStringLiteral) {
                // don't want the overhead if our predicate is too complex anyway
                if (indices == null) {
                    indices = new HashMap<XPathPathExpr, String>();
                    kids = this.getChildrenWithName(name);
                    if (kids.size() == 0) {
                        return null;
                    }
                    // Anything that we're going to use across elements should be on all of them
                    TreeElement kid = kids.get(0);
                    for (int j = 0; j < kid.getAttributeCount(); ++j) {
                        String attribute = kid.getAttributeName(j);
                        indices.put(XPathReference.getPathExpr("@" + attribute), attribute);
                    }
                }
                for (XPathPathExpr expr : indices.keySet()) {
                    if (expr.equals(left)) {
                        String attributeName = indices.get(expr);
                        for (TreeElement kid : kids) {
                            if (kid.getAttributeValue(null, attributeName).equals(((XPathStringLiteral) right).s)) {
                                if (selectedChildren == null) {
                                    selectedChildren = new ArrayList<TreeReference>();
                                }
                                selectedChildren.add(kid.getRef());
                            }
                        }
                        // Note that this predicate is evaluated and doesn't need to be evaluated in the future.
                        toRemove.add(DataUtil.integer(i));
                        continue predicate;
                    }
                }
            }
        }
        // so otherwise, just get outta here.
        break;
    }
    // if we weren't able to evaluate any predicates, signal that.
    if (selectedChildren == null) {
        return null;
    }
    // otherwise, remove all of the predicates we've already evaluated
    for (int i = toRemove.size() - 1; i >= 0; i--) {
        predicates.remove(toRemove.get(i).intValue());
    }
    return selectedChildren;
}
Also used : XPathExpression(org.javarosa.xpath.expr.XPathExpression) HashMap(java.util.HashMap) XPathPathExpr(org.javarosa.xpath.expr.XPathPathExpr) ArrayList(java.util.ArrayList) Constraint(org.javarosa.core.model.condition.Constraint) XPathStringLiteral(org.javarosa.xpath.expr.XPathStringLiteral) XPathEqExpr(org.javarosa.xpath.expr.XPathEqExpr)

Example 5 with XPathExpression

use of org.javarosa.xpath.expr.XPathExpression in project javarosa by opendatakit.

the class TreeReferenceLevel method readExternal.

public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
    name = ExtUtil.nullIfEmpty(ExtUtil.readString(in));
    multiplicity = ExtUtil.readInt(in);
    predicates = (List<XPathExpression>) ExtUtil.nullIfEmpty((List<XPathExpression>) ExtUtil.read(in, new ExtWrapListPoly()));
}
Also used : XPathExpression(org.javarosa.xpath.expr.XPathExpression) ExtWrapListPoly(org.javarosa.core.util.externalizable.ExtWrapListPoly)

Aggregations

XPathExpression (org.javarosa.xpath.expr.XPathExpression)14 XPathSyntaxException (org.javarosa.xpath.parser.XPathSyntaxException)6 XPathPathExpr (org.javarosa.xpath.expr.XPathPathExpr)5 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)4 FormDef (org.javarosa.core.model.FormDef)3 TreeElement (org.javarosa.core.model.instance.TreeElement)3 XPathNodeset (org.javarosa.xpath.XPathNodeset)3 ArrayList (java.util.ArrayList)2 TreeReference (org.javarosa.core.model.instance.TreeReference)2 XPathException (org.javarosa.xpath.XPathException)2 XPathFuncExpr (org.javarosa.xpath.expr.XPathFuncExpr)2 SuppressLint (android.annotation.SuppressLint)1 Cursor (android.database.Cursor)1 TextView (android.widget.TextView)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Vector (java.util.Vector)1