use of org.javarosa.core.model.instance.utils.IAnswerResolver in project javarosa by opendatakit.
the class TreeElement method populate.
// rebuilding a node from an imported instance
// there's a lot of error checking we could do on the received instance, but it's
// easier to just ignore the parts that are incorrect
public void populate(TreeElement incoming, FormDef f) {
if (this.isLeaf()) {
// check that incoming doesn't have children?
IAnswerData value = incoming.getValue();
if (value == null) {
this.setValue(null);
} else if (this.dataType == Constants.DATATYPE_TEXT || this.dataType == Constants.DATATYPE_NULL) {
// value is a StringData
this.setValue(value);
} else {
String textVal = (String) value.getValue();
// if there is no other IAnswerResolver, use the default one.
IAnswerResolver answerResolver = XFormParser.getAnswerResolver();
if (answerResolver == null) {
answerResolver = new DefaultAnswerResolver();
}
this.setValue(answerResolver.resolveAnswer(textVal, this, f));
}
} else {
List<String> names = new ArrayList<String>(this.getNumChildren());
for (int i = 0; i < this.getNumChildren(); i++) {
TreeElement child = this.getChildAt(i);
if (!names.contains(child.getName())) {
names.add(child.getName());
}
}
// remove all default repetitions from skeleton data model (_preserving_ templates, though)
for (int i = 0; i < this.getNumChildren(); i++) {
TreeElement child = this.getChildAt(i);
if (child.getMaskVar(MASK_REPEATABLE) && child.getMult() != TreeReference.INDEX_TEMPLATE) {
this.removeChildAt(i);
i--;
}
}
// make sure ordering is preserved (needed for compliance with xsd schema)
if (this.getNumChildren() != names.size()) {
throw new RuntimeException("sanity check failed");
}
for (int i = 0; i < this.getNumChildren(); i++) {
TreeElement child = this.getChildAt(i);
String expectedName = names.get(i);
if (!child.getName().equals(expectedName)) {
TreeElement child2 = null;
int j;
for (j = i + 1; j < this.getNumChildren(); j++) {
child2 = this.getChildAt(j);
if (child2.getName().equals(expectedName)) {
break;
}
}
if (j == this.getNumChildren()) {
throw new RuntimeException("sanity check failed");
}
this.removeChildAt(j);
this.children.add(i, child2);
}
}
for (int i = 0; i < this.getNumChildren(); i++) {
TreeElement child = this.getChildAt(i);
List<TreeElement> newChildren = incoming.getChildrenWithName(child.getName());
if (child.getMaskVar(MASK_REPEATABLE)) {
for (int k = 0; k < newChildren.size(); k++) {
TreeElement newChild = child.deepCopy(true);
newChild.setMult(k);
this.children.add(i + k + 1, newChild);
newChild.populate(newChildren.get(k), f);
}
i += newChildren.size();
} else {
if (newChildren.size() == 0) {
child.setRelevant(false);
} else {
child.populate(newChildren.get(0), f);
}
}
}
}
for (int i = 0; i < incoming.getAttributeCount(); i++) {
String name = incoming.getAttributeName(i);
String ns = incoming.getAttributeNamespace(i);
String value = incoming.getAttributeValue(i);
this.setAttribute(ns, name, value);
}
}
Aggregations