OSDN Git Service

JRE version-checking modified.
[mikutoga/Pmd2XML.git] / src / main / java / jp / sfjp / mikutoga / pmd2xml / Pmd2Xml.java
index 06d432c..05ba75d 100644 (file)
@@ -7,10 +7,8 @@
 
 package jp.sfjp.mikutoga.pmd2xml;
 
-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.pmd.IllegalPmdDataException;
-import jp.sourceforge.mikutoga.xml.TogaXmlException;
+import jp.sfjp.mikutoga.xml.TogaXmlException;
+import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 
 /**
@@ -53,6 +52,8 @@ public final class Pmd2Xml {
     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";
@@ -70,7 +71,7 @@ public final class Pmd2Xml {
             + "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 = Pmd2Xml.class;
@@ -91,6 +92,8 @@ public final class Pmd2Xml {
         APPLICENSE = verProps.getProperty("app.license");
         APPURL     = verProps.getProperty("app.url");
 
+        GENERATOR = APPNAME + ' ' + APPVER;
+
         new Pmd2Xml().hashCode();
     }
 
@@ -246,12 +249,13 @@ public final class Pmd2Xml {
     }
 
     /**
-     * å\85¥å\8a\9bã\82¹ã\83\88ã\83ªã\83¼ã\83 を準備する。
+     * å\85¥å\8a\9bã\82½ã\83¼ã\82¹を準備する。
      * <p>入力ファイルが通常ファイルとして存在しなければエラー終了。
-     * @param fileName 入力ファイル名
-     * @return å\85¥å\8a\9bã\82¹ã\83\88ã\83ªã\83¼ã\83 
+     * @param optInfo オプション情報
+     * @return å\85¥å\8a\9bã\82½ã\83¼ã\82¹
      */
-    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()) ){
@@ -261,31 +265,23 @@ public final class Pmd2Xml {
             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();
@@ -293,7 +289,7 @@ public final class Pmd2Xml {
                 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);
@@ -321,34 +317,11 @@ public final class Pmd2Xml {
     }
 
     /**
-     * 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 Pmd2XmlConv buildConverter(OptInfo optInfo){
         Pmd2XmlConv converter = new Pmd2XmlConv();
 
         converter.setInType (optInfo.getInFileType());
@@ -357,8 +330,21 @@ public final class Pmd2Xml {
         converter.setNewline(optInfo.getNewline());
         converter.setGenerator(optInfo.getGenerator());
 
+        return converter;
+    }
+
+    /**
+     * 実際のコンバート作業と異常系処理を行う。
+     * <p>異常系が起きた場合、このメソッドは制御を戻さない。
+     * @param converter コンバータ
+     * @param source 入力ソース
+     * @param ostream 出力ストリーム
+     */
+    private static void doConvert(Pmd2XmlConv converter,
+                                   InputSource source,
+                                   OutputStream ostream ){
         try{
-            converter.convert(is, os);
+            converter.convert(source, ostream);
         }catch(IOException e){
             ioError(e);
         }catch(IllegalPmdDataException e){
@@ -371,13 +357,53 @@ public final class Pmd2Xml {
             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);
+        }
+
+        Pmd2XmlConv converter = buildConverter(optInfo);
+        InputSource source = openInfile(optInfo);
+        OutputStream ostream = openOutfile(optInfo);
+
+        doConvert(converter, source, ostream);
+
+        try{
+            ostream.close();
         }catch(IOException e){
             ioError(e);
         }