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:
parent
23ff6048a3
commit
f6199e154f
@ -43,6 +43,7 @@ import net.filebot.format.ExpressionFileFilter;
|
|||||||
import net.filebot.format.ExpressionFileFormat;
|
import net.filebot.format.ExpressionFileFormat;
|
||||||
import net.filebot.format.ExpressionFilter;
|
import net.filebot.format.ExpressionFilter;
|
||||||
import net.filebot.format.ExpressionFormat;
|
import net.filebot.format.ExpressionFormat;
|
||||||
|
import net.filebot.format.ExpressionMapper;
|
||||||
import net.filebot.hash.HashType;
|
import net.filebot.hash.HashType;
|
||||||
import net.filebot.subtitle.SubtitleFormat;
|
import net.filebot.subtitle.SubtitleFormat;
|
||||||
import net.filebot.subtitle.SubtitleNaming;
|
import net.filebot.subtitle.SubtitleNaming;
|
||||||
@ -71,9 +72,12 @@ public class ArgumentBean {
|
|||||||
@Option(name = "--conflict", usage = "Conflict resolution", metaVar = "[skip, override, auto, index, fail]")
|
@Option(name = "--conflict", usage = "Conflict resolution", metaVar = "[skip, override, auto, index, fail]")
|
||||||
public String conflict = "skip";
|
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;
|
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)
|
@Option(name = "--format", usage = "Format expression", handler = GroovyExpressionHandler.class)
|
||||||
public String format;
|
public String format;
|
||||||
|
|
||||||
@ -266,6 +270,10 @@ public class ArgumentBean {
|
|||||||
return filter == null ? FILES : new ExpressionFileFilter(filter, xattr::getMetaInfo);
|
return filter == null ? FILES : new ExpressionFileFilter(filter, xattr::getMetaInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ExpressionMapper getExpressionMapper() throws Exception {
|
||||||
|
return mapper == null ? null : new ExpressionMapper(mapper);
|
||||||
|
}
|
||||||
|
|
||||||
public Datasource getDatasource() {
|
public Datasource getDatasource() {
|
||||||
return db == null ? null : WebServices.getService(db);
|
return db == null ? null : WebServices.getService(db);
|
||||||
}
|
}
|
||||||
|
48
source/net/filebot/format/ExpressionMapper.java
Normal file
48
source/net/filebot/format/ExpressionMapper.java
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user