use of org.javarosa.xpath.parser.XPathSyntaxException in project javarosa by opendatakit.
the class StandardBindAttributesProcessor method createBinding.
DataBinding createBinding(IXFormParserFunctions parserFunctions, FormDef formDef, Collection<String> usedAttributes, Collection<String> passedThroughAttributes, Element element) {
final DataBinding binding = new DataBinding();
binding.setId(element.getAttributeValue("", ID_ATTR));
final String nodeset = element.getAttributeValue(null, NODESET_ATTR);
if (nodeset == null) {
throw new XFormParseException("XForm Parse: <bind> without nodeset", element);
}
IDataReference ref;
try {
ref = new XPathReference(nodeset);
} catch (XPathException xpe) {
throw new XFormParseException(xpe.getMessage());
}
ref = parserFunctions.getAbsRef(ref, formDef);
binding.setReference(ref);
binding.setDataType(getDataType(element.getAttributeValue(null, "type")));
String xpathRel = element.getAttributeValue(null, "relevant");
if (xpathRel != null) {
if ("true()".equals(xpathRel)) {
binding.relevantAbsolute = true;
} else if ("false()".equals(xpathRel)) {
binding.relevantAbsolute = false;
} else {
Condition c = buildCondition(xpathRel, "relevant", ref);
c = (Condition) formDef.addTriggerable(c);
binding.relevancyCondition = c;
}
}
String xpathReq = element.getAttributeValue(null, "required");
if (xpathReq != null) {
if ("true()".equals(xpathReq)) {
binding.requiredAbsolute = true;
} else if ("false()".equals(xpathReq)) {
binding.requiredAbsolute = false;
} else {
Condition c = buildCondition(xpathReq, "required", ref);
c = (Condition) formDef.addTriggerable(c);
binding.requiredCondition = c;
}
}
String xpathRO = element.getAttributeValue(null, "readonly");
if (xpathRO != null) {
if ("true()".equals(xpathRO)) {
binding.readonlyAbsolute = true;
} else if ("false()".equals(xpathRO)) {
binding.readonlyAbsolute = false;
} else {
Condition c = buildCondition(xpathRO, "readonly", ref);
c = (Condition) formDef.addTriggerable(c);
binding.readonlyCondition = c;
}
}
final String xpathConstr = element.getAttributeValue(null, "constraint");
if (xpathConstr != null) {
try {
binding.constraint = new XPathConditional(xpathConstr);
} catch (XPathSyntaxException xse) {
throw new XFormParseException("bind for " + nodeset + " contains invalid constraint expression [" + xpathConstr + "] " + xse.getMessage());
}
binding.constraintMessage = element.getAttributeValue(NAMESPACE_JAVAROSA, "constraintMsg");
}
final String xpathCalc = element.getAttributeValue(null, "calculate");
if (xpathCalc != null) {
Recalculate r;
try {
r = buildCalculate(xpathCalc, ref);
} catch (XPathSyntaxException xpse) {
throw new XFormParseException("Invalid calculate for the bind attached to \"" + nodeset + "\" : " + xpse.getMessage() + " in expression " + xpathCalc);
}
r = (Recalculate) formDef.addTriggerable(r);
binding.calculate = r;
}
binding.setPreload(element.getAttributeValue(NAMESPACE_JAVAROSA, "preload"));
binding.setPreloadParams(element.getAttributeValue(NAMESPACE_JAVAROSA, "preloadParams"));
saveUnusedAttributes(binding, element, usedAttributes, passedThroughAttributes);
return binding;
}
use of org.javarosa.xpath.parser.XPathSyntaxException in project javarosa by opendatakit.
the class XFormParser method parseSetValueAction.
private void parseSetValueAction(FormDef form, Element e) {
String ref = e.getAttributeValue(null, REF_ATTR);
String bind = e.getAttributeValue(null, BIND_ATTR);
String event = e.getAttributeValue(null, "event");
IDataReference dataRef = null;
boolean refFromBind = false;
// TODO: There is a _lot_ of duplication of this code, fix that!
if (bind != null) {
DataBinding binding = bindingsByID.get(bind);
if (binding == null) {
throw new XFormParseException("XForm Parse: invalid binding ID in submit'" + bind + "'", e);
}
dataRef = binding.getReference();
refFromBind = true;
} else if (ref != null) {
dataRef = new XPathReference(ref);
} else {
throw new XFormParseException("setvalue action with no target!", e);
}
if (dataRef != null) {
if (!refFromBind) {
dataRef = FormDef.getAbsRef(dataRef, TreeReference.rootRef());
}
}
String valueRef = e.getAttributeValue(null, "value");
Action action;
TreeReference treeref = FormInstance.unpackReference(dataRef);
actionTargets.add(treeref);
if (valueRef == null) {
if (e.getChildCount() == 0 || !e.isText(0)) {
throw new XFormParseException("No 'value' attribute and no inner value set in <setvalue> associated with: " + treeref, e);
}
// Set expression
action = new SetValueAction(treeref, e.getText(0));
} else {
try {
action = new SetValueAction(treeref, XPathParseTool.parseXPath(valueRef));
} catch (XPathSyntaxException e1) {
Std.printStack(e1);
throw new XFormParseException("Invalid XPath in value set action declaration: '" + valueRef + "'", e);
}
}
form.registerEventListener(event, action);
}
use of org.javarosa.xpath.parser.XPathSyntaxException in project javarosa by opendatakit.
the class XPathReference method getPathExpr.
public static XPathPathExpr getPathExpr(String nodeset) {
XPathExpression path;
boolean validNonPathExpr = false;
try {
path = XPathParseTool.parseXPath(nodeset);
if (!(path instanceof XPathPathExpr)) {
validNonPathExpr = true;
throw new XPathSyntaxException();
}
} catch (XPathSyntaxException xse) {
// make these checked exceptions?
if (validNonPathExpr) {
throw new XPathTypeMismatchException("Expected XPath path, got XPath expression: [" + nodeset + "]," + xse.getMessage());
} else {
Std.printStack(xse);
throw new XPathException("Parse error in XPath path: [" + nodeset + "]." + (xse.getMessage() == null ? "" : "\n" + xse.getMessage()));
}
}
return (XPathPathExpr) path;
}
use of org.javarosa.xpath.parser.XPathSyntaxException in project javarosa by opendatakit.
the class XPathParseTest method testXPathValid.
private void testXPathValid(String expr, String expected) {
try {
XPathExpression xpe = XPathParseTool.parseXPath(expr);
String result = (xpe != null ? xpe.toString() : null);
if (result == null || !result.equals(expected)) {
fail("XPath Parse Failed! Incorrect parse tree." + "\n expression:[" + expr + "]" + "\n expected:[" + expected + "]" + "\n result:[" + result + "]");
}
// test serialization of parse tree
ExternalizableTest.testExternalizable(new ExtWrapTagged(xpe), new ExtWrapTagged(), pf, this, "XPath");
} catch (XPathSyntaxException xse) {
fail("XPath Parse Failed! Unexpected syntax error." + "\n expression:[" + expr + "]");
}
}
use of org.javarosa.xpath.parser.XPathSyntaxException in project javarosa by opendatakit.
the class XPathParseTest method testXPathInvalid.
private void testXPathInvalid(String expr) {
try {
XPathExpression xpe = XPathParseTool.parseXPath(expr);
String result = (xpe != null ? xpe.toString() : null);
fail("XPath Parse Failed! Did not get syntax error as expected." + "\n expression:[" + expr + "]" + "\n result:[" + (result == null ? "(null)" : result) + "]");
} catch (XPathSyntaxException xse) {
// success: syntax error as expected
}
}
Aggregations