Search in sources :

Example 1 with XPathConditional

use of org.javarosa.xpath.XPathConditional in project javarosa by opendatakit.

the class StandardBindAttributesProcessor method buildCondition.

private Condition buildCondition(String xpath, String type, IDataReference contextRef) {
    final int trueAction;
    final int falseAction;
    final String prettyType;
    switch(type) {
        case "relevant":
            prettyType = "display";
            trueAction = Condition.ACTION_SHOW;
            falseAction = Condition.ACTION_HIDE;
            break;
        case "required":
            prettyType = "require";
            trueAction = Condition.ACTION_REQUIRE;
            falseAction = Condition.ACTION_DONT_REQUIRE;
            break;
        case "readonly":
            prettyType = "readonly";
            trueAction = Condition.ACTION_DISABLE;
            falseAction = Condition.ACTION_ENABLE;
            break;
        default:
            throw new XFormParseException("Unsupported type " + type + " passed to buildCondition");
    }
    final XPathConditional xPathConditional;
    try {
        xPathConditional = new XPathConditional(xpath);
    } catch (XPathSyntaxException xse) {
        String errorMessage = "Encountered a problem with " + prettyType + " condition for node [" + contextRef.getReference().toString() + "] at line: " + xpath + ", " + xse.getMessage();
        reporter.error(errorMessage);
        throw new XFormParseException(errorMessage);
    }
    return new Condition(xPathConditional, trueAction, falseAction, FormInstance.unpackReference(contextRef));
}
Also used : Condition(org.javarosa.core.model.condition.Condition) XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException) XPathConditional(org.javarosa.xpath.XPathConditional)

Example 2 with XPathConditional

use of org.javarosa.xpath.XPathConditional in project javarosa by opendatakit.

the class XFormParser method parseOutput.

private String parseOutput(Element e) {
    List<String> usedAtts = new ArrayList<>();
    usedAtts.add(REF_ATTR);
    usedAtts.add(VALUE);
    String xpath = e.getAttributeValue(null, REF_ATTR);
    if (xpath == null) {
        xpath = e.getAttributeValue(null, VALUE);
    }
    if (xpath == null) {
        throw new XFormParseException("XForm Parse: <output> without 'ref' or 'value'", e);
    }
    XPathConditional expr = null;
    try {
        expr = new XPathConditional(xpath);
    } catch (XPathSyntaxException xse) {
        reporter.error("Invalid XPath expression in <output> [" + xpath + "]! " + xse.getMessage());
        return "";
    }
    int index = -1;
    if (_f.getOutputFragments().contains(expr)) {
        index = _f.getOutputFragments().indexOf(expr);
    } else {
        index = _f.getOutputFragments().size();
        _f.getOutputFragments().add(expr);
    }
    if (XFormUtils.showUnusedAttributeWarning(e, usedAtts)) {
        reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(e, usedAtts), getVagueLocation(e));
    }
    return String.valueOf(index);
}
Also used : XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException) ArrayList(java.util.ArrayList) XPathConditional(org.javarosa.xpath.XPathConditional)

Example 3 with XPathConditional

use of org.javarosa.xpath.XPathConditional 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 4 with XPathConditional

use of org.javarosa.xpath.XPathConditional in project javarosa by opendatakit.

the class StandardBindAttributesProcessor method createBinding.

DataBinding createBinding(IXFormParserFunctions parserFunctions, FormDef formDef, Collection<String> usedAttributes, Collection<String> passedThroughAttributes, Element element) {
    final DataBinding binding = new DataBinding();
    binding.setId(element.getAttributeValue("", ID_ATTR));
    final String nodeset = element.getAttributeValue(null, NODESET_ATTR);
    if (nodeset == null) {
        throw new XFormParseException("XForm Parse: <bind> without nodeset", element);
    }
    IDataReference ref;
    try {
        ref = new XPathReference(nodeset);
    } catch (XPathException xpe) {
        throw new XFormParseException(xpe.getMessage());
    }
    ref = parserFunctions.getAbsRef(ref, formDef);
    binding.setReference(ref);
    binding.setDataType(getDataType(element.getAttributeValue(null, "type")));
    String xpathRel = element.getAttributeValue(null, "relevant");
    if (xpathRel != null) {
        if ("true()".equals(xpathRel)) {
            binding.relevantAbsolute = true;
        } else if ("false()".equals(xpathRel)) {
            binding.relevantAbsolute = false;
        } else {
            Condition c = buildCondition(xpathRel, "relevant", ref);
            c = (Condition) formDef.addTriggerable(c);
            binding.relevancyCondition = c;
        }
    }
    String xpathReq = element.getAttributeValue(null, "required");
    if (xpathReq != null) {
        if ("true()".equals(xpathReq)) {
            binding.requiredAbsolute = true;
        } else if ("false()".equals(xpathReq)) {
            binding.requiredAbsolute = false;
        } else {
            Condition c = buildCondition(xpathReq, "required", ref);
            c = (Condition) formDef.addTriggerable(c);
            binding.requiredCondition = c;
        }
    }
    String xpathRO = element.getAttributeValue(null, "readonly");
    if (xpathRO != null) {
        if ("true()".equals(xpathRO)) {
            binding.readonlyAbsolute = true;
        } else if ("false()".equals(xpathRO)) {
            binding.readonlyAbsolute = false;
        } else {
            Condition c = buildCondition(xpathRO, "readonly", ref);
            c = (Condition) formDef.addTriggerable(c);
            binding.readonlyCondition = c;
        }
    }
    final String xpathConstr = element.getAttributeValue(null, "constraint");
    if (xpathConstr != null) {
        try {
            binding.constraint = new XPathConditional(xpathConstr);
        } catch (XPathSyntaxException xse) {
            throw new XFormParseException("bind for " + nodeset + " contains invalid constraint expression [" + xpathConstr + "] " + xse.getMessage());
        }
        binding.constraintMessage = element.getAttributeValue(NAMESPACE_JAVAROSA, "constraintMsg");
    }
    final String xpathCalc = element.getAttributeValue(null, "calculate");
    if (xpathCalc != null) {
        Recalculate r;
        try {
            r = buildCalculate(xpathCalc, ref);
        } catch (XPathSyntaxException xpse) {
            throw new XFormParseException("Invalid calculate for the bind attached to \"" + nodeset + "\" : " + xpse.getMessage() + " in expression " + xpathCalc);
        }
        r = (Recalculate) formDef.addTriggerable(r);
        binding.calculate = r;
    }
    binding.setPreload(element.getAttributeValue(NAMESPACE_JAVAROSA, "preload"));
    binding.setPreloadParams(element.getAttributeValue(NAMESPACE_JAVAROSA, "preloadParams"));
    saveUnusedAttributes(binding, element, usedAttributes, passedThroughAttributes);
    return binding;
}
Also used : Condition(org.javarosa.core.model.condition.Condition) XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException) Recalculate(org.javarosa.core.model.condition.Recalculate) XPathException(org.javarosa.xpath.XPathException) IDataReference(org.javarosa.core.model.IDataReference) DataBinding(org.javarosa.core.model.DataBinding) XPathConditional(org.javarosa.xpath.XPathConditional) XPathReference(org.javarosa.model.xform.XPathReference)

Example 5 with XPathConditional

use of org.javarosa.xpath.XPathConditional in project javarosa by opendatakit.

the class XFormParser method parseItemset.

private void parseItemset(QuestionDef q, Element e, IFormElement qparent) {
    ItemsetBinding itemset = new ItemsetBinding();
    // //////////////USED FOR PARSER WARNING OUTPUT ONLY
    // catalogue of used attributes in this method/element
    List<String> usedAtts = new ArrayList<>();
    // for child with name 'label'
    List<String> labelUA = new ArrayList<>();
    // for child with name 'value'
    List<String> valueUA = new ArrayList<>();
    // for child with name 'copy'
    List<String> copyUA = new ArrayList<>();
    usedAtts.add(NODESET_ATTR);
    labelUA.add(REF_ATTR);
    valueUA.add(REF_ATTR);
    valueUA.add(FORM_ATTR);
    copyUA.add(REF_ATTR);
    // //////////////////////////////////////////////////
    /*
         * At this point in time, we cannot construct a valid nodesetRef
         *
         * Leave all ...Ref entries as null and test the ...Expr entries for null / non-null values.
         *
         * We will patch this all up in the verifyItemsetBindings() method.
         */
    String nodesetStr = e.getAttributeValue("", NODESET_ATTR);
    if (nodesetStr == null)
        throw new RuntimeException("No nodeset attribute in element: [" + e.getName() + "]. This is required. (Element Printout:" + XFormSerializer.elementToString(e) + ")");
    XPathPathExpr path = XPathReference.getPathExpr(nodesetStr);
    itemset.nodesetExpr = new XPathConditional(path);
    itemset.contextRef = getFormElementRef(qparent);
    // this is not valid yet...
    itemset.nodesetRef = null;
    // itemset.nodesetRef = FormInstance.unpackReference(getAbsRef(new XPathReference(path.getReference(true)), itemset.contextRef));
    itemset.copyMode = false;
    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);
        if (LABEL_ELEMENT.equals(childName)) {
            String labelXpath = child.getAttributeValue("", REF_ATTR);
            boolean labelItext = false;
            // print unused attribute warning message for child element
            if (XFormUtils.showUnusedAttributeWarning(child, labelUA)) {
                reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(child, labelUA), getVagueLocation(child));
            }
            if (labelXpath != null) {
                if (labelXpath.startsWith(DYNAMIC_ITEXT_OPEN) && labelXpath.endsWith(DYNAMIC_ITEXT_CLOSE)) {
                    labelXpath = labelXpath.substring(DYNAMIC_ITEXT_OPEN.length(), labelXpath.lastIndexOf(DYNAMIC_ITEXT_CLOSE));
                    labelItext = true;
                }
            } else {
                throw new XFormParseException("<label> in <itemset> requires 'ref'");
            }
            XPathPathExpr labelPath = XPathReference.getPathExpr(labelXpath);
            itemset.labelRef = null;
            // itemset.labelRef = FormInstance.unpackReference(getAbsRef(new XPathReference(labelPath), itemset.nodesetRef));
            itemset.labelExpr = new XPathConditional(labelPath);
            itemset.labelIsItext = labelItext;
        } else if ("copy".equals(childName)) {
            String copyXpath = child.getAttributeValue("", REF_ATTR);
            // print unused attribute warning message for child element
            if (XFormUtils.showUnusedAttributeWarning(child, copyUA)) {
                reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(child, copyUA), getVagueLocation(child));
            }
            if (copyXpath == null) {
                throw new XFormParseException("<copy> in <itemset> requires 'ref'");
            }
            XPathPathExpr copyPath = XPathReference.getPathExpr(copyXpath);
            itemset.copyRef = null;
            // itemset.copyRef = FormInstance.unpackReference(getAbsRef(new XPathReference(copyPath), itemset.nodesetRef));
            itemset.copyExpr = new XPathConditional(copyPath);
            itemset.copyMode = true;
        } else if (VALUE.equals(childName)) {
            String valueXpath = child.getAttributeValue("", REF_ATTR);
            // print unused attribute warning message for child element
            if (XFormUtils.showUnusedAttributeWarning(child, valueUA)) {
                reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(child, valueUA), getVagueLocation(child));
            }
            if (valueXpath == null) {
                throw new XFormParseException("<value> in <itemset> requires 'ref'");
            }
            XPathPathExpr valuePath = XPathReference.getPathExpr(valueXpath);
            itemset.valueRef = null;
            // itemset.valueRef = FormInstance.unpackReference(getAbsRef(new XPathReference(valuePath), itemset.nodesetRef));
            itemset.valueExpr = new XPathConditional(valuePath);
        }
    }
    if (itemset.labelExpr == null) {
        throw new XFormParseException("<itemset> requires <label>");
    } else if (itemset.copyExpr == null && itemset.valueExpr == null) {
        throw new XFormParseException("<itemset> requires <copy> or <value>");
    }
    if (itemset.copyExpr != null) {
        if (itemset.valueExpr == null) {
            reporter.warning(XFormParserReporter.TYPE_TECHNICAL, "<itemset>s with <copy> are STRONGLY recommended to have <value> as well; pre-selecting, default answers, and display of answers will not work properly otherwise", getVagueLocation(e));
        }
    }
    itemsets.add(itemset);
    q.setDynamicChoices(itemset);
    // 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));
    }
}
Also used : XPathPathExpr(org.javarosa.xpath.expr.XPathPathExpr) 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) ArrayList(java.util.ArrayList) ItemsetBinding(org.javarosa.core.model.ItemsetBinding) XPathConditional(org.javarosa.xpath.XPathConditional)

Aggregations

XPathConditional (org.javarosa.xpath.XPathConditional)5 XPathSyntaxException (org.javarosa.xpath.parser.XPathSyntaxException)3 ArrayList (java.util.ArrayList)2 Condition (org.javarosa.core.model.condition.Condition)2 IOException (java.io.IOException)1 DataBinding (org.javarosa.core.model.DataBinding)1 FormDef (org.javarosa.core.model.FormDef)1 IDataReference (org.javarosa.core.model.IDataReference)1 IFormElement (org.javarosa.core.model.IFormElement)1 ItemsetBinding (org.javarosa.core.model.ItemsetBinding)1 IConditionExpr (org.javarosa.core.model.condition.IConditionExpr)1 Recalculate (org.javarosa.core.model.condition.Recalculate)1 IAnswerData (org.javarosa.core.model.data.IAnswerData)1 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)1 FormInstance (org.javarosa.core.model.instance.FormInstance)1 TreeElement (org.javarosa.core.model.instance.TreeElement)1 TreeReference (org.javarosa.core.model.instance.TreeReference)1 IXFormyFactory (org.javarosa.core.model.util.restorable.IXFormyFactory)1 IDataPayload (org.javarosa.core.services.transport.payload.IDataPayload)1 XPathReference (org.javarosa.model.xform.XPathReference)1