use of org.javarosa.core.model.DataBinding in project javarosa by opendatakit.
the class XFormParser method parseSubmission.
private void parseSubmission(Element submission) {
String id = submission.getAttributeValue(null, ID_ATTR);
// These two are always required
String method = submission.getAttributeValue(null, "method");
String action = submission.getAttributeValue(null, "action");
SubmissionParser parser = new SubmissionParser();
for (SubmissionParser p : submissionParsers) {
if (p.matchesCustomMethod(method)) {
parser = p;
}
}
// These two might exist, but if neither do, we just assume you want the entire instance.
String ref = submission.getAttributeValue(null, REF_ATTR);
String bind = submission.getAttributeValue(null, BIND_ATTR);
IDataReference dataRef = null;
boolean refFromBind = false;
if (bind != null) {
DataBinding binding = bindingsByID.get(bind);
if (binding == null) {
throw new XFormParseException("XForm Parse: invalid binding ID in submit'" + bind + "'", submission);
}
dataRef = binding.getReference();
refFromBind = true;
} else if (ref != null) {
dataRef = new XPathReference(ref);
} else {
// no reference! No big deal, assume we want the root reference
dataRef = new XPathReference("/");
}
if (dataRef != null) {
if (!refFromBind) {
dataRef = FormDef.getAbsRef(dataRef, TreeReference.rootRef());
}
}
SubmissionProfile profile = parser.parseSubmission(method, action, dataRef, submission);
if (id == null) {
// default submission profile
_f.setDefaultSubmission(profile);
} else {
// typed submission profile
_f.addSubmissionProfile(id, profile);
}
}
use of org.javarosa.core.model.DataBinding in project javarosa by opendatakit.
the class XFormParser method parseControl.
/**
* Parses a form control element into a {@link org.javarosa.core.model.QuestionDef} and attaches it to its parent.
*
* @param parent the form control element's parent
* @param e the form control element to parse
* @param controlType one of the control types defined in {@link org.javarosa.core.model.Constants}
* @param additionalUsedAtts attributes specific to the control type
* @param passedThroughAtts attributes specific to the control type that should be passed through to
* additionalAttributes for historical reasons
* @return a {@link org.javarosa.core.model.QuestionDef} representing the form control element
*/
private QuestionDef parseControl(IFormElement parent, Element e, int controlType, List<String> additionalUsedAtts, List<String> passedThroughAtts) {
final QuestionDef question = questionForControlType(controlType);
// until we come up with a better scheme
question.setID(serialQuestionID++);
final List<String> usedAtts = new ArrayList<>(Arrays.asList(REF_ATTR, BIND_ATTR, APPEARANCE_ATTR));
if (additionalUsedAtts != null) {
usedAtts.addAll(additionalUsedAtts);
}
IDataReference dataRef = null;
boolean refFromBind = false;
String ref = e.getAttributeValue(null, REF_ATTR);
String bind = e.getAttributeValue(null, BIND_ATTR);
if (bind != null) {
DataBinding binding = bindingsByID.get(bind);
if (binding == null) {
throw new XFormParseException("XForm Parse: invalid binding ID '" + bind + "'", e);
}
dataRef = binding.getReference();
refFromBind = true;
} else if (ref != null) {
try {
dataRef = new XPathReference(ref);
} catch (RuntimeException el) {
Std.out.println(XFormParser.getVagueLocation(e));
throw el;
}
} else {
// noinspection StatementWithEmptyBody
if (controlType == Constants.CONTROL_TRIGGER) {
// TODO: special handling for triggers? also, not all triggers created equal
} else {
throw new XFormParseException("XForm Parse: input control with neither 'ref' nor 'bind'", e);
}
}
if (dataRef != null) {
if (!refFromBind) {
dataRef = getAbsRef(dataRef, parent);
}
question.setBind(dataRef);
if (controlType == Constants.CONTROL_SELECT_ONE) {
selectOnes.add((TreeReference) dataRef.getReference());
} else if (controlType == Constants.CONTROL_SELECT_MULTI) {
selectMultis.add((TreeReference) dataRef.getReference());
}
}
boolean isSelect = (controlType == Constants.CONTROL_SELECT_MULTI || controlType == Constants.CONTROL_SELECT_ONE);
question.setControlType(controlType);
question.setAppearanceAttr(e.getAttributeValue(null, APPEARANCE_ATTR));
for (int i = 0; i < e.getChildCount(); i++) {
int type = e.getType(i);
Element child = (type == Node.ELEMENT ? e.getElement(i) : null);
String childName = (child != null ? child.getName() : null);
if (LABEL_ELEMENT.equals(childName)) {
parseQuestionLabel(question, child);
} else if ("hint".equals(childName)) {
parseHint(question, child);
} else if (isSelect && "item".equals(childName)) {
parseItem(question, child);
} else if (isSelect && "itemset".equals(childName)) {
parseItemset(question, child, parent);
}
}
if (isSelect) {
if (question.getNumChoices() > 0 && question.getDynamicChoices() != null) {
throw new XFormParseException("Select question contains both literal choices and <itemset>");
} else if (question.getNumChoices() == 0 && question.getDynamicChoices() == null) {
throw new XFormParseException("Select question has no choices");
}
}
if (question instanceof RangeQuestion) {
populateQuestionWithRangeAttributes((RangeQuestion) question, e);
}
parent.addChild(question);
processAdditionalAttributes(question, e, usedAtts, passedThroughAtts);
return question;
}
use of org.javarosa.core.model.DataBinding 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.core.model.DataBinding in project javarosa by opendatakit.
the class XFormParser method parseBind.
private void parseBind(Element element) {
final DataBinding binding = processStandardBindAttributes(usedAtts, passedThroughAtts, element);
// Warn of unused attributes of parent element
if (XFormUtils.showUnusedAttributeWarning(element, usedAtts)) {
reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(element, usedAtts), getVagueLocation(element));
}
addBinding(binding);
}
use of org.javarosa.core.model.DataBinding 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);
}
Aggregations