use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.
the class FormEntryModel method isIndexReadonly.
/**
* @param index
* @return true if the element at the specified index is read only
*/
public boolean isIndexReadonly(FormIndex index) {
if (index.isBeginningOfFormIndex() || index.isEndOfFormIndex())
return true;
TreeReference ref = form.getChildInstanceRef(index);
boolean isAskNewRepeat = (getEvent(index) == FormEntryController.EVENT_PROMPT_NEW_REPEAT || getEvent(index) == FormEntryController.EVENT_REPEAT_JUNCTURE);
if (isAskNewRepeat) {
return false;
} else {
TreeElement node = form.getMainInstance().resolveReference(ref);
return !node.isEnabled();
}
}
use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.
the class XFormSerializingVisitor method serializeNode.
public Element serializeNode(TreeElement instanceNode) {
// don't set anything on this element yet, as it might get overwritten
Element e = new Element();
// don't serialize template nodes or non-relevant nodes
if ((respectRelevance && !instanceNode.isRelevant()) || instanceNode.getMult() == TreeReference.INDEX_TEMPLATE) {
return null;
}
if (instanceNode.getValue() != null) {
Object serializedAnswer;
try {
serializedAnswer = serializer.serializeAnswerData(instanceNode.getValue(), instanceNode.getDataType());
} catch (RuntimeException ex) {
throw new RuntimeException("Unable to serialize " + instanceNode.getValue().toString() + ". Exception: " + ex.toString());
}
if (serializedAnswer instanceof Element) {
e = (Element) serializedAnswer;
} else if (serializedAnswer instanceof String) {
e = new Element();
e.addChild(Node.TEXT, (String) serializedAnswer);
} else {
throw new RuntimeException("Can't handle serialized output for" + instanceNode.getValue().toString() + ", " + serializedAnswer);
}
if (serializer.containsExternalData(instanceNode.getValue()).booleanValue()) {
IDataPointer[] pointer = serializer.retrieveExternalDataPointer(instanceNode.getValue());
for (int i = 0; i < pointer.length; ++i) {
dataPointers.add(pointer[i]);
}
}
} else {
// make sure all children of the same tag name are written en bloc
List<String> childNames = new ArrayList<String>(instanceNode.getNumChildren());
for (int i = 0; i < instanceNode.getNumChildren(); i++) {
String childName = instanceNode.getChildAt(i).getName();
if (!childNames.contains(childName))
childNames.add(childName);
}
for (int i = 0; i < childNames.size(); i++) {
String childName = (String) childNames.get(i);
int mult = instanceNode.getChildMultiplicity(childName);
for (int j = 0; j < mult; j++) {
Element child = serializeNode(instanceNode.getChild(childName, j));
if (child != null) {
e.addChild(Node.ELEMENT, child);
}
}
}
}
e.setName(instanceNode.getName());
// add hard-coded attributes
for (int i = 0; i < instanceNode.getAttributeCount(); i++) {
String namespace = instanceNode.getAttributeNamespace(i);
String name = instanceNode.getAttributeName(i);
String val = instanceNode.getAttributeValue(i);
// is it legal for getAttributeValue() to return null? playing it safe for now and assuming yes
if (val == null) {
val = "";
}
e.setAttribute(namespace, name, val);
}
if (instanceNode.getNamespace() != null) {
e.setNamespace(instanceNode.getNamespace());
}
return e;
}
use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.
the class XFormSerializingVisitor method visit.
/*
* (non-Javadoc)
* @see org.javarosa.core.model.utils.ITreeVisitor#visit(org.javarosa.core.model.DataModelTree)
*/
public void visit(FormInstance tree) {
theXmlDoc = new Document();
// TreeElement root = tree.getRoot();
TreeElement root = tree.resolveReference(rootRef);
// catch that case and just start at the root.
if (root == null) {
root = tree.getRoot();
}
if (root != null) {
theXmlDoc.addChild(Node.ELEMENT, serializeNode(root));
}
Element top = theXmlDoc.getElement(0);
String[] prefixes = tree.getNamespacePrefixes();
for (int i = 0; i < prefixes.length; ++i) {
top.setPrefix(prefixes[i], tree.getNamespaceURI(prefixes[i]));
}
if (tree.schema != null) {
top.setNamespace(tree.schema);
top.setPrefix("", tree.schema);
}
}
use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.
the class SMSSerializingVisitor method visit.
/*
* (non-Javadoc)
*
* @see
* org.javarosa.core.model.utils.ITreeVisitor#visit(org.javarosa.core.model
* .DataModelTree)
*/
public void visit(FormInstance tree) {
nodeSet = new String();
// TreeElement root = tree.getRoot();
TreeElement root = tree.resolveReference(rootRef);
xmlns = root.getAttributeValue("", "xmlns");
delimiter = root.getAttributeValue("", "delimiter");
if (delimiter == null) {
// for the spelling-impaired...
delimiter = root.getAttributeValue("", "delimeter");
}
prefix = root.getAttributeValue("", "prefix");
xmlns = (xmlns != null) ? xmlns : " ";
delimiter = (delimiter != null) ? delimiter : " ";
prefix = (prefix != null) ? prefix : " ";
// Don't bother adding any delimiters, yet. Delimiters are
// added before tags/data
theSmsStr = prefix;
// serialize each node to get it's answers
for (int j = 0; j < root.getNumChildren(); j++) {
TreeElement tee = root.getChildAt(j);
String e = serializeNode(tee);
if (e != null) {
theSmsStr += e;
}
}
theSmsStr = theSmsStr.trim();
}
use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.
the class Fast2014DagImpl method addChildrenOfReference.
/**
* This is a utility method to get all of the references of a node. It can
* be replaced when we support dependent XPath Steps (IE: /path/to//)
*/
public void addChildrenOfReference(FormInstance mainInstance, EvaluationContext evalContext, TreeReference original, Set<TreeReference> toAdd, boolean expandRepeatables) {
// original has already been added to the 'toAdd' list.
TreeElement repeatTemplate = expandRepeatables ? mainInstance.getTemplatePath(original) : null;
if (repeatTemplate != null) {
for (int i = 0; i < repeatTemplate.getNumChildren(); ++i) {
TreeElement child = repeatTemplate.getChildAt(i);
toAdd.add(child.getRef().genericize());
addChildrenOfElement(mainInstance, evalContext, child, toAdd, expandRepeatables);
}
} else {
List<TreeReference> refSet = evalContext.expandReference(original);
for (TreeReference ref : refSet) {
addChildrenOfElement(mainInstance, evalContext, evalContext.resolveReference(ref), toAdd, expandRepeatables);
}
}
}
Aggregations