OSDN Git Service

Vmd2Xmlとの共通化
[mikutoga/Pmd2XML.git] / src / main / java / jp / sfjp / mikutoga / pmd2xml / Pmd2XmlConv.java
index 3a0df47..1b222f2 100644 (file)
@@ -7,35 +7,35 @@
 
 package jp.sfjp.mikutoga.pmd2xml;
 
+import java.io.BufferedWriter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.validation.Schema;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.nio.charset.Charset;
 import jp.sfjp.mikutoga.bin.parser.MmdFormatException;
 import jp.sfjp.mikutoga.pmd.IllegalPmdDataException;
-import jp.sfjp.mikutoga.pmd.binio.PmdExporter;
-import jp.sfjp.mikutoga.pmd.binio.PmdLoader;
 import jp.sfjp.mikutoga.pmd.model.PmdModel;
-import jp.sfjp.mikutoga.pmd.xml.Schema101009;
-import jp.sfjp.mikutoga.pmd.xml.Schema130128;
-import jp.sfjp.mikutoga.pmd.xml.XmlExporter;
-import jp.sfjp.mikutoga.pmd.xml.XmlLoader;
-import jp.sfjp.mikutoga.pmd.xml.XmlModelFileType;
-import jp.sourceforge.mikutoga.xml.BotherHandler;
-import jp.sourceforge.mikutoga.xml.LocalSchema;
-import jp.sourceforge.mikutoga.xml.TogaXmlException;
-import jp.sourceforge.mikutoga.xml.XmlResourceResolver;
+import jp.sfjp.mikutoga.pmd.model.binio.PmdExporter;
+import jp.sfjp.mikutoga.pmd.model.binio.PmdLoader;
+import jp.sfjp.mikutoga.pmd.model.xml.PmdXmlExporter;
+import jp.sfjp.mikutoga.pmd.model.xml.XmlModelFileType;
+import jp.sfjp.mikutoga.pmd.model.xml.XmlPmdLoader;
+import jp.sfjp.mikutoga.xml.TogaXmlException;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
 
 /**
  * PMD-XML間コンバータ本体。
  */
 public class Pmd2XmlConv {
 
+    /** デフォルトエンコーディング。 */
+    private static final Charset CS_UTF8 = Charset.forName("UTF-8");
+
+
     private ModelFileType inTypes  = ModelFileType.NONE;
     private ModelFileType outTypes = ModelFileType.NONE;
     private String newLine = "\r\n";
@@ -52,74 +52,13 @@ public class Pmd2XmlConv {
 
 
     /**
-     * ドキュメントビルダファクトリを初期化する。
-     * @param builderFactory ドキュメントビルダファクトリ
-     */
-    private static void initBuilderFactory(
-            DocumentBuilderFactory builderFactory ){
-        builderFactory.setCoalescing(true);
-        builderFactory.setExpandEntityReferences(true);
-        builderFactory.setIgnoringComments(true);
-        builderFactory.setIgnoringElementContentWhitespace(false);
-        builderFactory.setNamespaceAware(true);
-        builderFactory.setValidating(false);
-        builderFactory.setXIncludeAware(false);
-
-//      builderFactory.setFeature(name, value);
-//      builderFactory.setAttribute(name, value);
-
-        return;
-    }
-
-    /**
-     * DOMビルダ生成。
-     * @return DOMビルダ
-     */
-    private DocumentBuilder buildBuilder(){
-        XmlResourceResolver resolver = new XmlResourceResolver();
-
-        Schema schema;
-
-        switch(this.inTypes){
-        case XML_101009:
-            schema = LocalSchema.newSchema(resolver, new Schema101009());
-            break;
-        case XML_130128:
-            schema = LocalSchema.newSchema(resolver, new Schema130128());
-            break;
-        case XML_AUTO:
-            schema = LocalSchema.newSchema(resolver,
-                    new Schema101009(), new Schema130128());
-            break;
-        default:
-            throw new IllegalStateException();
-        }
-
-        DocumentBuilderFactory builderFactory =
-                DocumentBuilderFactory.newInstance();
-        initBuilderFactory(builderFactory);
-        builderFactory.setSchema(schema);
-
-        DocumentBuilder result;
-        try{
-            result = builderFactory.newDocumentBuilder();
-        }catch(ParserConfigurationException e){
-            assert false;
-            throw new AssertionError(e);
-        }
-        result.setEntityResolver(resolver);
-        result.setErrorHandler(BotherHandler.HANDLER);
-
-        return result;
-    }
-
-
-    /**
      * 入力ファイル種別を設定する。
      * @param type ファイル種別
+     * @throws NullPointerException 引数がnull
      * @throws IllegalArgumentException 具体的な種別を渡さなかった
      */
-    public void setInType(ModelFileType type){
+    public void setInType(ModelFileType type)
+            throws NullPointerException, IllegalArgumentException {
         if(type == null) throw new NullPointerException();
         if(type == ModelFileType.NONE) throw new IllegalArgumentException();
         this.inTypes = type;
@@ -137,9 +76,11 @@ public class Pmd2XmlConv {
     /**
      * 出力ファイル種別を設定する。
      * @param type ファイル種別
+     * @throws NullPointerException 引数がnull
      * @throws IllegalArgumentException 具体的な種別を渡さなかった
      */
-    public void setOutType(ModelFileType type){
+    public void setOutType(ModelFileType type)
+            throws NullPointerException, IllegalArgumentException {
         if(type == null) throw new NullPointerException();
         if(type == ModelFileType.NONE) throw new IllegalArgumentException();
         this.outTypes = type;
@@ -167,7 +108,7 @@ public class Pmd2XmlConv {
      * XML出力用改行文字列を返す。
      * @return 改行文字
      */
-    public String getNewLine(){
+    public String getNewline(){
         return this.newLine;
     }
 
@@ -190,6 +131,8 @@ public class Pmd2XmlConv {
 
     /**
      * ファイル変換を行う。
+     * <p>XML入力の場合は{@link #convert(InputSource, OutputStream)}を
+     * 推奨する。
      * @param is 入力ストリーム
      * @param os 出力ストリーム
      * @throws IOException 入力エラー
@@ -210,26 +153,84 @@ public class Pmd2XmlConv {
     }
 
     /**
+     * ファイル変換を行う。
+     * <p>PMD入力の場合は{@link InputStream}に
+     * バイトストリームが直接設定されていなければならない。
+     * <p>XML入力の場合は{@link InputStream}に
+     * URL(systemId)のみの設定を推奨する。
+     * @param source 入力ソース
+     * @param os 出力ストリーム
+     * @throws IOException 入力エラー
+     * @throws MmdFormatException フォーマットエラー
+     * @throws SAXException XMLエラー
+     * @throws TogaXmlException XMLエラー
+     * @throws IllegalPmdDataException 内部エラー
+     */
+    public void convert(InputSource source, OutputStream os)
+            throws IOException,
+                   MmdFormatException,
+                   SAXException,
+                   TogaXmlException,
+                   IllegalPmdDataException {
+        PmdModel model = readModel(source);
+        writeModel(model, os);
+        return;
+    }
+
+    /**
      * モデルファイルを読み込む。
+     * <p>XML読み込みの場合は、
+     * こちらより{@link #readModel(InputSource)}版を推奨する。
      * @param is 入力ストリーム
      * @return モデルデータ
      * @throws IOException 入力エラー
      * @throws MmdFormatException フォーマットエラー
      * @throws SAXException XMLエラー
      * @throws TogaXmlException XMLエラー
-     * @throws IllegalStateException ファイル種別がまた指定されていない
      */
     public PmdModel readModel(InputStream is)
             throws IOException,
                    MmdFormatException,
                    SAXException,
                    TogaXmlException {
+        InputSource source = new InputSource(is);
+
+        PmdModel model;
+
+        try{
+            model = readModel(source);
+        }finally{
+            is.close();
+        }
+
+        return model;
+    }
+
+    /**
+     * モデルファイルを読み込む。
+     * @param source 入力ソース
+     * @return モデルデータ
+     * @throws IOException 入力エラー
+     * @throws MmdFormatException フォーマットエラー
+     * @throws SAXException XMLエラー
+     * @throws TogaXmlException XMLエラー
+     */
+    public PmdModel readModel(InputSource source)
+            throws IOException,
+                   MmdFormatException,
+                   SAXException,
+                   TogaXmlException {
         PmdModel model = null;
 
         if(this.inTypes.isPmd()){
-            model = pmdRead(is);
+            InputStream is = XmlInputUtil.openInputSource(source);
+            try{
+                model = pmdRead(is);
+            }finally{
+                is.close();
+            }
         }else if(this.inTypes.isXml()){
-            model = xmlRead(is);
+            model = xmlRead(source);
         }else{
             throw new IllegalStateException();
         }
@@ -243,7 +244,6 @@ public class Pmd2XmlConv {
      * @param os 出力ストリーム
      * @throws IOException 出力エラー
      * @throws IllegalPmdDataException データの不備
-     * @throws IllegalStateException ファイル種別がまた指定されていない
      */
     public void writeModel(PmdModel model, OutputStream os)
             throws IOException,
@@ -268,30 +268,13 @@ public class Pmd2XmlConv {
      */
     private PmdModel pmdRead(InputStream is)
             throws IOException, MmdFormatException{
-        PmdLoader loader = new PmdLoader(is);
-        PmdModel model = loader.load();
+        PmdLoader loader = new PmdLoader();
+        PmdModel model = loader.load(is);
         return model;
     }
 
     /**
      * XMLファイルからモデルデータを読み込む。
-     * @param is 入力ストリーム
-     * @return モデルデータ
-     * @throws IOException 入力エラー
-     * @throws SAXException XML構文エラー
-     * @throws TogaXmlException 不正なXMLデータ
-     */
-    private PmdModel xmlRead(InputStream is)
-            throws IOException,
-                   SAXException,
-                   TogaXmlException {
-        InputSource source = new InputSource(is);
-        PmdModel result = xmlRead(source);
-        return result;
-    }
-
-    /**
-     * XMLファイルからモデルデータを読み込む。
      * @param source 入力ソース
      * @return モデルデータ
      * @throws IOException 入力エラー
@@ -302,9 +285,9 @@ public class Pmd2XmlConv {
             throws IOException,
                    SAXException,
                    TogaXmlException {
-        DocumentBuilder builder = buildBuilder();
-        XmlLoader loader = new XmlLoader();
-        PmdModel model = loader.parse(builder, source);
+        XMLReader reader = XmlInputUtil.buildReader(this.inTypes);
+        XmlPmdLoader loader = new XmlPmdLoader(reader);
+        PmdModel model = loader.parse(source);
         return model;
     }
 
@@ -332,14 +315,18 @@ public class Pmd2XmlConv {
      */
     private void xmlOut(PmdModel model, OutputStream ostream)
             throws IOException, IllegalPmdDataException{
-        XmlExporter exporter = new XmlExporter(ostream);
+        PmdXmlExporter exporter = new PmdXmlExporter();
 
         XmlModelFileType xmlType = this.outTypes.toXmlType();
         exporter.setXmlFileType(xmlType);
         exporter.setNewLine(this.newLine);
         exporter.setGenerator(this.generator);
 
-        exporter.putPmdModel(model);
+        Writer writer;
+        writer = new OutputStreamWriter(ostream, CS_UTF8);
+        writer = new BufferedWriter(writer);
+
+        exporter.putPmdXml(model, writer);
 
         exporter.close();