Search in sources :

Example 1 with XFormSerializingVisitor

use of org.javarosa.model.xform.XFormSerializingVisitor in project collect by opendatakit.

the class FormController method getSubmissionXml.

/**
 * Extract the portion of the form that should be uploaded to the server.
 */
public ByteArrayPayload getSubmissionXml() throws IOException {
    FormInstance instance = getInstance();
    XFormSerializingVisitor serializer = new XFormSerializingVisitor();
    return (ByteArrayPayload) serializer.createSerializedPayload(instance, getSubmissionDataReference());
}
Also used : ByteArrayPayload(org.javarosa.core.services.transport.payload.ByteArrayPayload) XFormSerializingVisitor(org.javarosa.model.xform.XFormSerializingVisitor) FormInstance(org.javarosa.core.model.instance.FormInstance)

Example 2 with XFormSerializingVisitor

use of org.javarosa.model.xform.XFormSerializingVisitor in project collect by opendatakit.

the class FormController method getFilledInFormXml.

/**
 * Constructs the XML payload for a filled-in form instance. This payload
 * enables a filled-in form to be re-opened and edited.
 */
public ByteArrayPayload getFilledInFormXml() throws IOException {
    // assume no binary data inside the model.
    FormInstance datamodel = getInstance();
    XFormSerializingVisitor serializer = new XFormSerializingVisitor();
    return (ByteArrayPayload) serializer.createSerializedPayload(datamodel);
}
Also used : ByteArrayPayload(org.javarosa.core.services.transport.payload.ByteArrayPayload) XFormSerializingVisitor(org.javarosa.model.xform.XFormSerializingVisitor) FormInstance(org.javarosa.core.model.instance.FormInstance)

Example 3 with XFormSerializingVisitor

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

Aggregations

FormInstance (org.javarosa.core.model.instance.FormInstance)3 ByteArrayPayload (org.javarosa.core.services.transport.payload.ByteArrayPayload)3 XFormSerializingVisitor (org.javarosa.model.xform.XFormSerializingVisitor)3 FormDef (org.javarosa.core.model.FormDef)1 StringData (org.javarosa.core.model.data.StringData)1 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)1 TreeElement (org.javarosa.core.model.instance.TreeElement)1 ParseResult (org.javarosa.xform.parse.FormParserHelper.ParseResult)1 Test (org.junit.Test)1