2009-04-26 09:34:22 -04:00
|
|
|
|
2014-04-19 02:30:29 -04:00
|
|
|
package net.filebot.ui.rename;
|
2009-04-26 09:34:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
2011-09-18 15:08:03 -04:00
|
|
|
import java.text.Format;
|
2013-01-27 03:17:12 -05:00
|
|
|
import java.util.Map;
|
2009-04-26 09:34:22 -04:00
|
|
|
|
|
|
|
import javax.script.ScriptException;
|
|
|
|
|
2014-04-19 02:30:29 -04:00
|
|
|
import net.filebot.format.ExpressionFormat;
|
|
|
|
import net.filebot.format.MediaBindingBean;
|
|
|
|
import net.filebot.similarity.Match;
|
2009-04-26 09:34:22 -04:00
|
|
|
|
|
|
|
|
2011-09-18 15:08:03 -04:00
|
|
|
class ExpressionFormatter implements MatchFormatter {
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2009-08-25 11:41:08 -04:00
|
|
|
private final String expression;
|
|
|
|
private ExpressionFormat format;
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2011-09-18 15:08:03 -04:00
|
|
|
private Format preview;
|
|
|
|
private Class<?> target;
|
2015-07-25 18:47:19 -04:00
|
|
|
|
|
|
|
|
2011-09-18 15:08:03 -04:00
|
|
|
public ExpressionFormatter(String expression, Format preview, Class<?> target) {
|
2009-08-25 11:41:08 -04:00
|
|
|
if (expression == null || expression.isEmpty())
|
|
|
|
throw new IllegalArgumentException("Expression must not be null or empty");
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2009-08-25 11:41:08 -04:00
|
|
|
this.expression = expression;
|
2011-09-18 15:08:03 -04:00
|
|
|
this.preview = preview;
|
|
|
|
this.target = target;
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2009-04-26 09:34:22 -04:00
|
|
|
}
|
2015-07-25 18:47:19 -04:00
|
|
|
|
|
|
|
|
2009-04-26 09:34:22 -04:00
|
|
|
@Override
|
|
|
|
public boolean canFormat(Match<?, ?> match) {
|
2011-09-18 15:08:03 -04:00
|
|
|
// target object is required, file is optional
|
|
|
|
return target.isInstance(match.getValue()) && (match.getCandidate() == null || match.getCandidate() instanceof File);
|
2009-04-26 09:34:22 -04:00
|
|
|
}
|
2015-07-25 18:47:19 -04:00
|
|
|
|
|
|
|
|
2009-04-26 09:34:22 -04:00
|
|
|
@Override
|
|
|
|
public String preview(Match<?, ?> match) {
|
2011-09-18 15:08:03 -04:00
|
|
|
return preview != null ? preview.format(match.getValue()) : match.getValue().toString();
|
2009-04-26 09:34:22 -04:00
|
|
|
}
|
2015-07-25 18:47:19 -04:00
|
|
|
|
|
|
|
|
2009-04-26 09:34:22 -04:00
|
|
|
@Override
|
2013-01-27 03:17:12 -05:00
|
|
|
public synchronized String format(Match<?, ?> match, Map<?, ?> context) throws ScriptException {
|
2009-08-25 11:41:08 -04:00
|
|
|
// lazy initialize script engine
|
|
|
|
if (format == null) {
|
2011-11-24 07:03:17 -05:00
|
|
|
format = new ExpressionFormat(expression);
|
2009-08-25 11:41:08 -04:00
|
|
|
}
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2010-02-04 11:22:45 -05:00
|
|
|
// evaluate the expression using the given bindings
|
2013-01-27 03:17:12 -05:00
|
|
|
Object bindingBean = new MediaBindingBean(match.getValue(), (File) match.getCandidate(), (Map<File, Object>) context);
|
2011-09-18 15:08:03 -04:00
|
|
|
String result = format.format(bindingBean).trim();
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2009-07-18 18:06:32 -04:00
|
|
|
// if result is empty, check for script exceptions
|
2009-07-23 10:25:43 -04:00
|
|
|
if (result.isEmpty() && format.caughtScriptException() != null) {
|
2009-07-18 18:06:32 -04:00
|
|
|
throw format.caughtScriptException();
|
2009-07-23 10:25:43 -04:00
|
|
|
}
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2009-07-18 18:06:32 -04:00
|
|
|
return result;
|
2009-04-26 09:34:22 -04:00
|
|
|
}
|
2015-07-25 18:47:19 -04:00
|
|
|
|
2009-04-26 09:34:22 -04:00
|
|
|
}
|