Search in sources :

Example 1 with JexlOptions

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

the class TemplateInterpreter 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) {

            @Override
            protected Interpreter createInterpreter(final JexlContext context, final Frame local) {
                final JexlOptions opts = jexl.evalOptions(script, context);
                final TemplateInterpreter.Arguments targs = new TemplateInterpreter.Arguments(jexl).context(context).options(opts).frame(local).expressions(exprs).writer(writer);
                return new TemplateInterpreter(targs);
            }
        };
    }
    // otherwise...
    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;
}
Also used : ASTJexlLambda(org.apache.commons.jexl3.parser.ASTJexlLambda) JexlContext(org.apache.commons.jexl3.JexlContext) JexlOptions(org.apache.commons.jexl3.JexlOptions) JexlNode(org.apache.commons.jexl3.parser.JexlNode)

Example 2 with JexlOptions

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

the class TemplateScript method evaluate.

@Override
public void evaluate(final JexlContext context, final Writer writer, final Object... args) {
    final Engine jexl = jxlt.getEngine();
    final JexlOptions options = jexl.evalOptions(script, context);
    final Frame frame = script.createFrame(args);
    final TemplateInterpreter.Arguments targs = new TemplateInterpreter.Arguments(jexl).context(context).options(options).frame(frame).expressions(exprs).writer(writer);
    final Interpreter interpreter = new TemplateInterpreter(targs);
    interpreter.interpret(script);
}
Also used : JexlOptions(org.apache.commons.jexl3.JexlOptions) JxltEngine(org.apache.commons.jexl3.JxltEngine)

Example 3 with JexlOptions

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

the class TemplateScript method prepare.

@Override
public TemplateScript prepare(final JexlContext context) {
    final Engine jexl = jxlt.getEngine();
    final JexlOptions options = jexl.evalOptions(script, context);
    final Frame frame = script.createFrame((Object[]) null);
    final TemplateInterpreter.Arguments targs = new TemplateInterpreter.Arguments(jxlt.getEngine()).context(context).options(options).frame(frame);
    final Interpreter interpreter = new TemplateInterpreter(targs);
    final TemplateExpression[] immediates = new TemplateExpression[exprs.length];
    for (int e = 0; e < exprs.length; ++e) {
        try {
            immediates[e] = exprs[e].prepare(interpreter);
        } catch (final JexlException xjexl) {
            final JexlException xuel = TemplateEngine.createException(xjexl.getInfo(), "prepare", exprs[e], xjexl);
            if (jexl.isSilent()) {
                if (jexl.logger.isWarnEnabled()) {
                    jexl.logger.warn(xuel.getMessage(), xuel.getCause());
                }
                return null;
            }
            throw xuel;
        }
    }
    return new TemplateScript(jxlt, prefix, source, script, immediates);
}
Also used : JexlException(org.apache.commons.jexl3.JexlException) JexlOptions(org.apache.commons.jexl3.JexlOptions) TemplateExpression(org.apache.commons.jexl3.internal.TemplateEngine.TemplateExpression) JxltEngine(org.apache.commons.jexl3.JxltEngine)

Example 4 with JexlOptions

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

the class LexicalTest method testInternalLexicalFeatures.

@Test
public void testInternalLexicalFeatures() throws Exception {
    final String str = "42";
    final JexlFeatures f = new JexlFeatures();
    f.lexical(true);
    f.lexicalShade(true);
    final JexlEngine jexl = new JexlBuilder().features(f).create();
    final JexlScript e = jexl.createScript(str);
    final VarContext vars = new VarContext();
    final JexlOptions opts = vars.getEngineOptions();
    // so we can see the effect of features on options
    opts.setSharedInstance(true);
    final Script script = (Script) e;
    final JexlFeatures features = script.getFeatures();
    Assert.assertTrue(features.isLexical());
    Assert.assertTrue(features.isLexicalShade());
    final Object result = e.execute(vars);
    Assert.assertEquals(42, result);
    Assert.assertTrue(opts.isLexical());
    Assert.assertTrue(opts.isLexicalShade());
}
Also used : Script(org.apache.commons.jexl3.internal.Script) Test(org.junit.Test)

Example 5 with JexlOptions

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

the class Engine method evalOptions.

/**
 * Obsolete version of options evaluation.
 * @param opts the obsolete instance of options
 * @return the newer class of options
 */
private JexlOptions evalOptions(final JexlEngine.Options opts) {
    // This condition and block for compatibility between 3.1 and 3.2
    final JexlOptions jexlo = options.copy();
    final JexlEngine jexl = this;
    jexlo.setCancellable(option(opts.isCancellable(), jexl.isCancellable()));
    jexlo.setSilent(option(opts.isSilent(), jexl.isSilent()));
    jexlo.setStrict(option(opts.isStrict(), jexl.isStrict()));
    final JexlArithmetic jexla = jexl.getArithmetic();
    jexlo.setStrictArithmetic(option(opts.isStrictArithmetic(), jexla.isStrict()));
    jexlo.setMathContext(opts.getArithmeticMathContext());
    jexlo.setMathScale(opts.getArithmeticMathScale());
    return jexlo;
}
Also used : JexlEngine(org.apache.commons.jexl3.JexlEngine) JexlOptions(org.apache.commons.jexl3.JexlOptions) JexlArithmetic(org.apache.commons.jexl3.JexlArithmetic)

Aggregations

JexlOptions (org.apache.commons.jexl3.JexlOptions)4 Test (org.junit.Test)3 BigDecimal (java.math.BigDecimal)2 JxltEngine (org.apache.commons.jexl3.JxltEngine)2 Engine (org.apache.commons.jexl3.internal.Engine)2 JexlArithmetic (org.apache.commons.jexl3.JexlArithmetic)1 JexlContext (org.apache.commons.jexl3.JexlContext)1 JexlEngine (org.apache.commons.jexl3.JexlEngine)1 JexlException (org.apache.commons.jexl3.JexlException)1 Script (org.apache.commons.jexl3.internal.Script)1 TemplateExpression (org.apache.commons.jexl3.internal.TemplateEngine.TemplateExpression)1 ASTJexlLambda (org.apache.commons.jexl3.parser.ASTJexlLambda)1 JexlNode (org.apache.commons.jexl3.parser.JexlNode)1