use of org.javarosa.model.xform.XPathReference in project javarosa by opendatakit.
the class XFormParser method parseSetValueAction.
private void parseSetValueAction(FormDef form, Element e) {
String ref = e.getAttributeValue(null, REF_ATTR);
String bind = e.getAttributeValue(null, BIND_ATTR);
String event = e.getAttributeValue(null, "event");
IDataReference dataRef = null;
boolean refFromBind = false;
// TODO: There is a _lot_ of duplication of this code, fix that!
if (bind != null) {
DataBinding binding = bindingsByID.get(bind);
if (binding == null) {
throw new XFormParseException("XForm Parse: invalid binding ID in submit'" + bind + "'", e);
}
dataRef = binding.getReference();
refFromBind = true;
} else if (ref != null) {
dataRef = new XPathReference(ref);
} else {
throw new XFormParseException("setvalue action with no target!", e);
}
if (dataRef != null) {
if (!refFromBind) {
dataRef = FormDef.getAbsRef(dataRef, TreeReference.rootRef());
}
}
String valueRef = e.getAttributeValue(null, "value");
Action action;
TreeReference treeref = FormInstance.unpackReference(dataRef);
actionTargets.add(treeref);
if (valueRef == null) {
if (e.getChildCount() == 0 || !e.isText(0)) {
throw new XFormParseException("No 'value' attribute and no inner value set in <setvalue> associated with: " + treeref, e);
}
// Set expression
action = new SetValueAction(treeref, e.getText(0));
} else {
try {
action = new SetValueAction(treeref, XPathParseTool.parseXPath(valueRef));
} catch (XPathSyntaxException e1) {
Std.printStack(e1);
throw new XFormParseException("Invalid XPath in value set action declaration: '" + valueRef + "'", e);
}
}
form.registerEventListener(event, action);
}
use of org.javarosa.model.xform.XPathReference 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.model.xform.XPathReference in project javarosa by opendatakit.
the class IDag method getConditionExpressionForTrueAction.
/**
* Pull this in from FormOverview so that we can make fields private.
*
* @param instanceNode
* @param action
* @return
*/
public final IConditionExpr getConditionExpressionForTrueAction(FormInstance mainInstance, TreeElement instanceNode, int action) {
IConditionExpr expr = null;
for (int i = 0; i < triggerablesDAG.size() && expr == null; i++) {
// Clayton Sims - Jun 1, 2009 : Not sure how legitimate this
// cast is. It might work now, but break later.
// Clayton Sims - Jun 24, 2009 : Yeah, that change broke things.
// For now, we won't bother to print out anything that isn't
// a condition.
QuickTriggerable qt = triggerablesDAG.get(i);
if (qt.t instanceof Condition) {
Condition c = (Condition) qt.t;
if (c.trueAction == action) {
List<TreeReference> targets = c.getTargets();
for (int j = 0; j < targets.size() && expr == null; j++) {
TreeReference target = targets.get(j);
TreeReference tr = (TreeReference) (new XPathReference(target)).getReference();
TreeElement element = mainInstance.getTemplatePath(tr);
if (instanceNode == element) {
expr = c.getExpr();
}
}
}
}
}
return expr;
}
use of org.javarosa.model.xform.XPathReference 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));
}
Aggregations