* make {model} binding more universal and easy-to-use

This commit is contained in:
Reinhard Pointner 2014-08-09 07:24:01 +00:00
parent 162945c6e3
commit 95f84fb6fa
2 changed files with 37 additions and 25 deletions

View File

@ -1,5 +1,7 @@
package net.filebot.format; package net.filebot.format;
import static net.filebot.util.ExceptionUtilities.*;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.HashSet; import java.util.HashSet;
@ -9,8 +11,6 @@ import java.util.TreeMap;
import javax.script.Bindings; import javax.script.Bindings;
import net.filebot.util.ExceptionUtilities;
public class ExpressionBindings extends AbstractMap<String, Object> implements Bindings { public class ExpressionBindings extends AbstractMap<String, Object> implements Bindings {
protected final Object bindingBean; protected final Object bindingBean;
@ -51,29 +51,27 @@ public class ExpressionBindings extends AbstractMap<String, Object> implements B
return bindingBean; 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 @Override
public Object get(Object key) { public Object get(Object key) {
Method method = bindings.get(key); Method method = bindings.get(key);
if (method != null) { if (method != null) {
try { 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) { } 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; return null;
} }

View File

@ -71,9 +71,9 @@ public class MediaBindingBean {
} }
@Define(undefined) @Define(undefined)
public <T> T undefined() { public <T> T undefined(String name) {
// omit expressions that depend on undefined values // omit expressions that depend on undefined values
throw new RuntimeException("undefined"); throw new BindingException(name, "undefined");
} }
@Define("n") @Define("n")
@ -762,13 +762,13 @@ public class MediaBindingBean {
@Define("i") @Define("i")
public Integer getModelIndex() { public Integer getModelIndex() {
return identityIndexOf(getContext().values(), getInfoObject()); return identityIndexOf(context.values(), getInfoObject());
} }
@Define("di") @Define("di")
public Integer getDuplicateIndex() { public Integer getDuplicateIndex() {
List<Object> duplicates = new ArrayList<Object>(); List<Object> duplicates = new ArrayList<Object>();
for (Object it : getContext().values()) { for (Object it : context.values()) {
if (getInfoObject().equals(it)) { if (getInfoObject().equals(it)) {
duplicates.add(it); duplicates.add(it);
} }
@ -778,8 +778,18 @@ public class MediaBindingBean {
} }
@Define("model") @Define("model")
public Map<File, Object> getContext() { public List<AssociativeScriptObject> getContext() {
return context; List<AssociativeScriptObject> result = new ArrayList<AssociativeScriptObject>();
for (Entry<File, Object> it : context.entrySet()) {
MediaBindingBean mediaBindingBean = new MediaBindingBean(it.getValue(), it.getKey(), context) {
@Define(undefined)
public <T> 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") @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))) { } else if (SUBTITLE_FILES.accept(mediaFile) || ((infoObject instanceof Episode || infoObject instanceof Movie) && !VIDEO_FILES.accept(mediaFile))) {
// prefer equal match from current context if possible // prefer equal match from current context if possible
if (getContext() != null) { if (context != null) {
for (Entry<File, Object> it : getContext().entrySet()) { for (Entry<File, Object> it : context.entrySet()) {
if (infoObject.equals(it.getValue()) && VIDEO_FILES.accept(it.getKey())) { if (infoObject.equals(it.getValue()) && VIDEO_FILES.accept(it.getKey())) {
return it.getKey(); return it.getKey();
} }
@ -888,7 +898,7 @@ public class MediaBindingBean {
Object value = super.getProperty(name); Object value = super.getProperty(name);
if (value == null) { if (value == null) {
throw new BindingException(name, "undefined"); undefined(name);
} }
// auto-clean value of path separators // auto-clean value of path separators
@ -965,4 +975,8 @@ public class MediaBindingBean {
return s.toString().trim(); return s.toString().trim();
} }
@Override
public String toString() {
return String.format("%s ⇔ %s", infoObject, mediaFile == null ? null : mediaFile.getName());
}
} }