Search in sources :

Example 1 with DataBinding

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);
    }
}
Also used : IDataReference(org.javarosa.core.model.IDataReference) DataBinding(org.javarosa.core.model.DataBinding) SubmissionProfile(org.javarosa.core.model.SubmissionProfile) XPathReference(org.javarosa.model.xform.XPathReference)

Example 2 with DataBinding

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;
}
Also used : IDataReference(org.javarosa.core.model.IDataReference) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) Element(org.kxml2.kdom.Element) IFormElement(org.javarosa.core.model.IFormElement) ArrayList(java.util.ArrayList) XPathReference(org.javarosa.model.xform.XPathReference) TreeReference(org.javarosa.core.model.instance.TreeReference) RangeQuestion(org.javarosa.core.model.RangeQuestion) DataBinding(org.javarosa.core.model.DataBinding) QuestionDef(org.javarosa.core.model.QuestionDef)

Example 3 with DataBinding

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;
}
Also used : Condition(org.javarosa.core.model.condition.Condition) XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException) Recalculate(org.javarosa.core.model.condition.Recalculate) XPathException(org.javarosa.xpath.XPathException) IDataReference(org.javarosa.core.model.IDataReference) DataBinding(org.javarosa.core.model.DataBinding) XPathConditional(org.javarosa.xpath.XPathConditional) XPathReference(org.javarosa.model.xform.XPathReference)

Example 4 with DataBinding

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);
}
Also used : DataBinding(org.javarosa.core.model.DataBinding)

Example 5 with DataBinding

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);
}
Also used : Action(org.javarosa.core.model.Action) SetValueAction(org.javarosa.core.model.actions.SetValueAction) XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException) IDataReference(org.javarosa.core.model.IDataReference) TreeReference(org.javarosa.core.model.instance.TreeReference) DataBinding(org.javarosa.core.model.DataBinding) SetValueAction(org.javarosa.core.model.actions.SetValueAction) XPathReference(org.javarosa.model.xform.XPathReference)

Aggregations

DataBinding (org.javarosa.core.model.DataBinding)8 IDataReference (org.javarosa.core.model.IDataReference)5 XPathReference (org.javarosa.model.xform.XPathReference)5 TreeReference (org.javarosa.core.model.instance.TreeReference)4 ArrayList (java.util.ArrayList)3 IFormElement (org.javarosa.core.model.IFormElement)2 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)2 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)2 TreeElement (org.javarosa.core.model.instance.TreeElement)2 XPathSyntaxException (org.javarosa.xpath.parser.XPathSyntaxException)2 Element (org.kxml2.kdom.Element)2 Action (org.javarosa.core.model.Action)1 GroupDef (org.javarosa.core.model.GroupDef)1 QuestionDef (org.javarosa.core.model.QuestionDef)1 RangeQuestion (org.javarosa.core.model.RangeQuestion)1 SubmissionProfile (org.javarosa.core.model.SubmissionProfile)1 SetValueAction (org.javarosa.core.model.actions.SetValueAction)1 Condition (org.javarosa.core.model.condition.Condition)1 Constraint (org.javarosa.core.model.condition.Constraint)1 Recalculate (org.javarosa.core.model.condition.Recalculate)1