From 0de615cd00ea018f2bce77e8ddff44e5fa38620e Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 24 Nov 2011 12:03:17 +0000 Subject: [PATCH] * make binding value cleanup (path separators / trim) also work in CLI --- .../filebot/format/ExpressionFormat.java | 27 +++++++++++++++++-- .../ui/rename/ExpressionFormatter.java | 26 +----------------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/source/net/sourceforge/filebot/format/ExpressionFormat.java b/source/net/sourceforge/filebot/format/ExpressionFormat.java index 82841bc1..abbef917 100644 --- a/source/net/sourceforge/filebot/format/ExpressionFormat.java +++ b/source/net/sourceforge/filebot/format/ExpressionFormat.java @@ -3,6 +3,7 @@ package net.sourceforge.filebot.format; import static net.sourceforge.tuned.ExceptionUtilities.*; +import static net.sourceforge.tuned.FileUtilities.*; import groovy.lang.GroovyRuntimeException; import groovy.lang.MissingPropertyException; @@ -141,7 +142,13 @@ public class ExpressionFormat extends Format { 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) { if (snipped instanceof CompiledScript) { try { - Object value = ((CompiledScript) snipped).eval(context); + Object value = normalizeExpressionValue(((CompiledScript) snipped).eval(context)); if (value != null) { 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) { if (findCause(exception, MissingPropertyException.class) != null) { lastException = new ExpressionException(new BindingException(findCause(exception, MissingPropertyException.class).getProperty(), "undefined", exception)); diff --git a/source/net/sourceforge/filebot/ui/rename/ExpressionFormatter.java b/source/net/sourceforge/filebot/ui/rename/ExpressionFormatter.java index ae05c7ba..9b4f08f7 100644 --- a/source/net/sourceforge/filebot/ui/rename/ExpressionFormatter.java +++ b/source/net/sourceforge/filebot/ui/rename/ExpressionFormatter.java @@ -2,15 +2,11 @@ package net.sourceforge.filebot.ui.rename; -import static net.sourceforge.tuned.FileUtilities.*; - import java.io.File; import java.text.Format; -import javax.script.Bindings; import javax.script.ScriptException; -import net.sourceforge.filebot.format.ExpressionBindings; import net.sourceforge.filebot.format.ExpressionFormat; import net.sourceforge.filebot.format.MediaBindingBean; import net.sourceforge.filebot.similarity.Match; @@ -53,27 +49,7 @@ class ExpressionFormatter implements MatchFormatter { public synchronized String format(Match match) throws ScriptException { // lazy initialize script engine if (format == null) { - 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; - } - }; - } - }; + format = new ExpressionFormat(expression); } // evaluate the expression using the given bindings