/*
* Created on 2004/12/01
* Author aki@www.xucker.jpn.org
* License Apache2.0 or Common Public License
*/
package org.jpn.xucker.subtitle.sub2;
import java.util.List;
import java.util.Vector;
import org.jpn.xucker.subtitle.StandardSubObject;
import org.jpn.xucker.subtitle.srt.SRTObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
/**
*
*
*/
public class SUBParser {
public final static double NTSC=29.97;
public final static double PAL=25;
public List parseSub(Reader re){
List list=new Vector();
BufferedReader reader=new BufferedReader(re);
String line;
try {
while((line=reader.readLine())!=null){
if(line.startsWith(";")){
CommentObject comment=new CommentObject();
comment.setComment(line.substring(1));
list.add(comment);
}else if(line.startsWith("{")){
int frame1_end=line.indexOf("}{",1);
if(frame1_end!=-1){
int frame2_end=line.indexOf("}",frame1_end+2);
if(frame2_end!=-1){
SUBObject sub=new SUBObject();
sub.setStartFrame(Integer.parseInt(line.substring(1,frame1_end)));
sub.setEndFrame(Integer.parseInt(line.substring(frame1_end+2,frame2_end)));
sub.setText(line.substring(frame2_end+1));
list.add(sub);
}
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public List getSubDataList(List subList){
return getSubDataList(subList,NTSC);
}
public List getSubDataList(List subList,double frameRate){
List list=new Vector();
for(int i=0;i<subList.size();i++){
if(subList.get(i) instanceof SUBObject){
SUBObject sub=(SUBObject)subList.get(i);
StandardSubObject standard=new StandardSubObject();
standard.setStartTime((int)(frameRate*sub.getStartFrame()));
standard.setEndTime((int)(frameRate*sub.getEndFrame()));
if(isImageFile(sub.getText())){
standard.setImagePath(sub.getText());
}else{
standard.setText(sub.getPrintString());
}
list.add(standard);
}
}
return list;
}
/**
* @param text
* @return
*/
private boolean isImageFile(String text) {
String extensions[]={".png",".gif",".bmp",".jpg"};
for (int i = 0; i < extensions.length; i++) {
if(text.toLowerCase().endsWith(extensions[i])){
return true;
}
}
return false;
}
}
|