1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-24 00:38:52 -05:00

+ 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)

This commit is contained in:
Reinhard Pointner 2015-12-06 19:33:12 +00:00
parent 2616b095b5
commit 0ac9d7c616
5 changed files with 58 additions and 30 deletions

View File

@ -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")) {

View File

@ -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<? extends Map> type) {
return new LinkedHashMap();
return new LinkedHashMap(); // make sure to preserve order of arguments
}
}

View File

@ -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) {

View File

@ -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 {

View File

@ -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);
}
}