Search in sources :

Example 11 with FormDef

use of org.javarosa.core.model.FormDef in project javarosa by opendatakit.

the class XFormParserTest method parsesRangeForm.

@Test
public void parsesRangeForm() throws IOException {
    FormDef formDef = parse(r("range-form.xml")).formDef;
    RangeQuestion question = (RangeQuestion) formDef.getChild(0);
    assertEquals(CONTROL_RANGE, question.getControlType());
    assertEquals(-2.0d, question.getRangeStart().doubleValue(), 0);
    assertEquals(2.0d, question.getRangeEnd().doubleValue(), 0);
    assertEquals(0.5d, question.getRangeStep().doubleValue(), 0);
}
Also used : FormDef(org.javarosa.core.model.FormDef) RangeQuestion(org.javarosa.core.model.RangeQuestion) Test(org.junit.Test)

Example 12 with FormDef

use of org.javarosa.core.model.FormDef in project javarosa by opendatakit.

the class XFormParserTest method parsesPreloadForm.

@Test
public void parsesPreloadForm() throws IOException {
    FormDef formDef = parse(r("Sample-Preloading.xml")).formDef;
    assertEquals("Sample Form - Preloading", formDef.getTitle());
}
Also used : FormDef(org.javarosa.core.model.FormDef) Test(org.junit.Test)

Example 13 with FormDef

use of org.javarosa.core.model.FormDef 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 14 with FormDef

use of org.javarosa.core.model.FormDef in project javarosa by opendatakit.

the class XFormsModule method registerModule.

public void registerModule() {
    String[] classes = { "org.javarosa.model.xform.XPathReference", "org.javarosa.xpath.XPathConditional" };
    PrototypeManager.registerPrototypes(classes);
    PrototypeManager.registerPrototypes(XPathParseTool.xpathClasses);
    RestoreUtils.xfFact = new IXFormyFactory() {

        public TreeReference ref(String refStr) {
            return FormInstance.unpackReference(new XPathReference(refStr));
        }

        public IDataPayload serializeInstance(FormInstance dm) {
            try {
                return (new XFormSerializingVisitor()).createSerializedPayload(dm);
            } catch (IOException e) {
                return null;
            }
        }

        public FormInstance parseRestore(byte[] data, Class restorableType) {
            return XFormParser.restoreDataModel(data, restorableType);
        }

        public IAnswerData parseData(String textVal, int dataType, TreeReference ref, FormDef f) {
            return XFormAnswerDataParser.getAnswerData(textVal, dataType, XFormParser.ghettoGetQuestionDef(dataType, f, ref));
        }

        public String serializeData(IAnswerData data) {
            return (String) (new XFormAnswerDataSerializer().serializeAnswerData(data));
        }

        public IConditionExpr refToPathExpr(TreeReference ref) {
            return new XPathConditional(XPathPathExpr.fromRef(ref));
        }
    };
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) IConditionExpr(org.javarosa.core.model.condition.IConditionExpr) IXFormyFactory(org.javarosa.core.model.util.restorable.IXFormyFactory) IOException(java.io.IOException) XFormAnswerDataSerializer(org.javarosa.xform.util.XFormAnswerDataSerializer) TreeReference(org.javarosa.core.model.instance.TreeReference) FormDef(org.javarosa.core.model.FormDef) FormInstance(org.javarosa.core.model.instance.FormInstance) XPathConditional(org.javarosa.xpath.XPathConditional) IDataPayload(org.javarosa.core.services.transport.payload.IDataPayload)

Example 15 with FormDef

use of org.javarosa.core.model.FormDef in project javarosa by opendatakit.

the class XFormUtils method getFormFromSerializedResource.

public static FormDef getFormFromSerializedResource(String resource) {
    FormDef returnForm = null;
    InputStream is = System.class.getResourceAsStream(resource);
    DataInputStream dis = null;
    try {
        if (is != null) {
            dis = new DataInputStream(is);
            returnForm = (FormDef) ExtUtil.read(dis, FormDef.class);
        } else {
            // #if debug.output==verbose
            Std.out.println("ResourceStream NULL");
        // #endif
        }
    } catch (IOException e) {
        Std.printStack(e);
    } catch (DeserializationException e) {
        Std.printStack(e);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                Std.printStack(e);
            }
        }
        if (dis != null) {
            try {
                dis.close();
            } catch (IOException e) {
                Std.printStack(e);
            }
        }
    }
    return returnForm;
}
Also used : FormDef(org.javarosa.core.model.FormDef) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) DeserializationException(org.javarosa.core.util.externalizable.DeserializationException)

Aggregations

FormDef (org.javarosa.core.model.FormDef)31 Test (org.junit.Test)10 IOException (java.io.IOException)9 TreeElement (org.javarosa.core.model.instance.TreeElement)7 FormInstance (org.javarosa.core.model.instance.FormInstance)5 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)4 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)4 DataInputStream (java.io.DataInputStream)3 SubmissionProfile (org.javarosa.core.model.SubmissionProfile)3 IAnswerData (org.javarosa.core.model.data.IAnswerData)3 TreeReference (org.javarosa.core.model.instance.TreeReference)3 FormEntryController (org.javarosa.form.api.FormEntryController)3 ParseResult (org.javarosa.xform.parse.FormParserHelper.ParseResult)3 XPathExpression (org.javarosa.xpath.expr.XPathExpression)3 InputStream (java.io.InputStream)2 GroupDef (org.javarosa.core.model.GroupDef)2 IDataReference (org.javarosa.core.model.IDataReference)2 QuestionDef (org.javarosa.core.model.QuestionDef)2