1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-24 08:48:51 -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 static net.filebot.util.FileUtilities.*;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@ -188,7 +186,7 @@ public class ArgumentProcessor {
@Override @Override
public String fetchScript(URI uri) throws IOException { public String fetchScript(URI uri) throws IOException {
if (uri.getScheme().equals("file")) { 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")) { if (uri.getScheme().equals("g")) {

View File

@ -1,5 +1,9 @@
package net.filebot.cli; 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.LinkedHashMap;
import java.util.Map; import java.util.Map;
@ -33,29 +37,48 @@ public class BindingsHandler extends MapOptionHandler {
int pos = 0; int pos = 0;
while (pos < params.size()) { while (pos < params.size()) {
String[] nv = params.getParameter(pos).split("=", 2); if (params.getParameter(pos).startsWith("-")) {
if (nv.length < 2 || nv[0].startsWith("-")) {
return pos; return pos;
} }
String n = nv[0].trim(); String[] nv = params.getParameter(pos).split("=", 2);
String v = nv[1].trim(); if (nv.length < 2) {
return pos;
if (!isIdentifier(n)) {
throw new CmdLineException(owner, String.format("\"%s\" is not a valid identifier", n));
} }
map.put(n, v); String n = getIdentifier(nv[0].trim());
String v = getValue(nv[1].trim());
addToMap(map, n, v);
pos++; pos++;
} }
return 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) { public boolean isIdentifier(String n) {
if (n.isEmpty()) if (n == null || n.isEmpty()) {
return false; return false;
}
for (int i = 0; i < n.length();) { for (int i = 0; i < n.length();) {
int c = n.codePointAt(i); int c = n.codePointAt(i);
@ -76,7 +99,7 @@ public class BindingsHandler extends MapOptionHandler {
@Override @Override
protected Map createNewCollection(Class<? extends Map> type) { 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.ByteBuffer;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
@ -271,7 +272,7 @@ public final class SubtitleUtilities {
} }
// decode bytes and beware of byte-order marks // 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 // decode subtitle file with the first reader that seems to work
for (SubtitleFormat format : likelyFormats) { for (SubtitleFormat format : likelyFormats) {

View File

@ -198,16 +198,18 @@ public final class FileUtilities {
} }
} }
public static String readAll(Reader source) throws IOException { public static String readTextFile(File file) throws IOException {
StringBuilder text = new StringBuilder(); try (Reader reader = new UnicodeReader(new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE), false, StandardCharsets.UTF_8)) {
char[] buffer = new char[2048]; StringBuilder text = new StringBuilder();
char[] buffer = new char[BUFFER_SIZE];
int read = 0; int read = 0;
while ((read = source.read(buffer)) >= 0) { while ((read = reader.read(buffer)) >= 0) {
text.append(buffer, 0, read); text.append(buffer, 0, read);
}
return text.toString();
} }
return text.toString();
} }
public static File writeFile(ByteBuffer data, File destination) throws IOException { public static File writeFile(ByteBuffer data, File destination) throws IOException {
@ -239,7 +241,7 @@ public final class FileUtilities {
return charset.getReader(); return charset.getReader();
// assume UTF-8 by default // 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 { public static String getText(ByteBuffer data) throws IOException {

View File

@ -16,9 +16,10 @@ public class UnicodeReader extends Reader {
private final Reader reader; private final Reader reader;
public UnicodeReader(InputStream stream) throws IOException { public UnicodeReader(InputStream stream, boolean guessCharset, Charset defaultCharset) throws IOException {
if (!stream.markSupported()) if (!stream.markSupported()) {
throw new IllegalArgumentException("stream must support mark"); throw new IllegalArgumentException("stream must support mark");
}
stream.mark(BOM_SIZE); stream.mark(BOM_SIZE);
byte bom[] = new byte[BOM_SIZE]; byte bom[] = new byte[BOM_SIZE];
@ -49,12 +50,15 @@ public class UnicodeReader extends Reader {
stream.skip(skip); stream.skip(skip);
// guess character encoding if necessary // guess character encoding if necessary
if (bomEncoding == null) { if (bomEncoding != null) {
// auto-detect encoding
reader = new CharsetDetector().getReader(stream, "UTF-8");
} else {
// initialize reader via BOM // initialize reader via BOM
reader = new InputStreamReader(stream, bomEncoding); 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);
} }
} }