2014-04-19 02:30:29 -04:00
|
|
|
package net.filebot.format;
|
2014-04-15 08:23:58 -04:00
|
|
|
|
2016-03-28 06:13:55 -04:00
|
|
|
import static java.util.stream.Collectors.*;
|
2016-04-02 05:07:10 -04:00
|
|
|
import static net.filebot.util.RegularExpressions.*;
|
2016-03-28 06:13:55 -04:00
|
|
|
|
2016-04-03 14:14:27 -04:00
|
|
|
import java.io.File;
|
2014-04-18 15:41:39 -04:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.LinkedHashMap;
|
2014-04-15 08:23:58 -04:00
|
|
|
import java.util.List;
|
2014-04-18 15:41:39 -04:00
|
|
|
import java.util.Map;
|
2016-03-28 06:13:55 -04:00
|
|
|
import java.util.Objects;
|
2016-04-02 05:07:10 -04:00
|
|
|
import java.util.regex.Pattern;
|
2016-03-28 06:13:55 -04:00
|
|
|
import java.util.stream.Stream;
|
2014-04-15 08:23:58 -04:00
|
|
|
|
2016-03-27 09:52:59 -04:00
|
|
|
import groovy.lang.Closure;
|
2016-08-10 05:53:52 -04:00
|
|
|
import groovy.util.XmlSlurper;
|
2016-08-23 15:27:27 -04:00
|
|
|
import net.filebot.util.FileUtilities;
|
2016-03-27 09:52:59 -04:00
|
|
|
|
2014-04-15 08:23:58 -04:00
|
|
|
/**
|
|
|
|
* Global functions available in the {@link ExpressionFormat}
|
|
|
|
*/
|
|
|
|
public class ExpressionFormatFunctions {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* General helpers and utilities
|
|
|
|
*/
|
2016-03-28 06:13:55 -04:00
|
|
|
public static Object call(Object object) {
|
|
|
|
if (object instanceof Closure<?>) {
|
2014-04-15 08:23:58 -04:00
|
|
|
try {
|
2016-03-28 06:13:55 -04:00
|
|
|
return ((Closure<?>) object).call();
|
2014-04-15 08:23:58 -04:00
|
|
|
} catch (Exception e) {
|
2016-03-28 06:13:55 -04:00
|
|
|
return null;
|
2014-04-15 08:23:58 -04:00
|
|
|
}
|
|
|
|
}
|
2016-04-07 12:43:40 -04:00
|
|
|
if (object instanceof CharSequence && object.toString().isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-03-28 06:13:55 -04:00
|
|
|
return object;
|
2014-04-15 08:23:58 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 06:13:55 -04:00
|
|
|
public static Object any(Object c1, Object c2, Object... cN) {
|
2016-03-31 15:58:24 -04:00
|
|
|
return stream(c1, c2, cN).findFirst().orElse(null);
|
2016-03-28 06:13:55 -04:00
|
|
|
}
|
2014-04-15 10:29:13 -04:00
|
|
|
|
2016-03-28 06:13:55 -04:00
|
|
|
public static List<Object> allOf(Object c1, Object c2, Object... cN) {
|
|
|
|
return stream(c1, c2, cN).collect(toList());
|
|
|
|
}
|
2014-04-15 08:23:58 -04:00
|
|
|
|
2016-03-28 06:13:55 -04:00
|
|
|
public static String concat(Object c1, Object c2, Object... cN) {
|
|
|
|
return stream(c1, c2, cN).map(Objects::toString).collect(joining());
|
2014-04-15 08:23:58 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 06:13:55 -04:00
|
|
|
protected static Stream<Object> stream(Object c1, Object c2, Object... cN) {
|
|
|
|
return Stream.concat(Stream.of(c1, c2), Stream.of(cN)).map(ExpressionFormatFunctions::call).filter(Objects::nonNull);
|
2014-04-15 10:29:13 -04:00
|
|
|
}
|
|
|
|
|
2016-10-13 14:30:21 -04:00
|
|
|
public static String quote(Object c1, Object... cN) {
|
|
|
|
String q = String.valueOf('"');
|
|
|
|
String r = "\\" + q;
|
|
|
|
return stream(c1, null, cN).map(Objects::toString).map(s -> q + s.replace(q, r) + q).collect(joining(" "));
|
|
|
|
}
|
|
|
|
|
2014-07-05 09:11:57 -04:00
|
|
|
public static Map<String, String> csv(String path) throws IOException {
|
2016-08-04 08:29:33 -04:00
|
|
|
Pattern[] delimiter = { TAB, SEMICOLON };
|
2014-04-18 15:41:39 -04:00
|
|
|
Map<String, String> map = new LinkedHashMap<String, String>();
|
2016-08-23 15:27:27 -04:00
|
|
|
for (String line : readLines(path)) {
|
2016-08-04 08:29:33 -04:00
|
|
|
for (Pattern d : delimiter) {
|
|
|
|
String[] field = d.split(line, 2);
|
2014-12-16 21:19:29 -05:00
|
|
|
if (field.length >= 2) {
|
2016-04-03 14:14:27 -04:00
|
|
|
map.put(field[0].trim(), field[1].trim());
|
2014-12-16 21:19:29 -05:00
|
|
|
break;
|
|
|
|
}
|
2014-07-05 09:11:57 -04:00
|
|
|
}
|
2016-08-23 15:27:27 -04:00
|
|
|
}
|
2014-04-18 15:41:39 -04:00
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2015-10-09 04:01:21 -04:00
|
|
|
public static List<String> readLines(String path) throws IOException {
|
2016-08-23 15:27:27 -04:00
|
|
|
return FileUtilities.readLines(new File(path));
|
2015-10-09 04:01:21 -04:00
|
|
|
}
|
|
|
|
|
2016-08-10 05:53:52 -04:00
|
|
|
public static Object readXml(String path) throws Exception {
|
|
|
|
return new XmlSlurper().parse(new File(path));
|
|
|
|
}
|
|
|
|
|
2014-04-15 08:23:58 -04:00
|
|
|
}
|