Search in sources :

Example 1 with Script

use of org.apache.commons.jexl3.internal.Script in project nexus-public by sonatype.

the class DatastoreCselToSqlTest method refTest.

@Test
public void refTest() {
    final ASTJexlScript script = jexlEngine.parseExpression("a == dog.name");
    script.childrenAccept(underTest, builder);
    assertThat(builder.getQueryString(), is("a_alias = prop.name"));
    assertThat(builder.getQueryParameters().size(), is(0));
}
Also used : ASTJexlScript(org.apache.commons.jexl3.parser.ASTJexlScript) Test(org.junit.Test)

Example 2 with Script

use of org.apache.commons.jexl3.internal.Script in project nexus-public by sonatype.

the class DatastoreCselToSqlTest method likeTest.

@Test
public void likeTest() {
    final ASTJexlScript script = jexlEngine.parseExpression("a =^ \"woof\"");
    script.childrenAccept(underTest, builder);
    assertThat(builder.getQueryString(), is("a_alias like :param_0"));
    assertThat(builder.getQueryParameters().size(), is(1));
    assertThat(builder.getQueryParameters().get("param_0"), is("woof%"));
}
Also used : ASTJexlScript(org.apache.commons.jexl3.parser.ASTJexlScript) Test(org.junit.Test)

Example 3 with Script

use of org.apache.commons.jexl3.internal.Script in project nexus-public by sonatype.

the class DatastoreCselToSqlTest method parensTest.

@Test
public void parensTest() {
    final ASTJexlScript script = jexlEngine.parseExpression("a==\"woof\" && (b==\"meow\" || b==\"purr\")");
    script.childrenAccept(underTest, builder);
    assertThat(builder.getQueryString(), is("a_alias = :param_0 and (b_alias = :param_1 or b_alias = :param_2)"));
    assertThat(builder.getQueryParameters().size(), is(3));
    assertThat(builder.getQueryParameters().get("param_0"), is("woof"));
    assertThat(builder.getQueryParameters().get("param_1"), is("meow"));
    assertThat(builder.getQueryParameters().get("param_2"), is("purr"));
}
Also used : ASTJexlScript(org.apache.commons.jexl3.parser.ASTJexlScript) Test(org.junit.Test)

Example 4 with Script

use of org.apache.commons.jexl3.internal.Script in project commons-jexl by apache.

the class Interpreter method visit.

@Override
protected Object visit(final ASTJexlScript script, final Object data) {
    if (script instanceof ASTJexlLambda && !((ASTJexlLambda) script).isTopLevel()) {
        return new Closure(this, (ASTJexlLambda) script);
    }
    block = new LexicalFrame(frame, block).defineArgs();
    try {
        final int numChildren = script.jjtGetNumChildren();
        Object result = null;
        for (int i = 0; i < numChildren; i++) {
            final JexlNode child = script.jjtGetChild(i);
            result = child.jjtAccept(this, data);
            cancelCheck(child);
        }
        return result;
    } finally {
        block = block.pop();
    }
}
Also used : ASTJexlLambda(org.apache.commons.jexl3.parser.ASTJexlLambda) JexlNode(org.apache.commons.jexl3.parser.JexlNode)

Example 5 with Script

use of org.apache.commons.jexl3.internal.Script in project commons-jexl by apache.

the class Closure method setCaptured.

/**
 * Sets the captured index of a given symbol, ie the target index of a parent
 * captured symbol in this closure's frame.
 * <p>This is meant to allow a locally defined function to "see" and call
 * itself as a local (captured) variable;
 * in other words, this allows recursive call of a function.
 * @param symbol the symbol index (in the caller of this closure)
 * @param value the value to set in the local frame
 */
public void setCaptured(final int symbol, final Object value) {
    if (script instanceof ASTJexlLambda) {
        final ASTJexlLambda lambda = (ASTJexlLambda) script;
        final Scope scope = lambda.getScope();
        if (scope != null) {
            final Integer reg = scope.getCaptured(symbol);
            if (reg != null) {
                frame.set(reg, value);
            }
        }
    }
}
Also used : ASTJexlLambda(org.apache.commons.jexl3.parser.ASTJexlLambda)

Aggregations

Test (org.junit.Test)27 JexlScript (org.apache.commons.jexl3.JexlScript)24 JexlEngine (org.apache.commons.jexl3.JexlEngine)17 JexlException (org.apache.commons.jexl3.JexlException)17 JexlBuilder (org.apache.commons.jexl3.JexlBuilder)15 ASTJexlScript (org.apache.commons.jexl3.parser.ASTJexlScript)14 JexlContext (org.apache.commons.jexl3.JexlContext)8 JexlNode (org.apache.commons.jexl3.parser.JexlNode)8 MapContext (org.apache.commons.jexl3.MapContext)5 TemplateDebugger (org.apache.commons.jexl3.internal.TemplateDebugger)4 ASTJexlLambda (org.apache.commons.jexl3.parser.ASTJexlLambda)4 JexlFeatures (org.apache.commons.jexl3.JexlFeatures)3 JexlOptions (org.apache.commons.jexl3.JexlOptions)3 ASTIdentifier (org.apache.commons.jexl3.parser.ASTIdentifier)3 StringReader (java.io.StringReader)2 Map (java.util.Map)2 ScriptException (javax.script.ScriptException)2 JexlInfo (org.apache.commons.jexl3.JexlInfo)2 JxltEngine (org.apache.commons.jexl3.JxltEngine)2 Debugger (org.apache.commons.jexl3.internal.Debugger)2