Search in sources :

Example 11 with QuestionDef

use of org.javarosa.core.model.QuestionDef in project javarosa by opendatakit.

the class TextFormTests method testSelectChoiceIDsNoLocalizer.

public void testSelectChoiceIDsNoLocalizer() {
    QuestionDef q = fpi.getFirstQuestionDef();
    q.addSelectChoice(new SelectChoice("choice1 id", "val1"));
    q.addSelectChoice(new SelectChoice("loc: choice2", "val2", false));
    if (!fep.getSelectChoices().toString().equals("[{choice1 id} => val1, loc: choice2 => val2]")) {
        fail("Could not add individual select choice ID" + fep.getSelectChoices().toString());
    }
    // clean up
    q.removeSelectChoice(q.getChoices().get(0));
    q.removeSelectChoice(q.getChoices().get(0));
}
Also used : SelectChoice(org.javarosa.core.model.SelectChoice) QuestionDef(org.javarosa.core.model.QuestionDef)

Example 12 with QuestionDef

use of org.javarosa.core.model.QuestionDef in project javarosa by opendatakit.

the class TextFormTests method testPromptIDsNoLocalizer.

public void testPromptIDsNoLocalizer() {
    QuestionDef q = new QuestionDef();
    q.setTextID("long text id");
    if (!"long text id".equals(q.getTextID())) {
        fail("Long text ID getter/setter broken");
    }
    q.setHelpTextID("help text id");
    if (!"help text id".equals(q.getHelpTextID()) || q.getHelpText() != null) {
        fail("Help text ID getter/setter broken");
    }
}
Also used : QuestionDef(org.javarosa.core.model.QuestionDef)

Example 13 with QuestionDef

use of org.javarosa.core.model.QuestionDef in project javarosa by opendatakit.

the class XFormParser method parseUpload.

protected QuestionDef parseUpload(IFormElement parent, Element e, int controlUpload) {
    // get media type value
    String mediaType = e.getAttributeValue(null, "mediatype");
    // parse the control
    QuestionDef question = parseControl(parent, e, controlUpload, Arrays.asList("mediatype"));
    // apply the media type value to the returned question def.
    if ("image/*".equals(mediaType)) {
        // NOTE: this could be further expanded.
        question.setControlType(Constants.CONTROL_IMAGE_CHOOSE);
    } else if ("audio/*".equals(mediaType)) {
        question.setControlType(Constants.CONTROL_AUDIO_CAPTURE);
    } else if ("video/*".equals(mediaType)) {
        question.setControlType(Constants.CONTROL_VIDEO_CAPTURE);
    } else if ("osm/*".equals(mediaType)) {
        question.setControlType(Constants.CONTROL_OSM_CAPTURE);
        List<OSMTag> tags = parseOsmTags(e);
        question.setOsmTags(tags);
    } else {
        // everything else.
        // Presumably, the appearance attribute would govern how this is handled.
        question.setControlType(Constants.CONTROL_FILE_CAPTURE);
    }
    return question;
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) QuestionDef(org.javarosa.core.model.QuestionDef)

Example 14 with QuestionDef

use of org.javarosa.core.model.QuestionDef 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 15 with QuestionDef

use of org.javarosa.core.model.QuestionDef in project javarosa by opendatakit.

the class FormEntryModel method decrementHelper.

private void decrementHelper(List<Integer> indexes, List<Integer> multiplicities, List<IFormElement> elements) {
    int i = indexes.size() - 1;
    if (i != -1) {
        int curIndex = indexes.get(i);
        int curMult = multiplicities.get(i);
        if (repeatStructure == REPEAT_STRUCTURE_NON_LINEAR && elements.get(elements.size() - 1) instanceof GroupDef && ((GroupDef) elements.get(elements.size() - 1)).getRepeat() && multiplicities.get(multiplicities.size() - 1) != TreeReference.INDEX_REPEAT_JUNCTURE) {
            multiplicities.set(i, TreeReference.INDEX_REPEAT_JUNCTURE);
            return;
        } else if (repeatStructure != REPEAT_STRUCTURE_NON_LINEAR && curMult > 0) {
            multiplicities.set(i, curMult - 1);
        } else if (curIndex > 0) {
            // set node to previous element
            indexes.set(i, curIndex - 1);
            multiplicities.set(i, 0);
            elements.set(i, (i == 0 ? form : elements.get(i - 1)).getChild(curIndex - 1));
            if (setRepeatNextMultiplicity(elements, multiplicities))
                return;
        } else {
            // at absolute beginning of current level; index to parent
            indexes.remove(i);
            multiplicities.remove(i);
            elements.remove(i);
            return;
        }
    }
    IFormElement element = (i < 0 ? form : elements.get(i));
    while (!(element instanceof QuestionDef)) {
        if (element.getChildren() == null || element.getChildren().size() == 0) {
            // if there are no children we just return the current index (the group itself)
            return;
        }
        int subIndex = element.getChildren().size() - 1;
        element = element.getChild(subIndex);
        indexes.add(subIndex);
        multiplicities.add(0);
        elements.add(element);
        if (setRepeatNextMultiplicity(elements, multiplicities))
            return;
    }
}
Also used : IFormElement(org.javarosa.core.model.IFormElement) QuestionDef(org.javarosa.core.model.QuestionDef) GroupDef(org.javarosa.core.model.GroupDef)

Aggregations

QuestionDef (org.javarosa.core.model.QuestionDef)30 IFormElement (org.javarosa.core.model.IFormElement)8 SelectChoice (org.javarosa.core.model.SelectChoice)7 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 GroupDef (org.javarosa.core.model.GroupDef)5 IDataReference (org.javarosa.core.model.IDataReference)4 Selection (org.javarosa.core.model.data.helper.Selection)4 TreeElement (org.javarosa.core.model.instance.TreeElement)4 FormEntryPrompt (org.javarosa.form.api.FormEntryPrompt)4 TreeReference (org.javarosa.core.model.instance.TreeReference)3 Localizer (org.javarosa.core.services.locale.Localizer)3 List (java.util.List)2 FormDef (org.javarosa.core.model.FormDef)2 ItemsetBinding (org.javarosa.core.model.ItemsetBinding)2 Constraint (org.javarosa.core.model.condition.Constraint)2 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)2 SelectMultiData (org.javarosa.core.model.data.SelectMultiData)2 SelectOneData (org.javarosa.core.model.data.SelectOneData)2 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)2