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