OSDN Git Service

Vmd2Xmlとの共通化
[mikutoga/Pmd2XML.git] / src / main / java / jp / sfjp / mikutoga / pmd2xml / Pmd2XmlConv.java
1 /*
2  * pmd 2 xml converter
3  *
4  * License : The MIT License
5  * Copyright(c) 2010 MikuToga Partners
6  */
7
8 package jp.sfjp.mikutoga.pmd2xml;
9
10 import java.io.BufferedWriter;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.OutputStream;
14 import java.io.OutputStreamWriter;
15 import java.io.Writer;
16 import java.nio.charset.Charset;
17 import jp.sfjp.mikutoga.bin.parser.MmdFormatException;
18 import jp.sfjp.mikutoga.pmd.IllegalPmdDataException;
19 import jp.sfjp.mikutoga.pmd.model.PmdModel;
20 import jp.sfjp.mikutoga.pmd.model.binio.PmdExporter;
21 import jp.sfjp.mikutoga.pmd.model.binio.PmdLoader;
22 import jp.sfjp.mikutoga.pmd.model.xml.PmdXmlExporter;
23 import jp.sfjp.mikutoga.pmd.model.xml.XmlModelFileType;
24 import jp.sfjp.mikutoga.pmd.model.xml.XmlPmdLoader;
25 import jp.sfjp.mikutoga.xml.TogaXmlException;
26 import org.xml.sax.InputSource;
27 import org.xml.sax.SAXException;
28 import org.xml.sax.XMLReader;
29
30 /**
31  * PMD-XML間コンバータ本体。
32  */
33 public class Pmd2XmlConv {
34
35     /** デフォルトエンコーディング。 */
36     private static final Charset CS_UTF8 = Charset.forName("UTF-8");
37
38
39     private ModelFileType inTypes  = ModelFileType.NONE;
40     private ModelFileType outTypes = ModelFileType.NONE;
41     private String newLine = "\r\n";
42     private String generator = null;
43
44
45     /**
46      * コンストラクタ。
47      */
48     public Pmd2XmlConv(){
49         super();
50         return;
51     }
52
53
54     /**
55      * 入力ファイル種別を設定する。
56      * @param type ファイル種別
57      * @throws NullPointerException 引数がnull
58      * @throws IllegalArgumentException 具体的な種別を渡さなかった
59      */
60     public void setInType(ModelFileType type)
61             throws NullPointerException, IllegalArgumentException {
62         if(type == null) throw new NullPointerException();
63         if(type == ModelFileType.NONE) throw new IllegalArgumentException();
64         this.inTypes = type;
65         return;
66     }
67
68     /**
69      * 入力ファイル種別を返す。
70      * @return ファイル種別
71      */
72     public ModelFileType getInTypes(){
73         return this.inTypes;
74     }
75
76     /**
77      * 出力ファイル種別を設定する。
78      * @param type ファイル種別
79      * @throws NullPointerException 引数がnull
80      * @throws IllegalArgumentException 具体的な種別を渡さなかった
81      */
82     public void setOutType(ModelFileType type)
83             throws NullPointerException, IllegalArgumentException {
84         if(type == null) throw new NullPointerException();
85         if(type == ModelFileType.NONE) throw new IllegalArgumentException();
86         this.outTypes = type;
87         return;
88     }
89
90     /**
91      * 出力ファイル種別を返す。
92      * @return ファイル種別
93      */
94     public ModelFileType getOutTypes(){
95         return this.outTypes;
96     }
97
98     /**
99      * XML出力用改行文字列を設定する。
100      * @param newline 改行文字
101      */
102     public void setNewline(String newline){
103         this.newLine = newline;
104         return;
105     }
106
107     /**
108      * XML出力用改行文字列を返す。
109      * @return 改行文字
110      */
111     public String getNewline(){
112         return this.newLine;
113     }
114
115     /**
116      * ジェネレータ名を設定する。
117      * @param generator ジェネレータ名。表示したくない場合はnull
118      */
119     public void setGenerator(String generator){
120         this.generator = generator;
121         return;
122     }
123
124     /**
125      * ジェネレータ名を返す。
126      * @return ジェネレータ名。非表示の場合はnullを返す。
127      */
128     public String getGenerator(){
129         return this.generator;
130     }
131
132     /**
133      * ファイル変換を行う。
134      * <p>XML入力の場合は{@link #convert(InputSource, OutputStream)}を
135      * 推奨する。
136      * @param is 入力ストリーム
137      * @param os 出力ストリーム
138      * @throws IOException 入力エラー
139      * @throws MmdFormatException フォーマットエラー
140      * @throws SAXException XMLエラー
141      * @throws TogaXmlException XMLエラー
142      * @throws IllegalPmdDataException 内部エラー
143      */
144     public void convert(InputStream is, OutputStream os)
145             throws IOException,
146                    MmdFormatException,
147                    SAXException,
148                    TogaXmlException,
149                    IllegalPmdDataException {
150         PmdModel model = readModel(is);
151         writeModel(model, os);
152         return;
153     }
154
155     /**
156      * ファイル変換を行う。
157      * <p>PMD入力の場合は{@link InputStream}に
158      * バイトストリームが直接設定されていなければならない。
159      * <p>XML入力の場合は{@link InputStream}に
160      * URL(systemId)のみの設定を推奨する。
161      * @param source 入力ソース
162      * @param os 出力ストリーム
163      * @throws IOException 入力エラー
164      * @throws MmdFormatException フォーマットエラー
165      * @throws SAXException XMLエラー
166      * @throws TogaXmlException XMLエラー
167      * @throws IllegalPmdDataException 内部エラー
168      */
169     public void convert(InputSource source, OutputStream os)
170             throws IOException,
171                    MmdFormatException,
172                    SAXException,
173                    TogaXmlException,
174                    IllegalPmdDataException {
175         PmdModel model = readModel(source);
176         writeModel(model, os);
177         return;
178     }
179
180     /**
181      * モデルファイルを読み込む。
182      * <p>XML読み込みの場合は、
183      * こちらより{@link #readModel(InputSource)}版を推奨する。
184      * @param is 入力ストリーム
185      * @return モデルデータ
186      * @throws IOException 入力エラー
187      * @throws MmdFormatException フォーマットエラー
188      * @throws SAXException XMLエラー
189      * @throws TogaXmlException XMLエラー
190      */
191     public PmdModel readModel(InputStream is)
192             throws IOException,
193                    MmdFormatException,
194                    SAXException,
195                    TogaXmlException {
196         InputSource source = new InputSource(is);
197
198         PmdModel model;
199
200         try{
201             model = readModel(source);
202         }finally{
203             is.close();
204         }
205
206         return model;
207     }
208
209     /**
210      * モデルファイルを読み込む。
211      * @param source 入力ソース
212      * @return モデルデータ
213      * @throws IOException 入力エラー
214      * @throws MmdFormatException フォーマットエラー
215      * @throws SAXException XMLエラー
216      * @throws TogaXmlException XMLエラー
217      */
218     public PmdModel readModel(InputSource source)
219             throws IOException,
220                    MmdFormatException,
221                    SAXException,
222                    TogaXmlException {
223         PmdModel model = null;
224
225         if(this.inTypes.isPmd()){
226             InputStream is = XmlInputUtil.openInputSource(source);
227             try{
228                 model = pmdRead(is);
229             }finally{
230                 is.close();
231             }
232         }else if(this.inTypes.isXml()){
233             model = xmlRead(source);
234         }else{
235             throw new IllegalStateException();
236         }
237
238         return model;
239     }
240
241     /**
242      * モデルファイルを出力する。
243      * @param model モデルデータ
244      * @param os 出力ストリーム
245      * @throws IOException 出力エラー
246      * @throws IllegalPmdDataException データの不備
247      */
248     public void writeModel(PmdModel model, OutputStream os)
249             throws IOException,
250                    IllegalPmdDataException {
251         if(this.outTypes.isPmd()){
252             pmdOut(model, os);
253         }else if(this.outTypes.isXml()){
254             xmlOut(model, os);
255         }else{
256             throw new IllegalStateException();
257         }
258
259         return;
260     }
261
262     /**
263      * PMDファイルからモデルデータを読み込む。
264      * @param is 入力ストリーム
265      * @return モデルデータ
266      * @throws IOException 入力エラー
267      * @throws MmdFormatException 不正なPMDファイルフォーマット
268      */
269     private PmdModel pmdRead(InputStream is)
270             throws IOException, MmdFormatException{
271         PmdLoader loader = new PmdLoader();
272         PmdModel model = loader.load(is);
273         return model;
274     }
275
276     /**
277      * XMLファイルからモデルデータを読み込む。
278      * @param source 入力ソース
279      * @return モデルデータ
280      * @throws IOException 入力エラー
281      * @throws SAXException XML構文エラー
282      * @throws TogaXmlException 不正なXMLデータ
283      */
284     private PmdModel xmlRead(InputSource source)
285             throws IOException,
286                    SAXException,
287                    TogaXmlException {
288         XMLReader reader = XmlInputUtil.buildReader(this.inTypes);
289         XmlPmdLoader loader = new XmlPmdLoader(reader);
290         PmdModel model = loader.parse(source);
291         return model;
292     }
293
294     /**
295      * モデルデータをPMDファイルに出力する。
296      * @param model モデルデータ
297      * @param ostream 出力ストリーム
298      * @throws IOException 出力エラー
299      * @throws IllegalPmdDataException 不正なモデルデータ
300      */
301     private void pmdOut(PmdModel model, OutputStream ostream)
302             throws IOException, IllegalPmdDataException{
303         PmdExporter exporter = new PmdExporter(ostream);
304         exporter.dumpPmdModel(model);
305         ostream.close();
306         return;
307     }
308
309     /**
310      * モデルデータをXMLファイルに出力する。
311      * @param model モデルデータ
312      * @param ostream 出力ストリーム
313      * @throws IOException 出力エラー
314      * @throws IllegalPmdDataException 不正なモデルデータ
315      */
316     private void xmlOut(PmdModel model, OutputStream ostream)
317             throws IOException, IllegalPmdDataException{
318         PmdXmlExporter exporter = new PmdXmlExporter();
319
320         XmlModelFileType xmlType = this.outTypes.toXmlType();
321         exporter.setXmlFileType(xmlType);
322         exporter.setNewLine(this.newLine);
323         exporter.setGenerator(this.generator);
324
325         Writer writer;
326         writer = new OutputStreamWriter(ostream, CS_UTF8);
327         writer = new BufferedWriter(writer);
328
329         exporter.putPmdXml(model, writer);
330
331         exporter.close();
332
333         return;
334     }
335
336 }