Search in sources :

Example 6 with XPathReference

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

Example 7 with XPathReference

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

the class XFormParser method parseGroup.

private void parseGroup(IFormElement parent, Element e, int groupType) {
    GroupDef group = new GroupDef();
    // until we come up with a better scheme
    group.setID(serialQuestionID++);
    IDataReference dataRef = null;
    boolean refFromBind = false;
    List<String> usedAtts = new ArrayList<>();
    usedAtts.add(REF_ATTR);
    usedAtts.add(NODESET_ATTR);
    usedAtts.add(BIND_ATTR);
    usedAtts.add(APPEARANCE_ATTR);
    usedAtts.add("count");
    usedAtts.add("noAddRemove");
    if (groupType == CONTAINER_REPEAT) {
        group.setRepeat(true);
    }
    String ref = e.getAttributeValue(null, REF_ATTR);
    String nodeset = e.getAttributeValue(null, NODESET_ATTR);
    String bind = e.getAttributeValue(null, BIND_ATTR);
    group.setAppearanceAttr(e.getAttributeValue(null, APPEARANCE_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 (group.getRepeat()) {
            if (nodeset != null) {
                dataRef = new XPathReference(nodeset);
            } else {
                throw new XFormParseException("XForm Parse: <repeat> with no binding ('bind' or 'nodeset')", e);
            }
        } else {
            if (ref != null) {
                dataRef = new XPathReference(ref);
            } else if (nodeset != null) {
                dataRef = new XPathReference(nodeset);
            }
        // <group> not required to have a binding so no exception thrown
        }
    }
    if (!refFromBind) {
        dataRef = getAbsRef(dataRef, parent);
    }
    group.setBind(dataRef);
    if (group.getRepeat()) {
        repeats.add((TreeReference) dataRef.getReference());
        String countRef = e.getAttributeValue(NAMESPACE_JAVAROSA, "count");
        if (countRef != null) {
            group.count = getAbsRef(new XPathReference(countRef), parent);
            group.noAddRemove = true;
        } else {
            group.noAddRemove = (e.getAttributeValue(NAMESPACE_JAVAROSA, "noAddRemove") != null);
        }
    }
    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);
        String childNamespace = (child != null ? child.getNamespace() : null);
        if (group.getRepeat() && NAMESPACE_JAVAROSA.equals(childNamespace)) {
            if ("chooseCaption".equals(childName)) {
                group.chooseCaption = getLabel(child);
            } else if ("addCaption".equals(childName)) {
                group.addCaption = getLabel(child);
            } else if ("delCaption".equals(childName)) {
                group.delCaption = getLabel(child);
            } else if ("doneCaption".equals(childName)) {
                group.doneCaption = getLabel(child);
            } else if ("addEmptyCaption".equals(childName)) {
                group.addEmptyCaption = getLabel(child);
            } else if ("doneEmptyCaption".equals(childName)) {
                group.doneEmptyCaption = getLabel(child);
            } else if ("entryHeader".equals(childName)) {
                group.entryHeader = getLabel(child);
            } else if ("delHeader".equals(childName)) {
                group.delHeader = getLabel(child);
            } else if ("mainHeader".equals(childName)) {
                group.mainHeader = getLabel(child);
            }
        }
    }
    for (int i = 0; i < e.getChildCount(); i++) {
        if (e.getType(i) == Element.ELEMENT) {
            parseElement(e.getElement(i), group, groupLevelHandlers);
        }
    }
    // save all the unused attributes verbatim...
    for (int i = 0; i < e.getAttributeCount(); i++) {
        String name = e.getAttributeName(i);
        if (usedAtts.contains(name))
            continue;
        group.setAdditionalAttribute(e.getAttributeNamespace(i), name, e.getAttributeValue(i));
    }
    // print unused attribute warning message for parent element
    if (XFormUtils.showUnusedAttributeWarning(e, usedAtts)) {
        reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(e, usedAtts), getVagueLocation(e));
    }
    parent.addChild(group);
}
Also used : IDataReference(org.javarosa.core.model.IDataReference) DataBinding(org.javarosa.core.model.DataBinding) 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) GroupDef(org.javarosa.core.model.GroupDef)

Example 8 with XPathReference

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

the class IDag method getConditionExpressionForTrueAction.

/**
 * Pull this in from FormOverview so that we can make fields private.
 *
 * @param instanceNode
 * @param action
 * @return
 */
public final IConditionExpr getConditionExpressionForTrueAction(FormInstance mainInstance, TreeElement instanceNode, int action) {
    IConditionExpr expr = null;
    for (int i = 0; i < triggerablesDAG.size() && expr == null; i++) {
        // Clayton Sims - Jun 1, 2009 : Not sure how legitimate this
        // cast is. It might work now, but break later.
        // Clayton Sims - Jun 24, 2009 : Yeah, that change broke things.
        // For now, we won't bother to print out anything that isn't
        // a condition.
        QuickTriggerable qt = triggerablesDAG.get(i);
        if (qt.t instanceof Condition) {
            Condition c = (Condition) qt.t;
            if (c.trueAction == action) {
                List<TreeReference> targets = c.getTargets();
                for (int j = 0; j < targets.size() && expr == null; j++) {
                    TreeReference target = targets.get(j);
                    TreeReference tr = (TreeReference) (new XPathReference(target)).getReference();
                    TreeElement element = mainInstance.getTemplatePath(tr);
                    if (instanceNode == element) {
                        expr = c.getExpr();
                    }
                }
            }
        }
    }
    return expr;
}
Also used : Condition(org.javarosa.core.model.condition.Condition) IConditionExpr(org.javarosa.core.model.condition.IConditionExpr) TreeReference(org.javarosa.core.model.instance.TreeReference) XPathReference(org.javarosa.model.xform.XPathReference) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 9 with XPathReference

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

the class XFormParserTest method parseGroupWithNodesetAttrForm.

@Test
public void parseGroupWithNodesetAttrForm() throws IOException {
    // Given & When
    ParseResult parseResult = parse(r("group-with-nodeset-attr.xml"));
    // Then
    assertEquals(parseResult.formDef.getTitle(), "group with nodeset attribute");
    assertEquals("Number of error messages", 0, parseResult.errorMessages.size());
    final TreeReference expectedTreeReference = new TreeReference();
    // absolute reference
    expectedTreeReference.setRefLevel(-1);
    // the instance root
    expectedTreeReference.add("data", -1);
    // the outer repeat
    expectedTreeReference.add("R1", -1);
    // the inner group
    expectedTreeReference.add("G2", -1);
    final IDataReference expectedXPathReference = new XPathReference(expectedTreeReference);
    IFormElement groupElement = parseResult.formDef.getChild(0).getChild(0);
    assertThat(groupElement, instanceOf(GroupDef.class));
    assertThat(((GroupDef) groupElement).getRepeat(), is(false));
    assertThat(groupElement.getBind(), is(expectedXPathReference));
}
Also used : IFormElement(org.javarosa.core.model.IFormElement) ParseResult(org.javarosa.xform.parse.FormParserHelper.ParseResult) TreeReference(org.javarosa.core.model.instance.TreeReference) IDataReference(org.javarosa.core.model.IDataReference) XPathReference(org.javarosa.model.xform.XPathReference) GroupDef(org.javarosa.core.model.GroupDef) Test(org.junit.Test)

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