Search in sources :

Example 11 with XPathExpression

use of org.javarosa.xpath.expr.XPathExpression 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 12 with XPathExpression

use of org.javarosa.xpath.expr.XPathExpression in project javarosa by opendatakit.

the class ASTNodeLocPath method build.

public XPathExpression build() throws XPathSyntaxException {
    Vector<XPathStep> steps = new Vector<XPathStep>();
    XPathExpression filtExpr = null;
    int offset = isAbsolute() ? 1 : 0;
    for (int i = 0; i < clauses.size() + offset; i++) {
        if (offset == 0 || i > 0) {
            if (clauses.elementAt(i - offset) instanceof ASTNodePathStep) {
                steps.addElement(((ASTNodePathStep) clauses.elementAt(i - offset)).getStep());
            } else {
                filtExpr = clauses.elementAt(i - offset).build();
            }
        }
        if (i < separators.size()) {
            if (Parser.vectInt(separators, i) == Token.DBL_SLASH) {
                steps.addElement(XPathStep.ABBR_DESCENDANTS());
            }
        }
    }
    XPathStep[] stepArr = new XPathStep[steps.size()];
    for (int i = 0; i < stepArr.length; i++) stepArr[i] = steps.elementAt(i);
    if (filtExpr == null) {
        return new XPathPathExpr(isAbsolute() ? XPathPathExpr.INIT_CONTEXT_ROOT : XPathPathExpr.INIT_CONTEXT_RELATIVE, stepArr);
    } else {
        if (filtExpr instanceof XPathFilterExpr) {
            return new XPathPathExpr((XPathFilterExpr) filtExpr, stepArr);
        } else {
            return new XPathPathExpr(new XPathFilterExpr(filtExpr, new XPathExpression[0]), stepArr);
        }
    }
}
Also used : XPathExpression(org.javarosa.xpath.expr.XPathExpression) XPathPathExpr(org.javarosa.xpath.expr.XPathPathExpr) XPathStep(org.javarosa.xpath.expr.XPathStep) XPathFilterExpr(org.javarosa.xpath.expr.XPathFilterExpr) Vector(java.util.Vector)

Example 13 with XPathExpression

use of org.javarosa.xpath.expr.XPathExpression 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 14 with XPathExpression

use of org.javarosa.xpath.expr.XPathExpression 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

XPathExpression (org.javarosa.xpath.expr.XPathExpression)14 XPathSyntaxException (org.javarosa.xpath.parser.XPathSyntaxException)6 XPathPathExpr (org.javarosa.xpath.expr.XPathPathExpr)5 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)4 FormDef (org.javarosa.core.model.FormDef)3 TreeElement (org.javarosa.core.model.instance.TreeElement)3 XPathNodeset (org.javarosa.xpath.XPathNodeset)3 ArrayList (java.util.ArrayList)2 TreeReference (org.javarosa.core.model.instance.TreeReference)2 XPathException (org.javarosa.xpath.XPathException)2 XPathFuncExpr (org.javarosa.xpath.expr.XPathFuncExpr)2 SuppressLint (android.annotation.SuppressLint)1 Cursor (android.database.Cursor)1 TextView (android.widget.TextView)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Vector (java.util.Vector)1