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));
}
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");
}
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());
}
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());
}
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;
}
Aggregations