From 03ce43e6f9d03ce4333cb597d54a186d3409b038 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 17 Apr 2014 06:14:38 +0000 Subject: [PATCH] * use proper Groovy DefaultTypeTransformation instead of hacky "value as boolean" script --- .../filebot/format/ExpressionFilter.java | 55 +++++++------------ 1 file changed, 20 insertions(+), 35 deletions(-) diff --git a/source/net/sourceforge/filebot/format/ExpressionFilter.java b/source/net/sourceforge/filebot/format/ExpressionFilter.java index 58b4215f..1af07afd 100644 --- a/source/net/sourceforge/filebot/format/ExpressionFilter.java +++ b/source/net/sourceforge/filebot/format/ExpressionFilter.java @@ -1,7 +1,5 @@ - package net.sourceforge.filebot.format; - import java.security.AccessController; import javax.script.Bindings; @@ -9,71 +7,58 @@ import javax.script.Compilable; import javax.script.CompiledScript; import javax.script.ScriptContext; import javax.script.ScriptException; -import javax.script.SimpleBindings; import javax.script.SimpleScriptContext; +import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation; public class ExpressionFilter { - + private final String expression; - - private final CompiledScript userScript; - private final CompiledScript asBooleanScript; - + private final CompiledScript compiledExpression; + private Throwable lastException; - - + public ExpressionFilter(String expression) throws ScriptException { this.expression = expression; - + Compilable engine = (Compilable) ExpressionFormat.getGroovyScriptEngine(); - this.userScript = new SecureCompiledScript(engine.compile(expression)); // sandboxed script - this.asBooleanScript = engine.compile("value as Boolean"); + this.compiledExpression = new SecureCompiledScript(engine.compile(expression)); // sandboxed script } - - + public String getExpression() { return expression; } - - + public Throwable getLastException() { return lastException; } - - + public boolean matches(Object value) throws ScriptException { return matches(new ExpressionBindings(value)); } - - + public boolean matches(Bindings bindings) throws ScriptException { this.lastException = null; - + // use privileged bindings so we are not restricted by the script sandbox Bindings priviledgedBindings = PrivilegedInvocation.newProxy(Bindings.class, bindings, AccessController.getContext()); - + // initialize script context with the privileged bindings ScriptContext context = new SimpleScriptContext(); context.setBindings(priviledgedBindings, ScriptContext.GLOBAL_SCOPE); - + try { // evaluate user script - Object value = userScript.eval(context); - - // convert value to boolean - Bindings valueBinding = new SimpleBindings(); - valueBinding.put("value", value); - Object result = asBooleanScript.eval(valueBinding); - if (result instanceof Boolean) { - return (Boolean) result; - } + Object value = compiledExpression.eval(context); + + // value as boolean + return DefaultTypeTransformation.castToBoolean(value); } catch (Throwable e) { // ignore any and all scripting exceptions this.lastException = e; } - + return false; } - + }