From 0ac9d7c6165bc7791ae3fdf72624b94939ac8391 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 6 Dec 2015 19:33:12 +0000 Subject: [PATCH] + Support --def name=@/path/to/text/file parameter passing syntax that allows parameters to be read from a text file (i.e. for complex formats that would otherwise require escaping) --- source/net/filebot/cli/ArgumentProcessor.java | 4 +- source/net/filebot/cli/BindingsHandler.java | 45 ++++++++++++++----- .../filebot/subtitle/SubtitleUtilities.java | 3 +- source/net/filebot/util/FileUtilities.java | 20 +++++---- source/net/filebot/util/UnicodeReader.java | 16 ++++--- 5 files changed, 58 insertions(+), 30 deletions(-) diff --git a/source/net/filebot/cli/ArgumentProcessor.java b/source/net/filebot/cli/ArgumentProcessor.java index b9f09eee..bb255edf 100644 --- a/source/net/filebot/cli/ArgumentProcessor.java +++ b/source/net/filebot/cli/ArgumentProcessor.java @@ -6,10 +6,8 @@ import static net.filebot.util.ExceptionUtilities.*; import static net.filebot.util.FileUtilities.*; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.InputStreamReader; import java.net.URI; import java.net.URL; import java.nio.ByteBuffer; @@ -188,7 +186,7 @@ public class ArgumentProcessor { @Override public String fetchScript(URI uri) throws IOException { if (uri.getScheme().equals("file")) { - return readAll(new InputStreamReader(new FileInputStream(new File(uri)), "UTF-8")); + return readTextFile(new File(uri)); } if (uri.getScheme().equals("g")) { diff --git a/source/net/filebot/cli/BindingsHandler.java b/source/net/filebot/cli/BindingsHandler.java index 9cc72a31..cd665ebf 100644 --- a/source/net/filebot/cli/BindingsHandler.java +++ b/source/net/filebot/cli/BindingsHandler.java @@ -1,5 +1,9 @@ package net.filebot.cli; +import static net.filebot.util.FileUtilities.*; + +import java.io.File; +import java.io.IOException; import java.util.LinkedHashMap; import java.util.Map; @@ -33,29 +37,48 @@ public class BindingsHandler extends MapOptionHandler { int pos = 0; while (pos < params.size()) { - String[] nv = params.getParameter(pos).split("=", 2); - - if (nv.length < 2 || nv[0].startsWith("-")) { + if (params.getParameter(pos).startsWith("-")) { return pos; } - String n = nv[0].trim(); - String v = nv[1].trim(); - - if (!isIdentifier(n)) { - throw new CmdLineException(owner, String.format("\"%s\" is not a valid identifier", n)); + String[] nv = params.getParameter(pos).split("=", 2); + if (nv.length < 2) { + return pos; } - map.put(n, v); + String n = getIdentifier(nv[0].trim()); + String v = getValue(nv[1].trim()); + + addToMap(map, n, v); pos++; } return pos; } + public String getIdentifier(String n) throws CmdLineException { + if (!isIdentifier(n)) { + throw new CmdLineException(owner, "\"" + n + "\" is not a valid identifier", null); + } + return n; + } + + public String getValue(String v) throws CmdLineException { + if (v.startsWith("@")) { + File f = new File(v.substring(1)); + try { + return readTextFile(f).trim(); + } catch (IOException e) { + throw new CmdLineException(owner, "\"" + f + "\" is not a text file", e); + } + } + return v; + } + public boolean isIdentifier(String n) { - if (n.isEmpty()) + if (n == null || n.isEmpty()) { return false; + } for (int i = 0; i < n.length();) { int c = n.codePointAt(i); @@ -76,7 +99,7 @@ public class BindingsHandler extends MapOptionHandler { @Override protected Map createNewCollection(Class type) { - return new LinkedHashMap(); + return new LinkedHashMap(); // make sure to preserve order of arguments } } diff --git a/source/net/filebot/subtitle/SubtitleUtilities.java b/source/net/filebot/subtitle/SubtitleUtilities.java index 8425c1ec..b208b02e 100644 --- a/source/net/filebot/subtitle/SubtitleUtilities.java +++ b/source/net/filebot/subtitle/SubtitleUtilities.java @@ -13,6 +13,7 @@ import java.io.Reader; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -271,7 +272,7 @@ public final class SubtitleUtilities { } // decode bytes and beware of byte-order marks - Reader reader = new UnicodeReader(new ByteBufferInputStream(file.getData())); + Reader reader = new UnicodeReader(new ByteBufferInputStream(file.getData()), true, StandardCharsets.UTF_8); // decode subtitle file with the first reader that seems to work for (SubtitleFormat format : likelyFormats) { diff --git a/source/net/filebot/util/FileUtilities.java b/source/net/filebot/util/FileUtilities.java index 3321c749..14604018 100644 --- a/source/net/filebot/util/FileUtilities.java +++ b/source/net/filebot/util/FileUtilities.java @@ -198,16 +198,18 @@ public final class FileUtilities { } } - public static String readAll(Reader source) throws IOException { - StringBuilder text = new StringBuilder(); - char[] buffer = new char[2048]; + public static String readTextFile(File file) throws IOException { + try (Reader reader = new UnicodeReader(new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE), false, StandardCharsets.UTF_8)) { + StringBuilder text = new StringBuilder(); + char[] buffer = new char[BUFFER_SIZE]; - int read = 0; - while ((read = source.read(buffer)) >= 0) { - text.append(buffer, 0, read); + int read = 0; + while ((read = reader.read(buffer)) >= 0) { + text.append(buffer, 0, read); + } + + return text.toString(); } - - return text.toString(); } public static File writeFile(ByteBuffer data, File destination) throws IOException { @@ -239,7 +241,7 @@ public final class FileUtilities { return charset.getReader(); // assume UTF-8 by default - return new InputStreamReader(new FileInputStream(file), "UTF-8"); + return new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8); } public static String getText(ByteBuffer data) throws IOException { diff --git a/source/net/filebot/util/UnicodeReader.java b/source/net/filebot/util/UnicodeReader.java index efce7a07..213e6c7d 100644 --- a/source/net/filebot/util/UnicodeReader.java +++ b/source/net/filebot/util/UnicodeReader.java @@ -16,9 +16,10 @@ public class UnicodeReader extends Reader { private final Reader reader; - public UnicodeReader(InputStream stream) throws IOException { - if (!stream.markSupported()) + public UnicodeReader(InputStream stream, boolean guessCharset, Charset defaultCharset) throws IOException { + if (!stream.markSupported()) { throw new IllegalArgumentException("stream must support mark"); + } stream.mark(BOM_SIZE); byte bom[] = new byte[BOM_SIZE]; @@ -49,12 +50,15 @@ public class UnicodeReader extends Reader { stream.skip(skip); // guess character encoding if necessary - if (bomEncoding == null) { - // auto-detect encoding - reader = new CharsetDetector().getReader(stream, "UTF-8"); - } else { + if (bomEncoding != null) { // initialize reader via BOM reader = new InputStreamReader(stream, bomEncoding); + } else if (bomEncoding == null && guessCharset) { + // auto-detect encoding + reader = new CharsetDetector().getReader(stream, defaultCharset.name()); + } else { + // use default + reader = new InputStreamReader(stream, defaultCharset); } }