* make {model} binding more universal and easy-to-use
This commit is contained in:
parent
162945c6e3
commit
95f84fb6fa
|
@ -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<String, Object> implements Bindings {
|
||||
|
||||
protected final Object bindingBean;
|
||||
|
@ -51,29 +51,27 @@ public class ExpressionBindings extends AbstractMap<String, Object> 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -71,9 +71,9 @@ public class MediaBindingBean {
|
|||
}
|
||||
|
||||
@Define(undefined)
|
||||
public <T> T undefined() {
|
||||
public <T> 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<Object> duplicates = new ArrayList<Object>();
|
||||
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<File, Object> getContext() {
|
||||
return context;
|
||||
public List<AssociativeScriptObject> getContext() {
|
||||
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")
|
||||
|
@ -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<File, Object> it : getContext().entrySet()) {
|
||||
if (context != null) {
|
||||
for (Entry<File, Object> 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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue