use of org.javarosa.core.model.IDataReference in project javarosa by opendatakit.
the class XFormParser method parseGroup.
private void parseGroup(IFormElement parent, Element e, int groupType) {
GroupDef group = new GroupDef();
// until we come up with a better scheme
group.setID(serialQuestionID++);
IDataReference dataRef = null;
boolean refFromBind = false;
List<String> usedAtts = new ArrayList<>();
usedAtts.add(REF_ATTR);
usedAtts.add(NODESET_ATTR);
usedAtts.add(BIND_ATTR);
usedAtts.add(APPEARANCE_ATTR);
usedAtts.add("count");
usedAtts.add("noAddRemove");
if (groupType == CONTAINER_REPEAT) {
group.setRepeat(true);
}
String ref = e.getAttributeValue(null, REF_ATTR);
String nodeset = e.getAttributeValue(null, NODESET_ATTR);
String bind = e.getAttributeValue(null, BIND_ATTR);
group.setAppearanceAttr(e.getAttributeValue(null, APPEARANCE_ATTR));
if (bind != null) {
DataBinding binding = bindingsByID.get(bind);
if (binding == null) {
throw new XFormParseException("XForm Parse: invalid binding ID [" + bind + "]", e);
}
dataRef = binding.getReference();
refFromBind = true;
} else {
if (group.getRepeat()) {
if (nodeset != null) {
dataRef = new XPathReference(nodeset);
} else {
throw new XFormParseException("XForm Parse: <repeat> with no binding ('bind' or 'nodeset')", e);
}
} else {
if (ref != null) {
dataRef = new XPathReference(ref);
} else if (nodeset != null) {
dataRef = new XPathReference(nodeset);
}
// <group> not required to have a binding so no exception thrown
}
}
if (!refFromBind) {
dataRef = getAbsRef(dataRef, parent);
}
group.setBind(dataRef);
if (group.getRepeat()) {
repeats.add((TreeReference) dataRef.getReference());
String countRef = e.getAttributeValue(NAMESPACE_JAVAROSA, "count");
if (countRef != null) {
group.count = getAbsRef(new XPathReference(countRef), parent);
group.noAddRemove = true;
} else {
group.noAddRemove = (e.getAttributeValue(NAMESPACE_JAVAROSA, "noAddRemove") != null);
}
}
for (int i = 0; i < e.getChildCount(); i++) {
int type = e.getType(i);
Element child = (type == Node.ELEMENT ? e.getElement(i) : null);
String childName = (child != null ? child.getName() : null);
String childNamespace = (child != null ? child.getNamespace() : null);
if (group.getRepeat() && NAMESPACE_JAVAROSA.equals(childNamespace)) {
if ("chooseCaption".equals(childName)) {
group.chooseCaption = getLabel(child);
} else if ("addCaption".equals(childName)) {
group.addCaption = getLabel(child);
} else if ("delCaption".equals(childName)) {
group.delCaption = getLabel(child);
} else if ("doneCaption".equals(childName)) {
group.doneCaption = getLabel(child);
} else if ("addEmptyCaption".equals(childName)) {
group.addEmptyCaption = getLabel(child);
} else if ("doneEmptyCaption".equals(childName)) {
group.doneEmptyCaption = getLabel(child);
} else if ("entryHeader".equals(childName)) {
group.entryHeader = getLabel(child);
} else if ("delHeader".equals(childName)) {
group.delHeader = getLabel(child);
} else if ("mainHeader".equals(childName)) {
group.mainHeader = getLabel(child);
}
}
}
for (int i = 0; i < e.getChildCount(); i++) {
if (e.getType(i) == Element.ELEMENT) {
parseElement(e.getElement(i), group, groupLevelHandlers);
}
}
// save all the unused attributes verbatim...
for (int i = 0; i < e.getAttributeCount(); i++) {
String name = e.getAttributeName(i);
if (usedAtts.contains(name))
continue;
group.setAdditionalAttribute(e.getAttributeNamespace(i), name, e.getAttributeValue(i));
}
// print unused attribute warning message for parent element
if (XFormUtils.showUnusedAttributeWarning(e, usedAtts)) {
reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(e, usedAtts), getVagueLocation(e));
}
parent.addChild(group);
}
use of org.javarosa.core.model.IDataReference in project javarosa by opendatakit.
the class FormInstanceParser method verifyControlBindings.
private void verifyControlBindings(IFormElement fe, FormInstance instance, List<String> errors) {
// throws XmlPullParserException {
if (fe.getChildren() == null)
return;
for (int i = 0; i < fe.getChildren().size(); i++) {
IFormElement child = fe.getChildren().get(i);
IDataReference ref = null;
String type = null;
if (child instanceof GroupDef) {
ref = child.getBind();
type = (((GroupDef) child).getRepeat() ? "Repeat" : "Group");
} else if (child instanceof QuestionDef) {
ref = child.getBind();
type = "Question";
}
TreeReference tref = FormInstance.unpackReference(ref);
if (child instanceof QuestionDef && tref.size() == 0) {
// group can bind to '/'; repeat can't, but that's checked above
reporter.warning(XFormParserReporter.TYPE_INVALID_STRUCTURE, "Cannot bind control to '/'", null);
} else {
List<TreeReference> nodes = new EvaluationContext(instance).expandReference(tref, true);
if (nodes.size() == 0) {
String error = type + " bound to non-existent node: [" + tref.toString() + "]";
reporter.error(error);
errors.add(error);
}
// we can't check whether questions map to the right kind of node ('data' node vs. 'sub-tree' node) as that depends
// on the question's data type, which we don't know yet
}
verifyControlBindings(child, instance, errors);
}
}
use of org.javarosa.core.model.IDataReference in project javarosa by opendatakit.
the class XFormParserTest method parseGroupWithNodesetAttrForm.
@Test
public void parseGroupWithNodesetAttrForm() throws IOException {
// Given & When
ParseResult parseResult = parse(r("group-with-nodeset-attr.xml"));
// Then
assertEquals(parseResult.formDef.getTitle(), "group with nodeset attribute");
assertEquals("Number of error messages", 0, parseResult.errorMessages.size());
final TreeReference expectedTreeReference = new TreeReference();
// absolute reference
expectedTreeReference.setRefLevel(-1);
// the instance root
expectedTreeReference.add("data", -1);
// the outer repeat
expectedTreeReference.add("R1", -1);
// the inner group
expectedTreeReference.add("G2", -1);
final IDataReference expectedXPathReference = new XPathReference(expectedTreeReference);
IFormElement groupElement = parseResult.formDef.getChild(0).getChild(0);
assertThat(groupElement, instanceOf(GroupDef.class));
assertThat(((GroupDef) groupElement).getRepeat(), is(false));
assertThat(groupElement.getBind(), is(expectedXPathReference));
}
use of org.javarosa.core.model.IDataReference in project javarosa by opendatakit.
the class FormDefTest method newRef.
public IDataReference newRef(String xpath) {
IDataReference ref = new DummyReference();
ref.setReference(xpath);
pf.addClass(DummyReference.class);
return ref;
}
use of org.javarosa.core.model.IDataReference in project javarosa by opendatakit.
the class QuestionDefTest method testAccessorsModifiers.
@Test
public void testAccessorsModifiers() {
QuestionDef q = new QuestionDef();
q.setID(45);
if (q.getID() != 45) {
fail("ID getter/setter broken");
}
testSerialize(q, "c");
IDataReference ref = newRef();
q.setBind(ref);
if (q.getBind() != ref) {
fail("Ref getter/setter broken");
}
testSerialize(q, "e");
q.setControlType(Constants.CONTROL_SELECT_ONE);
if (q.getControlType() != Constants.CONTROL_SELECT_ONE) {
fail("Control type getter/setter broken");
}
testSerialize(q, "g");
q.setAppearanceAttr("minimal");
if (!"minimal".equals(q.getAppearanceAttr())) {
fail("Appearance getter/setter broken");
}
testSerialize(q, "h");
}
Aggregations