package org.livesub.input;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.livesub.input.parsers.Parser;
import org.livesub.utils.doublelinkedlist.DoubleLinkedList;
import org.livesub.input.parsers.SrtParser;
import org.livesub.input.parsers.SubParser;
import org.livesub.utils.Utils;
/**
* Detect the encoding and open the file,
* then read the subs with the appropriate parser
* @author vasilis
*/
public class CaptionsParser {
public DoubleLinkedList<Caption> read(File file){
FileInputStream fstream = null;
try {
fstream = new FileInputStream(file);
} catch (FileNotFoundException ex) {
Logger.getLogger(CaptionsParser.class.getName()).log(Level.SEVERE, null, ex);
}
DataInputStream in = new DataInputStream(fstream);
String encoding;
try {
encoding = Utils.detectEncoding(file.getPath());
} catch (IOException ex) {
encoding = Utils.encoding;
Logger.getLogger(CaptionsParser.class.getName()).log(Level.SEVERE, null, ex);
}
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(in, encoding));
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(CaptionsParser.class.getName()).log(Level.SEVERE, null, ex);
}
DoubleLinkedList<Caption> subs = new DoubleLinkedList<Caption>();
Parser parser = null;
if(file.getName().toLowerCase().endsWith(".srt")) {
parser = new SrtParser();
} else if(file.getName().toLowerCase().endsWith(".sub")) {
parser = new SubParser();
}
if(parser != null) {
subs = parser.read(br);
}
return subs;
}
}