1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-22 07:48:52 -05:00

Add ExpressionMapper interface

This commit is contained in:
Reinhard Pointner 2019-05-27 14:36:38 +07:00
parent 23ff6048a3
commit f6199e154f
2 changed files with 57 additions and 1 deletions

View File

@ -43,6 +43,7 @@ import net.filebot.format.ExpressionFileFilter;
import net.filebot.format.ExpressionFileFormat;
import net.filebot.format.ExpressionFilter;
import net.filebot.format.ExpressionFormat;
import net.filebot.format.ExpressionMapper;
import net.filebot.hash.HashType;
import net.filebot.subtitle.SubtitleFormat;
import net.filebot.subtitle.SubtitleNaming;
@ -71,9 +72,12 @@ public class ArgumentBean {
@Option(name = "--conflict", usage = "Conflict resolution", metaVar = "[skip, override, auto, index, fail]")
public String conflict = "skip";
@Option(name = "--filter", usage = "Match filter expression", handler = GroovyExpressionHandler.class)
@Option(name = "--filter", usage = "Filter expression", handler = GroovyExpressionHandler.class)
public String filter = null;
@Option(name = "--xem", usage = "XEM expression", handler = GroovyExpressionHandler.class)
public String mapper = null;
@Option(name = "--format", usage = "Format expression", handler = GroovyExpressionHandler.class)
public String format;
@ -266,6 +270,10 @@ public class ArgumentBean {
return filter == null ? FILES : new ExpressionFileFilter(filter, xattr::getMetaInfo);
}
public ExpressionMapper getExpressionMapper() throws Exception {
return mapper == null ? null : new ExpressionMapper(mapper);
}
public Datasource getDatasource() {
return db == null ? null : WebServices.getService(db);
}

View File

@ -0,0 +1,48 @@
package net.filebot.format;
import static net.filebot.format.ExpressionFormat.*;
import java.security.AccessController;
import javax.script.Bindings;
import javax.script.CompiledScript;
import javax.script.ScriptContext;
import javax.script.ScriptException;
import javax.script.SimpleScriptContext;
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
public class ExpressionMapper {
private final String expression;
private final CompiledScript compiledExpression;
public ExpressionMapper(String expression) throws ScriptException {
this.expression = expression;
this.compiledExpression = new SecureCompiledScript(compileScriptlet(asExpression(expression)));
}
public String getExpression() {
return expression;
}
public <T> Object map(Object value, Class<T> type) throws ScriptException {
return map(new ExpressionBindings(value), type);
}
public <T> Object map(Bindings bindings, Class<T> type) throws ScriptException {
// 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);
// evaluate user script
Object value = compiledExpression.eval(context);
// value as target type
return DefaultTypeTransformation.castToType(value, type);
}
}