From 95f84fb6face571cd39260931e0fa9a5f91ee077 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 9 Aug 2014 07:24:01 +0000 Subject: [PATCH] * make {model} binding more universal and easy-to-use --- .../filebot/format/ExpressionBindings.java | 30 ++++++++--------- .../net/filebot/format/MediaBindingBean.java | 32 +++++++++++++------ 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/source/net/filebot/format/ExpressionBindings.java b/source/net/filebot/format/ExpressionBindings.java index a9f90b5b..8ec547ec 100644 --- a/source/net/filebot/format/ExpressionBindings.java +++ b/source/net/filebot/format/ExpressionBindings.java @@ -1,5 +1,7 @@ package net.filebot.format; +import static net.filebot.util.ExceptionUtilities.*; + import java.lang.reflect.Method; import java.util.AbstractMap; import java.util.HashSet; @@ -9,8 +11,6 @@ import java.util.TreeMap; import javax.script.Bindings; -import net.filebot.util.ExceptionUtilities; - public class ExpressionBindings extends AbstractMap implements Bindings { protected final Object bindingBean; @@ -51,29 +51,27 @@ public class ExpressionBindings extends AbstractMap implements B return bindingBean; } - protected Object evaluate(final Method method) throws Exception { - Object value = method.invoke(bindingBean); - - if (!isUndefined(value)) { - return value; - } - - // invoke fallback method - return undefined.invoke(bindingBean); - } - @Override public Object get(Object key) { Method method = bindings.get(key); if (method != null) { try { - return evaluate(method); + Object value = method.invoke(bindingBean); + if (!isUndefined(value)) { + return value; + } + if (undefined != null) { + return undefined.invoke(bindingBean, key); // invoke fallback method + } } catch (Exception e) { - throw new BindingException(key.toString(), ExceptionUtilities.getRootCauseMessage(e), e); + // check InvocationTargetException cause + if (e.getCause() instanceof BindingException) { + throw (BindingException) e.getCause(); + } + throw new BindingException(key.toString(), getRootCauseMessage(e), e); } } - return null; } diff --git a/source/net/filebot/format/MediaBindingBean.java b/source/net/filebot/format/MediaBindingBean.java index 5363f13c..c48d14a9 100644 --- a/source/net/filebot/format/MediaBindingBean.java +++ b/source/net/filebot/format/MediaBindingBean.java @@ -71,9 +71,9 @@ public class MediaBindingBean { } @Define(undefined) - public T undefined() { + public T undefined(String name) { // omit expressions that depend on undefined values - throw new RuntimeException("undefined"); + throw new BindingException(name, "undefined"); } @Define("n") @@ -762,13 +762,13 @@ public class MediaBindingBean { @Define("i") public Integer getModelIndex() { - return identityIndexOf(getContext().values(), getInfoObject()); + return identityIndexOf(context.values(), getInfoObject()); } @Define("di") public Integer getDuplicateIndex() { List duplicates = new ArrayList(); - for (Object it : getContext().values()) { + for (Object it : context.values()) { if (getInfoObject().equals(it)) { duplicates.add(it); } @@ -778,8 +778,18 @@ public class MediaBindingBean { } @Define("model") - public Map getContext() { - return context; + public List getContext() { + List result = new ArrayList(); + for (Entry it : context.entrySet()) { + MediaBindingBean mediaBindingBean = new MediaBindingBean(it.getValue(), it.getKey(), context) { + @Define(undefined) + public T undefined(String name) { + return null; // never throw exceptions for empty or null values + } + }; + result.add(new AssociativeScriptObject(new ExpressionBindings(mediaBindingBean))); + } + return result; } @Define("json") @@ -799,8 +809,8 @@ public class MediaBindingBean { } } else if (SUBTITLE_FILES.accept(mediaFile) || ((infoObject instanceof Episode || infoObject instanceof Movie) && !VIDEO_FILES.accept(mediaFile))) { // prefer equal match from current context if possible - if (getContext() != null) { - for (Entry it : getContext().entrySet()) { + if (context != null) { + for (Entry it : context.entrySet()) { if (infoObject.equals(it.getValue()) && VIDEO_FILES.accept(it.getKey())) { return it.getKey(); } @@ -888,7 +898,7 @@ public class MediaBindingBean { Object value = super.getProperty(name); if (value == null) { - throw new BindingException(name, "undefined"); + undefined(name); } // auto-clean value of path separators @@ -965,4 +975,8 @@ public class MediaBindingBean { return s.toString().trim(); } + @Override + public String toString() { + return String.format("%s ⇔ %s", infoObject, mediaFile == null ? null : mediaFile.getName()); + } }