/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.livesub.input.parsers;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.livesub.input.Caption;
import org.livesub.utils.Utils;
import org.livesub.utils.doublelinkedlist.DoubleLinkedList;
/**
*
* @author vasilis
*/
public class SubParser implements Parser {
@Override
public DoubleLinkedList<Caption> read(BufferedReader br) {
DoubleLinkedList<Caption> subs = new DoubleLinkedList<Caption>();
String line = null;
try {
line = br.readLine();
} catch (IOException ex) {
Logger.getLogger(SubParser.class.getName()).log(Level.SEVERE, null, ex);
}
int lineNumber = 0;
while(line != null){
Pattern pattern = Pattern.compile("\\{([0-9]+)\\}\\{([0-9]+)\\}(\\{.+\\})?(.*)");
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
long startAt = (long) ((Long.parseLong(matcher.group(1)) / Utils.fps) * 1000);
long endAt = (long) ((Long.parseLong(matcher.group(2)) / Utils.fps) * 1000);
String text = matcher.group(4);
Caption caption = new Caption(startAt, endAt);
caption.setContents(text);
caption.setNumericCounter(lineNumber);
subs.put(caption, caption.getNumericCounter());
}
lineNumber++;
try {
line = br.readLine();
} catch (IOException ex) {
Logger.getLogger(SubParser.class.getName()).log(Level.SEVERE, null, ex);
}
}
return subs;
}
}