Search in sources :

Example 1 with XPathReference

use of org.javarosa.model.xform.XPathReference in project collect by opendatakit.

the class FormController method getSubmissionDataReference.

/**
 * Find the portion of the form that is to be submitted
 */
private IDataReference getSubmissionDataReference() {
    FormDef formDef = formEntryController.getModel().getForm();
    // Determine the information about the submission...
    SubmissionProfile p = formDef.getSubmissionProfile();
    if (p == null || p.getRef() == null) {
        return new XPathReference("/");
    } else {
        return p.getRef();
    }
}
Also used : FormDef(org.javarosa.core.model.FormDef) SubmissionProfile(org.javarosa.core.model.SubmissionProfile) XPathReference(org.javarosa.model.xform.XPathReference)

Example 2 with XPathReference

use of org.javarosa.model.xform.XPathReference 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 3 with XPathReference

use of org.javarosa.model.xform.XPathReference 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 4 with XPathReference

use of org.javarosa.model.xform.XPathReference in project javarosa by opendatakit.

the class FormDef method getAbsRef.

// take a (possibly relative) reference, and make it absolute based on its parent
// moved from the parser to this class so it can be used more cleanly by ItemsetBinding
public static IDataReference getAbsRef(IDataReference ref, TreeReference parentRef) {
    TreeReference tref;
    if (!parentRef.isAbsolute()) {
        throw new RuntimeException("XFormParser.getAbsRef: parentRef must be absolute");
    }
    if (ref != null) {
        tref = (TreeReference) ref.getReference();
    } else {
        // only happens for <group>s with no binding
        tref = TreeReference.selfRef();
    }
    tref = tref.parent(parentRef);
    if (tref == null) {
        throw new XFormParseException("Binding path [" + tref + "] not allowed with parent binding of [" + parentRef + "]");
    }
    return new XPathReference(tref);
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) XFormParseException(org.javarosa.xform.parse.XFormParseException) XPathReference(org.javarosa.model.xform.XPathReference)

Example 5 with XPathReference

use of org.javarosa.model.xform.XPathReference 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)

Aggregations

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