package jp.sfjp.mikutoga.vmd2xml;
-import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import jp.sfjp.mikutoga.bin.parser.MmdFormatException;
import jp.sfjp.mikutoga.vmd.IllegalVmdDataException;
-import jp.sourceforge.mikutoga.xml.TogaXmlException;
+import jp.sfjp.mikutoga.xml.TogaXmlException;
+import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public static final String APPLICENSE;
/** 開発元URL。 */
public static final String APPURL;
+ /** ジェネレータ名。 */
+ public static final String GENERATOR;
private static final Class<?> THISCLASS;
private static final String RES_VER = "resources/version.properties";
"{0} {1}\n"
+ "\u0020\u0020License\u0020:\u0020{2}\n"
+ "\u0020\u0020{3}\n";
- private static final String MSG_NOINFILE = "Can't find input file:{0}";
+ private static final String MSG_NOINFILE = "Can''t find input file: {0}";
private static final String MSG_ABNFILE = "{0} is not file.";
private static final String MSG_OWOUTFILE =
"{0} already exists.\n"
+ "If you want to overwrite, use -f.";
private static final String MSG_OLDJRE = "You need JRE {0} or later.";
- private static final String REQUIRED_JRE = "1.6";
+ private static final String REQUIRED_JRE = "1.8";
static{
THISCLASS = Vmd2Xml.class;
APPLICENSE = verProps.getProperty("app.license");
APPURL = verProps.getProperty("app.url");
+ GENERATOR = APPNAME + ' ' + APPVER;
+
new Vmd2Xml().hashCode();
}
/**
* VMを終了させる。
+ *
* @param code 終了コード
* @see java.lang.System#exit(int)
*/
/**
* 共通エラーメッセージを出力する。
+ *
* @param text 個別メッセージ
*/
private static void errMsg(String text){
/**
* 標準エラー出力へ例外情報出力。
+ *
* @param ex 例外
* @param dumpStack スタックトレースを出力するならtrue
*/
/**
* 標準エラー出力へ例外情報出力。
+ *
* @param ex 例外
*/
private static void thPrintln(Throwable ex){
/**
* 入出力エラー処理。
* 例外を出力してVM終了する。
+ *
* @param ex 例外
*/
private static void ioError(IOException ex){
/**
* XML構文エラー処理。
* 例外を出力してVM終了する。
+ *
* @param ex 例外
*/
private static void xmlError(Throwable ex){
/**
* VMDファイルフォーマットエラー処理。
* 例外を出力してVM終了する。
+ *
* @param ex 例外
*/
private static void vmdError(MmdFormatException ex){
/**
* 内部エラー処理。
* 例外を出力してVM終了する。
+ *
* @param ex 例外
*/
private static void internalError(Throwable ex){
/**
* ファイルサイズを0に切り詰める。
+ *
* <p>ファイルが存在しなければなにもしない。
+ *
* <p>通常ファイルでなければなにもしない。
+ *
* @param file ファイル
* @throws IOException 入出力エラー
*/
}
/**
- * 入力ストリームを準備する。
+ * 入力ソースを準備する。
+ *
* <p>入力ファイルが通常ファイルとして存在しなければエラー終了。
- * @param fileName 入力ファイル名
- * @return 入力ストリーム
+ *
+ * @param optInfo オプション情報
+ * @return 入力ソース
*/
- private static InputStream openInfile(String fileName){
+ private static InputSource openInfile(OptInfo optInfo){
+ String fileName = optInfo.getInFilename();
File inFile = new File(fileName);
if( (! inFile.exists()) || (! inFile.isFile()) ){
exit(EXIT_IOERR);
}
- InputStream is;
- try{
- is = new FileInputStream(inFile);
- }catch(FileNotFoundException e){
- ioError(e);
- assert false;
- throw new AssertionError(e);
- }
-
- is = new BufferedInputStream(is);
+ InputSource source = XmlInputUtil.fileToSource(inFile);
- return is;
+ return source;
}
/**
* 出力ストリームを準備する。
+ *
* <p>出力ファイルが通常ファイルでない場合はエラー終了。
+ *
* <p>既存の出力ファイルに上書き指示が伴っていなければエラー終了。
- * @param fileName 出力ファイル名
- * @param overWrite 頭から上書きして良ければtrue
+ *
+ * @param optInfo オプション情報
* @return 出力ストリーム
*/
- private static OutputStream openOutfile(String fileName,
- boolean overWrite) {
- File outFile = new File(fileName);
+ private static OutputStream openOutfile(OptInfo optInfo) {
+ String outputFile = optInfo.getOutFilename();
+ boolean overwrite = optInfo.overwriteMode();
+
+ File outFile = new File(outputFile);
if(outFile.exists()){
String absPath = outFile.getAbsolutePath();
String msg = MessageFormat.format(MSG_ABNFILE, absPath);
errMsg(msg);
exit(EXIT_IOERR);
- }else if( ! overWrite ){
+ }else if( ! overwrite ){
String msg = MessageFormat.format(MSG_OWOUTFILE, absPath);
errMsg(msg);
exit(EXIT_IOERR);
}
/**
- * Mainエントリ。
- * @param args コマンドパラメータ
+ * オプション情報に従いコンバータを生成する。
+ *
+ * @param optInfo オプション情報
+ * @return コンバータ
*/
- public static void main(String[] args){
- checkJRE();
-
- OptInfo optInfo;
- try{
- optInfo = OptInfo.parseOption(args);
- }catch(CmdLineException e){
- String optErrMsg = e.getLocalizedMessage();
- errMsg(optErrMsg);
- exit(EXIT_OPTERR);
- return;
- }
-
- if(optInfo.needHelp()){
- putHelp();
- exit(EXIT_OK);
- }
-
- String inputFile = optInfo.getInFilename();
- String outputFile = optInfo.getOutFilename();
- boolean overwrite = optInfo.overwriteMode();
-
- InputStream is = openInfile(inputFile);
- OutputStream os = openOutfile(outputFile, overwrite);
-
+ private static Vmd2XmlConv buildConverter(OptInfo optInfo){
Vmd2XmlConv converter = new Vmd2XmlConv();
converter.setInType (optInfo.getInFileType());
converter.setGenerator(optInfo.getGenerator());
converter.setQuaterniomMode(optInfo.isQuaterniomMode());
+ return converter;
+ }
+
+ /**
+ * 実際のコンバート作業と異常系処理を行う。
+ *
+ * <p>異常系が起きた場合、このメソッドは制御を戻さない。
+ *
+ * @param converter コンバータ
+ * @param source 入力ソース
+ * @param ostream 出力ストリーム
+ */
+ private static void doConvert(Vmd2XmlConv converter,
+ InputSource source,
+ OutputStream ostream ){
try{
- converter.convert(is, os);
+ converter.convert(source, ostream);
}catch(IOException e){
ioError(e);
}catch(IllegalVmdDataException e){
xmlError(e);
}
+ return;
+ }
+
+ /**
+ * コマンドライン文字列をオプション情報としてパースする。
+ *
+ * <p>異常系が起きた場合、このメソッドは制御を戻さない。
+ *
+ * @param args コマンドライン文字列群
+ * @return オプション情報
+ */
+ private static OptInfo parseOption(String[] args){
+ OptInfo optInfo;
+
try{
- is.close();
- try{
- os.close();
- }catch(IOException e){
- ioError(e);
- }
+ optInfo = OptInfo.parseOption(args);
+ }catch(CmdLineException e){
+ String optErrMsg = e.getLocalizedMessage();
+ errMsg(optErrMsg);
+ exit(EXIT_OPTERR);
+
+ assert false;
+ throw new AssertionError(e);
+ }
+
+ return optInfo;
+ }
+
+ /**
+ * Mainエントリ。
+ *
+ * @param args コマンドパラメータ
+ */
+ public static void main(String[] args){
+ checkJRE();
+
+ OptInfo optInfo = parseOption(args);
+ if(optInfo.needHelp()){
+ putHelp();
+ exit(EXIT_OK);
+ }
+
+ Vmd2XmlConv converter = buildConverter(optInfo);
+ InputSource source = openInfile(optInfo);
+ OutputStream ostream = openOutfile(optInfo);
+
+ doConvert(converter, source, ostream);
+
+ try{
+ ostream.close();
}catch(IOException e){
ioError(e);
}