/*
* Created on 2004/11/13
* Author aki@www.xucker.jpn.org
* License Apache2.0 or Common Public License
*/
package org.jpn.xucker.subtitle.sub;
import java.util.List;
import java.util.Vector;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.SystemUtils;
import org.jpn.xucker.subtitle.StandardSubObject;
import org.jpn.xucker.subtitle.SubUtils;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
/**
*
*
*/
public class SUBParser {
List subList=new Vector();
String baseDir;
private double frameRate=SubUtils.NTSC_FRAME_RATE;
private String imageExtensions[]=SubUtils.getDefaultImageExtensions();
public long getFrameTime(int frame){
long result=-1;
result=(long)((long)frame*1000/frameRate);
return result;
}
private void parseLine(String line){
if(line.startsWith("{")){
SUBObject data=new SUBObject();
String splitValue[]=parseSplit(line);
if(splitValue!=null){
int start=Integer.parseInt(splitValue[0]);
int end=Integer.parseInt(splitValue[1]);
data.setStartFrame(start);
data.setEndFrame(end);
data.setText(splitValue[2]);
subList.add(data);
}
}else{
if(line.startsWith("FrameRate=")){
setFrameRate(Double.parseDouble(line.substring("FrameRate=".length())));
}
TextObject text=new TextObject();
text.setText(line);
subList.add(text);
//
}
}
public List getSubDataList(){
List list=new Vector();
for(int i=0;i<subList.size();i++){
if(subList.get(i) instanceof SUBObject){
SUBObject subobject=(SUBObject)subList.get(i);
StandardSubObject standard=new StandardSubObject();
standard.setStartTime(getFrameTime(subobject.getStartFrame()));
standard.setEndTime(getFrameTime(subobject.getEndFrame()));
String text=subobject.getText();
if(isImagePath(text)){
String path=getInText(text);
standard.setImagePath(path);
}
list.add(standard);
}
}
return list;
}
/**
* @param text
* @return
*/
private String getInText(String text) {
if(text.startsWith("{") && text.endsWith("}")){
return text.substring(1,text.length()-1);
}else{
return text;
}
}
/**
* @param text
* @return
*/
private boolean isImagePath(String line) {
String text=getInText(line);
for (int i = 0; i < imageExtensions.length; i++) {
if(text.toLowerCase().endsWith(imageExtensions[i])){
return true;
}
}
return false;
}
public static String[] parseSplit(String line){
String result[]=new String[3];
int f1=line.indexOf("}{",1);
if(f1!=-1){
result[0]=line.substring(1,f1);
int f2=line.indexOf("}{",f1+2);
if(f2!=-1){
result[1]=line.substring(f1+2,f2);
int f3=line.indexOf("}",f2+2);
if(f3!=-1){
result[2]=line.substring(f2+2,f3);
}
}
}
return result;
}
public void parse(File file){
int offset=0;
baseDir=file.getParent();
try {
String text=IOUtils.toString(new FileReader(file));
String lines[]=text.split(SystemUtils.LINE_SEPARATOR);
int lastFrame=0;
int relative=0;
for(int i=0;i<lines.length;i++){
parseLine(lines[i]);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getBaseDir() {
return baseDir;
}
public void setBaseDir(String baseDir) {
this.baseDir = baseDir;
}
public double getFrameRate() {
return frameRate;
}
public void setFrameRate(double frameRate) {
this.frameRate = frameRate;
}
public String[] getImageExtensions() {
return imageExtensions;
}
public void setImageExtensions(String[] imageExtensions) {
this.imageExtensions = imageExtensions;
}
public List getSubList() {
return subList;
}
public void setSubList(List subList) {
this.subList = subList;
}
}
|