OSDN Git Service

modify javadoc paragraph.
[mikutoga/Vmd2XML.git] / src / main / java / jp / sfjp / mikutoga / vmd2xml / Vmd2Xml.java
index 5fff72f..29bdcf7 100644 (file)
@@ -7,10 +7,8 @@
 
 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;
@@ -22,7 +20,8 @@ import java.text.MessageFormat;
 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;
 
 
@@ -54,6 +53,8 @@ public final class Vmd2Xml {
     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";
@@ -64,14 +65,14 @@ public final class Vmd2Xml {
               "{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;
@@ -92,6 +93,8 @@ public final class Vmd2Xml {
         APPLICENSE = verProps.getProperty("app.license");
         APPURL     = verProps.getProperty("app.url");
 
+        GENERATOR = APPNAME + ' ' + APPVER;
+
         new Vmd2Xml().hashCode();
     }
 
@@ -108,6 +111,7 @@ public final class Vmd2Xml {
 
     /**
      * VMを終了させる。
+     *
      * @param code 終了コード
      * @see java.lang.System#exit(int)
      */
@@ -119,6 +123,7 @@ public final class Vmd2Xml {
 
     /**
      * 共通エラーメッセージを出力する。
+     *
      * @param text 個別メッセージ
      */
     private static void errMsg(String text){
@@ -129,6 +134,7 @@ public final class Vmd2Xml {
 
     /**
      * 標準エラー出力へ例外情報出力。
+     *
      * @param ex 例外
      * @param dumpStack スタックトレースを出力するならtrue
      */
@@ -145,6 +151,7 @@ public final class Vmd2Xml {
 
     /**
      * 標準エラー出力へ例外情報出力。
+     *
      * @param ex 例外
      */
     private static void thPrintln(Throwable ex){
@@ -155,6 +162,7 @@ public final class Vmd2Xml {
     /**
      * 入出力エラー処理。
      * 例外を出力してVM終了する。
+     *
      * @param ex 例外
      */
     private static void ioError(IOException ex){
@@ -165,6 +173,7 @@ public final class Vmd2Xml {
     /**
      * XML構文エラー処理。
      * 例外を出力してVM終了する。
+     *
      * @param ex 例外
      */
     private static void xmlError(Throwable ex){
@@ -175,6 +184,7 @@ public final class Vmd2Xml {
     /**
      * VMDファイルフォーマットエラー処理。
      * 例外を出力してVM終了する。
+     *
      * @param ex 例外
      */
     private static void vmdError(MmdFormatException ex){
@@ -185,6 +195,7 @@ public final class Vmd2Xml {
     /**
      * 内部エラー処理。
      * 例外を出力してVM終了する。
+     *
      * @param ex 例外
      */
     private static void internalError(Throwable ex){
@@ -220,8 +231,11 @@ public final class Vmd2Xml {
 
     /**
      * ファイルサイズを0に切り詰める。
+     *
      * <p>ファイルが存在しなければなにもしない。
+     *
      * <p>通常ファイルでなければなにもしない。
+     *
      * @param file ファイル
      * @throws IOException 入出力エラー
      */
@@ -247,12 +261,15 @@ public final class Vmd2Xml {
     }
 
     /**
-     * 入力ストリームを準備する。
+     * 入力ソースを準備する。
+     *
      * <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()) ){
@@ -262,31 +279,26 @@ public final class Vmd2Xml {
             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();
@@ -294,7 +306,7 @@ public final class Vmd2Xml {
                 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);
@@ -322,34 +334,12 @@ public final class Vmd2Xml {
     }
 
     /**
-     * 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());
@@ -359,8 +349,23 @@ public final class Vmd2Xml {
         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){
@@ -373,13 +378,56 @@ public final class Vmd2Xml {
             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);
         }