Search in sources :

Example 26 with Script

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

the class DatastoreCselToSqlTest method orTest.

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

Example 27 with Script

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

the class DatastoreCselToSqlTest method notEqualTest.

@Test
public void notEqualTest() {
    final ASTJexlScript script = jexlEngine.parseExpression("a != \"woof\"");
    script.childrenAccept(underTest, builder);
    assertThat(builder.getQueryString(), is("(a_alias is null or a_alias <> :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 28 with Script

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

the class DatastoreCselToSqlTest method andTest.

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

Example 29 with Script

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

the class Engine method parse.

/**
 * Parses an expression.
 *
 * @param info      information structure
 * @param parsingf  the set of parsing features
 * @param src      the expression to parse
 * @param scope     the script frame
 * @return the parsed tree
 * @throws JexlException if any error occurred during parsing
 */
protected ASTJexlScript parse(final JexlInfo info, final JexlFeatures parsingf, final String src, final Scope scope) {
    final boolean cached = src.length() < cacheThreshold && cache != null;
    JexlFeatures features = parsingf != null ? parsingf : DEFAULT_FEATURES;
    // if (features.getNameSpaces().isEmpty() && !functions.isEmpty()) {
    // features = new JexlFeatures(features).nameSpaces(functions.keySet());
    // }
    final Source source = cached ? new Source(features, src) : null;
    ASTJexlScript script;
    if (source != null) {
        script = cache.get(source);
        if (script != null) {
            final Scope f = script.getScope();
            if ((f == null && scope == null) || (f != null && f.equals(scope))) {
                return script;
            }
        }
    }
    final JexlInfo ninfo = info == null && debug ? createInfo() : info;
    // if parser not in use...
    if (parsing.compareAndSet(false, true)) {
        try {
            // lets parse
            script = parser.parse(ninfo, features, src, scope);
        } finally {
            // no longer in use
            parsing.set(false);
        }
    } else {
        // ...otherwise parser was in use, create a new temporary one
        final Parser lparser = new Parser(new StringProvider(";"));
        script = lparser.parse(ninfo, features, src, scope);
    }
    if (source != null) {
        cache.put(source, script);
    }
    return script;
}
Also used : ASTJexlScript(org.apache.commons.jexl3.parser.ASTJexlScript) StringProvider(org.apache.commons.jexl3.parser.StringProvider) JexlFeatures(org.apache.commons.jexl3.JexlFeatures) JexlInfo(org.apache.commons.jexl3.JexlInfo) Parser(org.apache.commons.jexl3.parser.Parser)

Example 30 with Script

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

the class Engine method setProperty.

@Override
public void setProperty(JexlContext context, final Object bean, final String expr, final Object value) {
    // synthesize expr using register
    String src = trimSource(expr);
    src = "#0" + (src.charAt(0) == '[' ? "" : ".") + src + "=" + "#1";
    try {
        final Scope scope = new Scope(null, "#0", "#1");
        final ASTJexlScript script = parse(null, PROPERTY_FEATURES, src, scope);
        final JexlNode node = script.jjtGetChild(0);
        final Frame frame = script.createFrame(bean, value);
        final Interpreter interpreter = createInterpreter(context != null ? context : EMPTY_CONTEXT, frame, options);
        interpreter.visitLexicalNode(node, null);
    } catch (final JexlException xjexl) {
        if (silent) {
            if (logger.isWarnEnabled()) {
                logger.warn(xjexl.getMessage(), xjexl.getCause());
            }
            return;
        }
        throw xjexl.clean();
    }
}
Also used : ASTJexlScript(org.apache.commons.jexl3.parser.ASTJexlScript) JexlException(org.apache.commons.jexl3.JexlException) JexlNode(org.apache.commons.jexl3.parser.JexlNode)

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