* make binding value cleanup (path separators / trim) also work in CLI

This commit is contained in:
Reinhard Pointner 2011-11-24 12:03:17 +00:00
parent 1039701549
commit 0de615cd00
2 changed files with 26 additions and 27 deletions

View File

@ -3,6 +3,7 @@ package net.sourceforge.filebot.format;
import static net.sourceforge.tuned.ExceptionUtilities.*; import static net.sourceforge.tuned.ExceptionUtilities.*;
import static net.sourceforge.tuned.FileUtilities.*;
import groovy.lang.GroovyRuntimeException; import groovy.lang.GroovyRuntimeException;
import groovy.lang.MissingPropertyException; import groovy.lang.MissingPropertyException;
@ -141,7 +142,13 @@ public class ExpressionFormat extends Format {
public Bindings getBindings(Object value) { public Bindings getBindings(Object value) {
return new ExpressionBindings(value); return new ExpressionBindings(value) {
@Override
public Object get(Object key) {
return normalizeBindingValue(super.get(key));
}
};
} }
@ -165,7 +172,7 @@ public class ExpressionFormat extends Format {
for (Object snipped : compilation) { for (Object snipped : compilation) {
if (snipped instanceof CompiledScript) { if (snipped instanceof CompiledScript) {
try { try {
Object value = ((CompiledScript) snipped).eval(context); Object value = normalizeExpressionValue(((CompiledScript) snipped).eval(context));
if (value != null) { if (value != null) {
sb.append(value); sb.append(value);
@ -182,6 +189,22 @@ public class ExpressionFormat extends Format {
} }
protected Object normalizeBindingValue(Object value) {
// if the binding value is a String, remove illegal characters
if (value instanceof CharSequence) {
return replacePathSeparators(value.toString()).trim();
}
// if the binding value is an Object, just leave it
return value;
}
protected Object normalizeExpressionValue(Object value) {
return value;
}
protected void handleException(ScriptException exception) { protected void handleException(ScriptException exception) {
if (findCause(exception, MissingPropertyException.class) != null) { if (findCause(exception, MissingPropertyException.class) != null) {
lastException = new ExpressionException(new BindingException(findCause(exception, MissingPropertyException.class).getProperty(), "undefined", exception)); lastException = new ExpressionException(new BindingException(findCause(exception, MissingPropertyException.class).getProperty(), "undefined", exception));

View File

@ -2,15 +2,11 @@
package net.sourceforge.filebot.ui.rename; package net.sourceforge.filebot.ui.rename;
import static net.sourceforge.tuned.FileUtilities.*;
import java.io.File; import java.io.File;
import java.text.Format; import java.text.Format;
import javax.script.Bindings;
import javax.script.ScriptException; import javax.script.ScriptException;
import net.sourceforge.filebot.format.ExpressionBindings;
import net.sourceforge.filebot.format.ExpressionFormat; import net.sourceforge.filebot.format.ExpressionFormat;
import net.sourceforge.filebot.format.MediaBindingBean; import net.sourceforge.filebot.format.MediaBindingBean;
import net.sourceforge.filebot.similarity.Match; import net.sourceforge.filebot.similarity.Match;
@ -53,27 +49,7 @@ class ExpressionFormatter implements MatchFormatter {
public synchronized String format(Match<?, ?> match) throws ScriptException { public synchronized String format(Match<?, ?> match) throws ScriptException {
// lazy initialize script engine // lazy initialize script engine
if (format == null) { if (format == null) {
format = new ExpressionFormat(expression) { format = new ExpressionFormat(expression);
@Override
public Bindings getBindings(Object value) {
return new ExpressionBindings(value) {
@Override
public Object get(Object key) {
Object value = super.get(key);
// if the binding value is a String, remove illegal characters
if (value instanceof CharSequence) {
return replacePathSeparators(value.toString()).trim();
}
// if the binding value is an Object, just leave it
return value;
}
};
}
};
} }
// evaluate the expression using the given bindings // evaluate the expression using the given bindings