Search in sources :

Example 1 with ParseResult

use of org.javarosa.xform.parse.FormParserHelper.ParseResult in project javarosa by opendatakit.

the class XFormParserTest method parseIMCIbyDTreeForm.

@Test
public void parseIMCIbyDTreeForm() throws IOException {
    // Given & When
    ParseResult parseResult = parse(r("eIMCI-by-D-Tree.xml"));
    // Then
    assertEquals(parseResult.formDef.getTitle(), "eIMCI by D-Tree");
    assertNoParseErrors(parseResult);
}
Also used : ParseResult(org.javarosa.xform.parse.FormParserHelper.ParseResult) Test(org.junit.Test)

Example 2 with ParseResult

use of org.javarosa.xform.parse.FormParserHelper.ParseResult 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 3 with ParseResult

use of org.javarosa.xform.parse.FormParserHelper.ParseResult in project javarosa by opendatakit.

the class XFormParserTest method parsesMetaNamespaceForm.

@Test
public void parsesMetaNamespaceForm() throws IOException {
    ParseResult parseResult = parse(r("meta-namespace-form.xml"));
    assertEquals(parseResult.formDef.getTitle(), "Namespace for Metadata");
    assertNoParseErrors(parseResult);
}
Also used : ParseResult(org.javarosa.xform.parse.FormParserHelper.ParseResult) Test(org.junit.Test)

Example 4 with ParseResult

use of org.javarosa.xform.parse.FormParserHelper.ParseResult 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 5 with ParseResult

use of org.javarosa.xform.parse.FormParserHelper.ParseResult 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)

Aggregations

ParseResult (org.javarosa.xform.parse.FormParserHelper.ParseResult)9 Test (org.junit.Test)9 FormDef (org.javarosa.core.model.FormDef)3 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)3 TreeElement (org.javarosa.core.model.instance.TreeElement)3 GroupDef (org.javarosa.core.model.GroupDef)1 IDataReference (org.javarosa.core.model.IDataReference)1 IFormElement (org.javarosa.core.model.IFormElement)1 QuestionDef (org.javarosa.core.model.QuestionDef)1 SubmissionProfile (org.javarosa.core.model.SubmissionProfile)1 StringData (org.javarosa.core.model.data.StringData)1 FormInstance (org.javarosa.core.model.instance.FormInstance)1 InstanceInitializationFactory (org.javarosa.core.model.instance.InstanceInitializationFactory)1 TreeReference (org.javarosa.core.model.instance.TreeReference)1 ByteArrayPayload (org.javarosa.core.services.transport.payload.ByteArrayPayload)1 XFormSerializingVisitor (org.javarosa.model.xform.XFormSerializingVisitor)1 XPathReference (org.javarosa.model.xform.XPathReference)1