Search in sources :

Example 6 with AbstractTreeElement

use of org.javarosa.core.model.instance.AbstractTreeElement in project javarosa by opendatakit.

the class SetValueAction method processAction.

public void processAction(FormDef model, TreeReference contextRef) {
    // Qualify the reference if necessary
    TreeReference qualifiedReference = contextRef == null ? target : target.contextualize(contextRef);
    // insert events should only trigger on the right nodes
    if (contextRef != null) {
        // strategy
        if (!contextRef.isParentOf(qualifiedReference, false)) {
            return;
        }
    }
    // TODO: either the target or the value's node might not exist here, catch and throw
    // reasonably
    EvaluationContext context = new EvaluationContext(model.getEvaluationContext(), qualifiedReference);
    Object result;
    if (explicitValue != null) {
        result = explicitValue;
    } else {
        result = XPathFuncExpr.unpack(value.eval(model.getMainInstance(), context));
    }
    AbstractTreeElement node = context.resolveReference(qualifiedReference);
    if (node == null) {
        throw new NullPointerException("Target of TreeReference " + qualifiedReference.toString(true) + " could not be resolved!");
    }
    int dataType = node.getDataType();
    IAnswerData val = Recalculate.wrapData(result, dataType);
    model.setValue(val == null ? null : AnswerDataFactory.templateByDataType(dataType).cast(val.uncast()), qualifiedReference, true);
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) TreeReference(org.javarosa.core.model.instance.TreeReference) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext)

Example 7 with AbstractTreeElement

use of org.javarosa.core.model.instance.AbstractTreeElement in project javarosa by opendatakit.

the class April2014DagImpl method addChildrenOfElement.

// Recursive step of utility method
private void addChildrenOfElement(AbstractTreeElement el, ArrayList<TreeReference> toAdd) {
    for (int i = 0; i < el.getNumChildren(); ++i) {
        AbstractTreeElement child = el.getChildAt(i);
        toAdd.add(child.getRef().genericize());
        addChildrenOfElement(child, toAdd);
    }
}
Also used : AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Example 8 with AbstractTreeElement

use of org.javarosa.core.model.instance.AbstractTreeElement in project javarosa by opendatakit.

the class XFormParserTest method parsesExternalSecondaryInstanceForm.

@Test
public void parsesExternalSecondaryInstanceForm() throws IOException, XPathSyntaxException {
    FormDef formDef = parse(EXTERNAL_SECONDARY_INSTANCE_XML).formDef;
    assertEquals("Form with external secondary instance", formDef.getTitle());
    TreeReference treeReference = ((XPathPathExpr) parseXPath("instance('towns')/data_set")).getReference();
    EvaluationContext evaluationContext = formDef.getEvaluationContext();
    List<TreeReference> treeReferences = evaluationContext.expandReference(treeReference);
    assertEquals(1, treeReferences.size());
    DataInstance townInstance = formDef.getNonMainInstance("towns");
    AbstractTreeElement tiRoot = townInstance.getRoot();
    assertEquals("towndata", tiRoot.getName());
    assertEquals(1, tiRoot.getNumChildren());
    AbstractTreeElement dataSetChild = tiRoot.getChild("data_set", 0);
    assertEquals("us_east", dataSetChild.getValue().getDisplayText());
}
Also used : AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) DataInstance(org.javarosa.core.model.instance.DataInstance) FormDef(org.javarosa.core.model.FormDef) TreeReference(org.javarosa.core.model.instance.TreeReference) XPathPathExpr(org.javarosa.xpath.expr.XPathPathExpr) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) Test(org.junit.Test)

Example 9 with AbstractTreeElement

use of org.javarosa.core.model.instance.AbstractTreeElement in project javarosa by opendatakit.

the class QuestionDataGroupTests method testAcceptsVisitor.

public void testAcceptsVisitor() {
    final MutableBoolean visitorAccepted = new MutableBoolean(false);
    final MutableBoolean dispatchedWrong = new MutableBoolean(false);
    ITreeVisitor sampleVisitor = new ITreeVisitor() {

        public void visit(FormInstance tree) {
            dispatchedWrong.setValue(true);
        }

        public void visit(AbstractTreeElement element) {
            visitorAccepted.setValue(true);
        }
    };
    stringElement.accept(sampleVisitor);
    assertTrue("The visitor's visit method was not called correctly by the QuestionDataElement", visitorAccepted.getValue());
    assertTrue("The visitor was dispatched incorrectly by the QuestionDataElement", !dispatchedWrong.getValue());
}
Also used : AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) FormInstance(org.javarosa.core.model.instance.FormInstance) ITreeVisitor(org.javarosa.core.model.instance.utils.ITreeVisitor)

Example 10 with AbstractTreeElement

use of org.javarosa.core.model.instance.AbstractTreeElement in project javarosa by opendatakit.

the class QuestionDataElementTests method testAcceptsVisitor.

public void testAcceptsVisitor() {
    final MutableBoolean visitorAccepted = new MutableBoolean(false);
    final MutableBoolean dispatchedWrong = new MutableBoolean(false);
    ITreeVisitor sampleVisitor = new ITreeVisitor() {

        public void visit(FormInstance tree) {
            dispatchedWrong.bool = true;
        }

        public void visit(AbstractTreeElement element) {
            visitorAccepted.bool = true;
        }
    };
    stringElement.accept(sampleVisitor);
    assertTrue("The visitor's visit method was not called correctly by the QuestionDataElement", visitorAccepted.getValue());
    assertTrue("The visitor was dispatched incorrectly by the QuestionDataElement", !dispatchedWrong.getValue());
}
Also used : AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) FormInstance(org.javarosa.core.model.instance.FormInstance) ITreeVisitor(org.javarosa.core.model.instance.utils.ITreeVisitor)

Aggregations

AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)10 TreeElement (org.javarosa.core.model.instance.TreeElement)4 FormInstance (org.javarosa.core.model.instance.FormInstance)3 TreeReference (org.javarosa.core.model.instance.TreeReference)3 FormDef (org.javarosa.core.model.FormDef)2 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)2 DataInstance (org.javarosa.core.model.instance.DataInstance)2 ITreeVisitor (org.javarosa.core.model.instance.utils.ITreeVisitor)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 IFormElement (org.javarosa.core.model.IFormElement)1 IAnswerData (org.javarosa.core.model.data.IAnswerData)1 ExternalDataInstance (org.javarosa.core.model.instance.ExternalDataInstance)1 ExternalDataInstance.getPathIfExternalDataInstance (org.javarosa.core.model.instance.ExternalDataInstance.getPathIfExternalDataInstance)1 CodeTimer (org.javarosa.core.util.CodeTimer)1 InvalidStructureException (org.javarosa.xml.util.InvalidStructureException)1 UnfullfilledRequirementsException (org.javarosa.xml.util.UnfullfilledRequirementsException)1 XPathExpression (org.javarosa.xpath.expr.XPathExpression)1 XPathPathExpr (org.javarosa.xpath.expr.XPathPathExpr)1 Test (org.junit.Test)1