filebot/source/net/filebot/format/ExpressionFormatFunctions.java

105 lines
2.7 KiB
Java
Raw Normal View History

2014-04-19 02:30:29 -04:00
package net.filebot.format;
2016-11-11 06:07:00 -05:00
import static java.util.Collections.*;
import static java.util.stream.Collectors.*;
2016-11-11 06:07:00 -05:00
import static net.filebot.Settings.*;
2016-04-02 05:07:10 -04:00
import static net.filebot.util.RegularExpressions.*;
import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
2016-04-02 05:07:10 -04:00
import java.util.regex.Pattern;
import java.util.stream.Stream;
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-11-11 06:07:00 -05:00
import net.filebot.ApplicationFolder;
import net.filebot.mac.MacAppUtilities;
import net.filebot.util.FileUtilities;
2016-03-27 09:52:59 -04:00
/**
* Global functions available in the {@link ExpressionFormat}
*/
public class ExpressionFormatFunctions {
/**
* General helpers and utilities
*/
public static Object call(Object object) {
if (object instanceof Closure<?>) {
try {
return ((Closure<?>) object).call();
} catch (Exception e) {
return null;
}
}
if (object instanceof CharSequence && object.toString().isEmpty()) {
return null;
}
return object;
}
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);
}
2014-04-15 10:29:13 -04:00
public static List<Object> allOf(Object c1, Object c2, Object... cN) {
return stream(c1, c2, cN).collect(toList());
}
public static String concat(Object c1, Object c2, Object... cN) {
return stream(c1, c2, cN).map(Objects::toString).collect(joining());
}
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
}
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(" "));
}
public static Map<String, String> csv(String path) throws IOException {
2016-08-04 08:29:33 -04:00
Pattern[] delimiter = { TAB, SEMICOLON };
Map<String, String> map = new LinkedHashMap<String, String>();
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) {
map.put(field[0].trim(), field[1].trim());
2014-12-16 21:19:29 -05:00
break;
}
}
}
return map;
}
public static List<String> readLines(String path) throws IOException {
2016-11-19 07:16:00 -05:00
return FileUtilities.readLines(getUserFile(path));
}
2016-08-10 05:53:52 -04:00
public static Object readXml(String path) throws Exception {
2016-11-19 07:16:00 -05:00
return new XmlSlurper().parse(getUserFile(path));
2016-11-11 06:07:00 -05:00
}
2016-11-19 07:16:00 -05:00
public static File getUserFile(String path) {
2016-11-11 06:07:00 -05:00
File f = new File(path);
if (!f.isAbsolute()) {
2016-11-25 10:59:26 -05:00
f = ApplicationFolder.UserHome.resolve(path);
2016-11-11 06:07:00 -05:00
}
if (isMacSandbox()) {
MacAppUtilities.askUnlockFolders(null, singleton(f));
}
return f;
2016-08-10 05:53:52 -04:00
}
}