* fixed critical bug in SubStationAlphaReader
* remove ass tags
This commit is contained in:
parent
a2afdbaa2c
commit
ab18c0f61d
|
@ -2,13 +2,10 @@
|
||||||
package net.sourceforge.filebot.subtitle;
|
package net.sourceforge.filebot.subtitle;
|
||||||
|
|
||||||
|
|
||||||
import static net.sourceforge.tuned.StringUtilities.*;
|
|
||||||
|
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.EnumMap;
|
|
||||||
import java.util.InputMismatchException;
|
import java.util.InputMismatchException;
|
||||||
import java.util.Map;
|
import java.util.List;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,8 +13,13 @@ public class SubStationAlphaReader extends SubtitleReader {
|
||||||
|
|
||||||
private final DateFormat timeFormat = new SubtitleTimeFormat();
|
private final DateFormat timeFormat = new SubtitleTimeFormat();
|
||||||
private final Pattern newline = Pattern.compile(Pattern.quote("\\n"), Pattern.CASE_INSENSITIVE);
|
private final Pattern newline = Pattern.compile(Pattern.quote("\\n"), Pattern.CASE_INSENSITIVE);
|
||||||
|
private final Pattern tag = Pattern.compile("[{]\\\\[^}]+[}]");
|
||||||
|
private final Pattern separator = Pattern.compile("\\s*,\\s*");
|
||||||
|
|
||||||
private Map<EventProperty, Integer> format;
|
private String[] format;
|
||||||
|
private int formatIndexStart;
|
||||||
|
private int formatIndexEnd;
|
||||||
|
private int formatIndexText;
|
||||||
|
|
||||||
|
|
||||||
public SubStationAlphaReader(Readable source) {
|
public SubStationAlphaReader(Readable source) {
|
||||||
|
@ -33,18 +35,13 @@ public class SubStationAlphaReader extends SubtitleReader {
|
||||||
if (!event[0].equals("Format"))
|
if (!event[0].equals("Format"))
|
||||||
throw new InputMismatchException("Illegal format header: " + Arrays.toString(event));
|
throw new InputMismatchException("Illegal format header: " + Arrays.toString(event));
|
||||||
|
|
||||||
String[] columns = event[1].split(",");
|
// read columns
|
||||||
|
format = separator.split(event[1]);
|
||||||
|
|
||||||
// map column name to column index
|
List<String> lookup = Arrays.asList(format);
|
||||||
format = new EnumMap<EventProperty, Integer>(EventProperty.class);
|
formatIndexStart = lookup.indexOf("Start");
|
||||||
|
formatIndexEnd = lookup.indexOf("End");
|
||||||
for (int i = 0; i < columns.length; i++) {
|
formatIndexText = lookup.indexOf("Text");
|
||||||
try {
|
|
||||||
format.put(EventProperty.valueOf(columns[i].trim()), i);
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -74,23 +71,22 @@ public class SubStationAlphaReader extends SubtitleReader {
|
||||||
throw new InputMismatchException("Illegal dialogue event: " + Arrays.toString(event));
|
throw new InputMismatchException("Illegal dialogue event: " + Arrays.toString(event));
|
||||||
|
|
||||||
// extract information
|
// extract information
|
||||||
String[] values = event[1].split(",", format.size());
|
String[] values = separator.split(event[1], format.length);
|
||||||
|
|
||||||
long start = timeFormat.parse(values[format.get(EventProperty.Start)]).getTime();
|
long start = timeFormat.parse(values[formatIndexStart]).getTime();
|
||||||
long end = timeFormat.parse(values[format.get(EventProperty.End)]).getTime();
|
long end = timeFormat.parse(values[formatIndexEnd]).getTime();
|
||||||
String text = values[format.get(EventProperty.Text)].trim();
|
String text = values[formatIndexText];
|
||||||
|
|
||||||
// translate "\\n" to new lines
|
return new SubtitleElement(start, end, resolve(text));
|
||||||
String[] lines = newline.split(text);
|
|
||||||
|
|
||||||
return new SubtitleElement(start, end, join(lines, "\n"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private enum EventProperty {
|
protected String resolve(String text) {
|
||||||
Start,
|
// remove tags
|
||||||
End,
|
text = tag.matcher(text).replaceAll("");
|
||||||
Text
|
|
||||||
|
// resolve line breaks
|
||||||
|
return newline.matcher(text).replaceAll("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue