filebot/source/net/filebot/subtitle/SubViewerReader.java

52 lines
1.2 KiB
Java
Raw Normal View History

2014-04-19 02:30:29 -04:00
package net.filebot.subtitle;
import static java.util.regex.Pattern.*;
2014-04-19 02:30:29 -04:00
import static net.filebot.util.StringUtilities.*;
import java.text.DateFormat;
2011-11-21 07:01:05 -05:00
import java.text.ParseException;
import java.util.InputMismatchException;
import java.util.regex.Pattern;
public class SubViewerReader extends SubtitleReader {
2015-07-25 18:47:19 -04:00
private final DateFormat timeFormat = new SubtitleTimeFormat();
private final Pattern newline = compile(quote("[br]"), CASE_INSENSITIVE);
2015-07-25 18:47:19 -04:00
public SubViewerReader(Readable source) {
super(source);
}
2015-07-25 18:47:19 -04:00
@Override
protected SubtitleElement readNext() throws Exception {
// element starts with interval (e.g. 00:42:16.33,00:42:19.39)
String[] interval = scanner.nextLine().split(",", 2);
2015-07-25 18:47:19 -04:00
if (interval.length < 2 || interval[0].startsWith("[")) {
// ignore property lines
return null;
}
2015-07-25 18:47:19 -04:00
try {
long t1 = timeFormat.parse(interval[0]).getTime();
long t2 = timeFormat.parse(interval[1]).getTime();
2015-07-25 18:47:19 -04:00
// translate [br] to new lines
2009-07-10 11:12:16 -04:00
String[] lines = newline.split(scanner.nextLine());
2015-07-25 18:47:19 -04:00
return new SubtitleElement(t1, t2, join(lines, "\n"));
2011-11-21 07:01:05 -05:00
} catch (ParseException e) {
// can't parse interval, ignore line
return null;
} catch (InputMismatchException e) {
// can't parse interval, ignore line
return null;
}
}
}