Search in sources :

Example 21 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.

the class XPathEvalTest method createTestInstance.

public FormInstance createTestInstance() {
    TreeElement data = new TreeElement("data");
    data.addChild(new TreeElement("path"));
    return new FormInstance(data);
}
Also used : FormInstance(org.javarosa.core.model.instance.FormInstance) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 22 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.

the class XPathEvalTest method createCountNonEmptyTestInstance.

public FormInstance createCountNonEmptyTestInstance() {
    TreeElement data = new TreeElement("data");
    TreeElement path = new TreeElement("path", 0);
    path.addChild(new TreeElement("child", 0));
    path.addChild(new TreeElement("child", 1));
    data.addChild(path);
    data.addChild(new TreeElement("path", 1));
    path = new TreeElement("path", 2);
    path.setValue(new StringData("some value"));
    data.addChild(path);
    path = new TreeElement("path", 3);
    path.addChild(new TreeElement("child", 0));
    data.addChild(path);
    data.addChild(new TreeElement("path", 4));
    return new FormInstance(data);
}
Also used : StringData(org.javarosa.core.model.data.StringData) FormInstance(org.javarosa.core.model.instance.FormInstance) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 23 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.

the class XPathEvalTest method createTestDataForIndexedRepeatFunction.

private FormInstance createTestDataForIndexedRepeatFunction(Integer indexNodeValue) {
    TreeElement root = new TreeElement("data");
    TreeElement repeat = new TreeElement("repeat");
    TreeElement repeatChild = new TreeElement("name");
    repeatChild.setAnswer(new StringData("A"));
    repeat.addChild(repeatChild);
    root.addChild(repeat);
    repeat = new TreeElement("repeat");
    repeatChild = new TreeElement("name");
    repeatChild.setAnswer(new StringData("B"));
    repeat.addChild(repeatChild);
    root.addChild(repeat);
    repeat = new TreeElement("repeat");
    repeatChild = new TreeElement("name");
    repeatChild.setAnswer(new StringData("C"));
    repeat.addChild(repeatChild);
    root.addChild(repeat);
    TreeElement index = new TreeElement("index1");
    if (indexNodeValue != null) {
        index.setValue(new IntegerData(1));
    }
    return new FormInstance(root);
}
Also used : IntegerData(org.javarosa.core.model.data.IntegerData) StringData(org.javarosa.core.model.data.StringData) FormInstance(org.javarosa.core.model.instance.FormInstance) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 24 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.

the class XFormParser method parseDoc.

private void parseDoc(Map<String, String> namespacePrefixesByUri) {
    final CodeTimer codeTimer = new CodeTimer("Creating FormDef from parsed XML");
    _f = new FormDef();
    initState();
    final String defaultNamespace = _xmldoc.getRootElement().getNamespaceUri(null);
    parseElement(_xmldoc.getRootElement(), _f, topLevelHandlers);
    collapseRepeatGroups(_f);
    final FormInstanceParser instanceParser = new FormInstanceParser(_f, defaultNamespace, reporter, bindings, repeats, itemsets, selectOnes, selectMultis, actionTargets);
    // if this assumption is wrong, well, then we're screwed.
    if (instanceNodes.size() > 1) {
        for (int instanceIndex = 1; instanceIndex < instanceNodes.size(); instanceIndex++) {
            final Element instance = instanceNodes.get(instanceIndex);
            final String instanceId = instanceNodeIdStrs.get(instanceIndex);
            final String ediPath = getPathIfExternalDataInstance(instance.getAttributeValue(null, "src"));
            if (ediPath != null) {
                try {
                    /* todo implement better error handling */
                    _f.addNonMainInstance(ExternalDataInstance.buildFromPath(ediPath, instanceId));
                } catch (IOException | UnfullfilledRequirementsException | InvalidStructureException | XmlPullParserException e) {
                    e.printStackTrace();
                }
            } else {
                FormInstance fi = instanceParser.parseInstance(instance, false, instanceNodeIdStrs.get(instanceNodes.indexOf(instance)), namespacePrefixesByUri);
                // same situation as below
                loadNamespaces(_xmldoc.getRootElement(), fi);
                loadInstanceData(instance, fi.getRoot(), _f);
                _f.addNonMainInstance(fi);
            }
        }
    }
    // now parse the main instance
    if (mainInstanceNode != null) {
        FormInstance fi = instanceParser.parseInstance(mainInstanceNode, true, instanceNodeIdStrs.get(instanceNodes.indexOf(mainInstanceNode)), namespacePrefixesByUri);
        /*
             Load namespaces definition (map of prefixes -> URIs) into a form instance so later it can be used
             during the form instance serialization (XFormSerializingVisitor#visit). If the map is not present, then
             serializer will provide own prefixes for the namespaces present in the nodes.
             This will lead to inconsistency between prefixes used in the form definition (bindings)
             and prefixes in the form instance after the instance is restored and inserted into the form definition.
             */
        loadNamespaces(_xmldoc.getRootElement(), fi);
        addMainInstanceToFormDef(mainInstanceNode, fi);
    }
    // Clear the caches, as these may not have been initialized
    // entirely correctly during the validation steps.
    Enumeration<DataInstance> e = _f.getNonMainInstances();
    while (e.hasMoreElements()) {
        DataInstance instance = e.nextElement();
        final AbstractTreeElement treeElement = instance.getRoot();
        if (treeElement instanceof TreeElement) {
            ((TreeElement) treeElement).clearChildrenCaches();
        }
        treeElement.clearCaches();
    }
    _f.getMainInstance().getRoot().clearChildrenCaches();
    _f.getMainInstance().getRoot().clearCaches();
    codeTimer.logDone();
}
Also used : AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) ExternalDataInstance.getPathIfExternalDataInstance(org.javarosa.core.model.instance.ExternalDataInstance.getPathIfExternalDataInstance) DataInstance(org.javarosa.core.model.instance.DataInstance) ExternalDataInstance(org.javarosa.core.model.instance.ExternalDataInstance) UnfullfilledRequirementsException(org.javarosa.xml.util.UnfullfilledRequirementsException) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) Element(org.kxml2.kdom.Element) IFormElement(org.javarosa.core.model.IFormElement) InvalidStructureException(org.javarosa.xml.util.InvalidStructureException) IOException(java.io.IOException) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) FormDef(org.javarosa.core.model.FormDef) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) FormInstance(org.javarosa.core.model.instance.FormInstance) CodeTimer(org.javarosa.core.util.CodeTimer)

Example 25 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.

the class XFormParser method loadInstanceData.

// TODO: hook here for turning sub-trees into complex IAnswerData objects (like for immunizations)
// FIXME: the 'ref' and FormDef parameters (along with the helper function above that initializes them) are only needed so that we
// can fetch QuestionDefs bound to the given node, as the QuestionDef reference is needed to properly represent answers
// to select questions. obviously, we want to fix this.
private static void loadInstanceData(Element node, TreeElement cur, FormDef f) {
    int numChildren = node.getChildCount();
    boolean hasElements = false;
    for (int i = 0; i < numChildren; i++) {
        if (node.getType(i) == Node.ELEMENT) {
            hasElements = true;
            break;
        }
    }
    if (hasElements) {
        // stores max multiplicity seen for a given node name thus far
        HashMap<String, Integer> multiplicities = new HashMap<>();
        for (int i = 0; i < numChildren; i++) {
            if (node.getType(i) == Node.ELEMENT) {
                Element child = node.getElement(i);
                String name = child.getName();
                int index;
                boolean isTemplate = isTemplate(child);
                if (isTemplate) {
                    index = TreeReference.INDEX_TEMPLATE;
                } else {
                    // update multiplicity counter
                    Integer mult = multiplicities.get(name);
                    index = (mult == null ? 0 : mult + 1);
                    multiplicities.put(name, index);
                }
                loadInstanceData(child, cur.getChild(name, index), f);
            }
        }
    } else {
        String text = getXMLText(node, true);
        if (text != null && text.trim().length() > 0) {
            // ignore text that is only whitespace
            // TODO: custom data types? modelPrototypes?
            cur.setValue(XFormAnswerDataParser.getAnswerData(text, cur.getDataType(), ghettoGetQuestionDef(cur.getDataType(), f, cur.getRef())));
        }
    }
}
Also used : HashMap(java.util.HashMap) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) Element(org.kxml2.kdom.Element) IFormElement(org.javarosa.core.model.IFormElement)

Aggregations

TreeElement (org.javarosa.core.model.instance.TreeElement)86 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)33 TreeReference (org.javarosa.core.model.instance.TreeReference)28 FormInstance (org.javarosa.core.model.instance.FormInstance)16 ArrayList (java.util.ArrayList)15 Constraint (org.javarosa.core.model.condition.Constraint)11 Test (org.junit.Test)10 Element (org.kxml2.kdom.Element)9 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)8 FormDef (org.javarosa.core.model.FormDef)7 InstanceInitializationFactory (org.javarosa.core.model.instance.InstanceInitializationFactory)7 IOException (java.io.IOException)6 IFormElement (org.javarosa.core.model.IFormElement)6 StringData (org.javarosa.core.model.data.StringData)6 HashMap (java.util.HashMap)5 IAnswerData (org.javarosa.core.model.data.IAnswerData)4 File (java.io.File)3 GroupDef (org.javarosa.core.model.GroupDef)3 IDataReference (org.javarosa.core.model.IDataReference)3 QuestionDef (org.javarosa.core.model.QuestionDef)3