Search in sources :

Example 6 with XPathSyntaxException

use of org.javarosa.xpath.parser.XPathSyntaxException 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 7 with XPathSyntaxException

use of org.javarosa.xpath.parser.XPathSyntaxException 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);
}
Also used : Action(org.javarosa.core.model.Action) SetValueAction(org.javarosa.core.model.actions.SetValueAction) XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException) IDataReference(org.javarosa.core.model.IDataReference) TreeReference(org.javarosa.core.model.instance.TreeReference) DataBinding(org.javarosa.core.model.DataBinding) SetValueAction(org.javarosa.core.model.actions.SetValueAction) XPathReference(org.javarosa.model.xform.XPathReference)

Example 8 with XPathSyntaxException

use of org.javarosa.xpath.parser.XPathSyntaxException in project javarosa by opendatakit.

the class XPathReference method getPathExpr.

public static XPathPathExpr getPathExpr(String nodeset) {
    XPathExpression path;
    boolean validNonPathExpr = false;
    try {
        path = XPathParseTool.parseXPath(nodeset);
        if (!(path instanceof XPathPathExpr)) {
            validNonPathExpr = true;
            throw new XPathSyntaxException();
        }
    } catch (XPathSyntaxException xse) {
        // make these checked exceptions?
        if (validNonPathExpr) {
            throw new XPathTypeMismatchException("Expected XPath path, got XPath expression: [" + nodeset + "]," + xse.getMessage());
        } else {
            Std.printStack(xse);
            throw new XPathException("Parse error in XPath path: [" + nodeset + "]." + (xse.getMessage() == null ? "" : "\n" + xse.getMessage()));
        }
    }
    return (XPathPathExpr) path;
}
Also used : XPathExpression(org.javarosa.xpath.expr.XPathExpression) XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException) XPathException(org.javarosa.xpath.XPathException) XPathPathExpr(org.javarosa.xpath.expr.XPathPathExpr) XPathTypeMismatchException(org.javarosa.xpath.XPathTypeMismatchException)

Example 9 with XPathSyntaxException

use of org.javarosa.xpath.parser.XPathSyntaxException in project javarosa by opendatakit.

the class XPathParseTest method testXPathValid.

private void testXPathValid(String expr, String expected) {
    try {
        XPathExpression xpe = XPathParseTool.parseXPath(expr);
        String result = (xpe != null ? xpe.toString() : null);
        if (result == null || !result.equals(expected)) {
            fail("XPath Parse Failed! Incorrect parse tree." + "\n    expression:[" + expr + "]" + "\n    expected:[" + expected + "]" + "\n    result:[" + result + "]");
        }
        // test serialization of parse tree
        ExternalizableTest.testExternalizable(new ExtWrapTagged(xpe), new ExtWrapTagged(), pf, this, "XPath");
    } catch (XPathSyntaxException xse) {
        fail("XPath Parse Failed! Unexpected syntax error." + "\n    expression:[" + expr + "]");
    }
}
Also used : XPathExpression(org.javarosa.xpath.expr.XPathExpression) XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException) ExtWrapTagged(org.javarosa.core.util.externalizable.ExtWrapTagged)

Example 10 with XPathSyntaxException

use of org.javarosa.xpath.parser.XPathSyntaxException in project javarosa by opendatakit.

the class XPathParseTest method testXPathInvalid.

private void testXPathInvalid(String expr) {
    try {
        XPathExpression xpe = XPathParseTool.parseXPath(expr);
        String result = (xpe != null ? xpe.toString() : null);
        fail("XPath Parse Failed! Did not get syntax error as expected." + "\n    expression:[" + expr + "]" + "\n    result:[" + (result == null ? "(null)" : result) + "]");
    } catch (XPathSyntaxException xse) {
    // success: syntax error as expected
    }
}
Also used : XPathExpression(org.javarosa.xpath.expr.XPathExpression) XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException)

Aggregations

XPathSyntaxException (org.javarosa.xpath.parser.XPathSyntaxException)10 XPathExpression (org.javarosa.xpath.expr.XPathExpression)6 XPathConditional (org.javarosa.xpath.XPathConditional)3 XPathException (org.javarosa.xpath.XPathException)3 DataBinding (org.javarosa.core.model.DataBinding)2 IDataReference (org.javarosa.core.model.IDataReference)2 Condition (org.javarosa.core.model.condition.Condition)2 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)2 XPathReference (org.javarosa.model.xform.XPathReference)2 XPathNodeset (org.javarosa.xpath.XPathNodeset)2 SuppressLint (android.annotation.SuppressLint)1 TextView (android.widget.TextView)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Action (org.javarosa.core.model.Action)1 FormDef (org.javarosa.core.model.FormDef)1 SetValueAction (org.javarosa.core.model.actions.SetValueAction)1 Recalculate (org.javarosa.core.model.condition.Recalculate)1 TreeElement (org.javarosa.core.model.instance.TreeElement)1 TreeReference (org.javarosa.core.model.instance.TreeReference)1