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;
}
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;
}
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;
}
}
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;
}
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()));
}
Aggregations