/*
* Created on 2005/02/07
* Author aki@www.xucker.jpn.org
* License Apache2.0 or Common Public License
*/
package org.jpn.xucker.commons.ipod;
import java.util.List;
import java.util.Vector;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*
*
*/
public class TextConverter {
public String encode=TextUtil.SJIS;//
public String audioPath;
public int margin=64;
public String title;
public String header;
public String footer;
public MenuMaker menuMaker=new PrevNextMenu();
public String audioName="PLAY";
public TextPopper popper=new DoubleLineTextPopper();
public static Log log=LogFactory.getLog(TextConverter.class);
public String lineSeparator=TextUtil.LINE_N;
public String[] convert(String baseName,String text){
String baseText[]=split(baseName,text);
log.debug("split");
for(int i=0;i<baseText.length;i++){
int number=i+1;
StringBuffer mainBuffer=new StringBuffer();
mainBuffer.append(TextUtil.toEncode(encode));
mainBuffer.append(lineSeparator);
mainBuffer.append(TextUtil.toTitle(baseName+" "+number));
mainBuffer.append(lineSeparator);
if(i==0 && audioPath!=null){
mainBuffer.append(TextUtil.toLink(audioName,audioPath));
mainBuffer.append(lineSeparator);
}
mainBuffer.append(baseText[i]);
mainBuffer.append(lineSeparator);
mainBuffer.append(menuMaker.getMenu(i,baseText.length));
if(log.isDebugEnabled()){
int textSize=TextUtil.countText(baseText[i],encode);
int allSize=TextUtil.countText(mainBuffer.toString(),encode);
log.debug("text size:"+allSize+","+textSize+","+(allSize-textSize));
}
baseText[i]=TextUtil.toFix(mainBuffer.toString());
}
return baseText;
}
public String[] split(String baseName,String text){
List stringVector=new Vector();
int ignorebyte=margin;
menuMaker.setBaseName(baseName);
menuMaker.setText(text);
ignorebyte+=menuMaker.getBytes(encode);
ignorebyte+=countOtherByte();
ignorebyte+=TextUtil.toEncode(encode).length();
log.debug("header size"+ignorebyte);
StringBuffer textData=new StringBuffer();
String tmpLine=null;
int count=0;
popper.setText(text);
boolean countAudio=false;
while((tmpLine=popper.nextLine())!=null){
if(countAudio==false && audioPath!=null){
count+=TextUtil.countText(TextUtil.toLink(audioName,audioPath),encode);
countAudio=false;
}
count+=TextUtil.countText(tmpLine+lineSeparator,encode);
if(count+ignorebyte>TextUtil.MAX_LENGTH){
log.debug("over "+count+","+ignorebyte);
stringVector.add(textData.toString());
log.debug("text size:"+TextUtil.countText(textData.toString(),encode));
textData=new StringBuffer();
textData.append(tmpLine+lineSeparator);
count=TextUtil.countText(tmpLine+lineSeparator,encode);
}else{
textData.append(tmpLine+lineSeparator);
}
}
if(textData.length()>0){
stringVector.add(textData.toString());
}
log.debug("text size "+stringVector.size());
return (String[])stringVector.toArray(new String[stringVector.size()]);
}
public int countOtherByte(){
int all=TextUtil.countText(TextUtil.toTitle(title),encode);
if(header!=null){
all+=TextUtil.countText(header,encode);
}
if(footer!=null){
all+=TextUtil.countText(footer,encode);
}
return all;
}
}
|