Search in sources :

Example 76 with TreeElement

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

the class Recalculate method apply.

public void apply(TreeReference ref, Object result, FormInstance mainInstance) {
    TreeElement element = mainInstance.resolveReference(ref);
    int dataType = element.getDataType();
    element.setAnswer(wrapData(result, dataType));
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 77 with TreeElement

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

the class XFormParserTest method passedThroughAttributesHaveExpectedValues.

/**
 * Attributes that started being used by clients without being added as fields to DataBinding or QuestionDef should
 * be passed through and made available in the bindAttributes or additionalAttributes list.
 */
@Test
public void passedThroughAttributesHaveExpectedValues() throws IOException {
    ParseResult parseResult = parse(r("whitelisted-attributes.xml"));
    TreeElement instanceIDElement = parseResult.formDef.getMainInstance().getRoot().getChildAt(0).getChildAt(0);
    // Bind attributes
    String requiredMsg = instanceIDElement.getBindAttribute("", "requiredMsg").getValue().getDisplayText();
    assertEquals(requiredMsg, "this is required");
    String saveIncomplete = instanceIDElement.getBindAttribute("", "saveIncomplete").getValue().getDisplayText();
    assertEquals(saveIncomplete, "false");
    // Attributes specific to input form controls
    QuestionDef question = FormDef.findQuestionByRef(instanceIDElement.getRef(), parseResult.formDef);
    String rows = question.getAdditionalAttribute("", "rows");
    assertEquals(rows, "2");
    String query = question.getAdditionalAttribute("", "query");
    assertEquals(query, "instance('fake')/root/item[fake2 = /data/meta/instanceID");
}
Also used : ParseResult(org.javarosa.xform.parse.FormParserHelper.ParseResult) QuestionDef(org.javarosa.core.model.QuestionDef) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) Test(org.junit.Test)

Example 78 with TreeElement

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

the class XFormParserTest method serializeAndRestoreMetaNamespaceFormInstance.

@Test
public void serializeAndRestoreMetaNamespaceFormInstance() throws IOException {
    // Given
    ParseResult parseResult = parse(r("meta-namespace-form.xml"));
    assertEquals(parseResult.formDef.getTitle(), "Namespace for Metadata");
    assertNoParseErrors(parseResult);
    FormDef formDef = parseResult.formDef;
    TreeElement audit = findDepthFirst(formDef.getInstance().getRoot(), AUDIT_NODE);
    TreeElement audit2 = findDepthFirst(formDef.getInstance().getRoot(), AUDIT_2_NODE);
    TreeElement audit3 = findDepthFirst(formDef.getInstance().getRoot(), AUDIT_3_NODE);
    assertNotNull(audit);
    assertEquals(ORX_2_NAMESPACE_PREFIX, audit.getNamespacePrefix());
    assertEquals(ORX_2_NAMESPACE_URI, audit.getNamespace());
    assertNotNull(audit2);
    assertEquals(ORX_2_NAMESPACE_PREFIX, audit2.getNamespacePrefix());
    assertEquals(ORX_2_NAMESPACE_URI, audit2.getNamespace());
    assertNotNull(audit3);
    assertEquals(null, audit3.getNamespacePrefix());
    assertEquals(null, audit3.getNamespace());
    audit.setAnswer(new StringData(AUDIT_ANSWER));
    audit2.setAnswer(new StringData(AUDIT_2_ANSWER));
    audit3.setAnswer(new StringData(AUDIT_3_ANSWER));
    // When
    // serialize the form instance
    XFormSerializingVisitor serializer = new XFormSerializingVisitor();
    ByteArrayPayload xml = (ByteArrayPayload) serializer.createSerializedPayload(formDef.getInstance());
    copy(xml.getPayloadStream(), FORM_INSTANCE_XML_FILE_NAME, REPLACE_EXISTING);
    // restore (deserialize) the form instance
    byte[] formInstanceBytes = readAllBytes(FORM_INSTANCE_XML_FILE_NAME);
    FormInstance formInstance = XFormParser.restoreDataModel(formInstanceBytes, null);
    // Then
    audit = findDepthFirst(formInstance.getRoot(), AUDIT_NODE);
    audit2 = findDepthFirst(formInstance.getRoot(), AUDIT_2_NODE);
    audit3 = findDepthFirst(formInstance.getRoot(), AUDIT_3_NODE);
    assertNotNull(audit);
    assertEquals(ORX_2_NAMESPACE_PREFIX, audit.getNamespacePrefix());
    assertEquals(ORX_2_NAMESPACE_URI, audit.getNamespace());
    assertEquals(AUDIT_ANSWER, audit.getValue().getValue());
    assertNotNull(audit2);
    assertEquals(ORX_2_NAMESPACE_PREFIX, audit2.getNamespacePrefix());
    assertEquals(ORX_2_NAMESPACE_URI, audit2.getNamespace());
    assertEquals(AUDIT_2_ANSWER, audit2.getValue().getValue());
    assertNotNull(audit3);
    assertEquals(null, audit3.getNamespacePrefix());
    assertEquals(null, audit3.getNamespace());
    assertEquals(AUDIT_3_ANSWER, audit3.getValue().getValue());
}
Also used : ParseResult(org.javarosa.xform.parse.FormParserHelper.ParseResult) ByteArrayPayload(org.javarosa.core.services.transport.payload.ByteArrayPayload) FormDef(org.javarosa.core.model.FormDef) XFormSerializingVisitor(org.javarosa.model.xform.XFormSerializingVisitor) StringData(org.javarosa.core.model.data.StringData) FormInstance(org.javarosa.core.model.instance.FormInstance) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) Test(org.junit.Test)

Example 79 with TreeElement

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

the class XFormParserTest method parseFormWithSetValueAction.

@Test
public void parseFormWithSetValueAction() throws IOException {
    // Given & When
    ParseResult parseResult = parse(r("form-with-setvalue-action.xml"));
    FormDef formDef = parseResult.formDef;
    // dispatch 'xforms-ready' action (Action.EVENT_XFORMS_READY)
    formDef.initialize(true, new InstanceInitializationFactory());
    // Then
    assertEquals(formDef.getTitle(), "SetValue action");
    assertNoParseErrors(parseResult);
    assertEquals(1, formDef.getEventListeners(Action.EVENT_XFORMS_READY).size());
    TreeElement textNode = formDef.getMainInstance().getRoot().getChildrenWithName("text").get(0);
    assertEquals("Test Value", textNode.getValue().getValue());
}
Also used : InstanceInitializationFactory(org.javarosa.core.model.instance.InstanceInitializationFactory) ParseResult(org.javarosa.xform.parse.FormParserHelper.ParseResult) FormDef(org.javarosa.core.model.FormDef) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) Test(org.junit.Test)

Example 80 with TreeElement

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

the class TreeElementNameComparatorTest method createTreeElement.

/**
 * Creates a TreeElement for testing.
 * <p>
 * JavaRosa creates TreeElements that don’t contain prefixes. Rather, they have the namespace
 * associated with the prefix.
 *
 * @param name the name of the element, without any prefix
 * @param namespace the namespace, e.g., http://openrosa.org/xforms, or null
 * @param prefix the prefix we are pretending has been associated with namespace, or null
 * @return a TreeElement
 */
private TreeElement createTreeElement(String name, final String namespace, final String prefix) {
    TreeElement te = new TreeElement(name);
    if (namespace != null && prefix != null) {
        te.setNamespace(namespace);
        te.setNamespacePrefix(prefix);
    }
    return te;
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement)

Aggregations

TreeElement (org.javarosa.core.model.instance.TreeElement)86 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)33 TreeReference (org.javarosa.core.model.instance.TreeReference)28 FormInstance (org.javarosa.core.model.instance.FormInstance)16 ArrayList (java.util.ArrayList)15 Constraint (org.javarosa.core.model.condition.Constraint)11 Test (org.junit.Test)10 Element (org.kxml2.kdom.Element)9 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)8 FormDef (org.javarosa.core.model.FormDef)7 InstanceInitializationFactory (org.javarosa.core.model.instance.InstanceInitializationFactory)7 IOException (java.io.IOException)6 IFormElement (org.javarosa.core.model.IFormElement)6 StringData (org.javarosa.core.model.data.StringData)6 HashMap (java.util.HashMap)5 IAnswerData (org.javarosa.core.model.data.IAnswerData)4 File (java.io.File)3 GroupDef (org.javarosa.core.model.GroupDef)3 IDataReference (org.javarosa.core.model.IDataReference)3 QuestionDef (org.javarosa.core.model.QuestionDef)3