OSDN Git Service

Merge release/v1.203.2
[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      *
57      * @param type ファイル種別
58      * @throws NullPointerException 引数がnull
59      * @throws IllegalArgumentException 具体的な種別を渡さなかった
60      */
61     public void setInType(ModelFileType type)
62             throws NullPointerException, IllegalArgumentException {
63         if(type == null) throw new NullPointerException();
64         if(type == ModelFileType.NONE) throw new IllegalArgumentException();
65         this.inTypes = type;
66         return;
67     }
68
69     /**
70      * 入力ファイル種別を返す。
71      *
72      * @return ファイル種別
73      */
74     public ModelFileType getInTypes(){
75         return this.inTypes;
76     }
77
78     /**
79      * 出力ファイル種別を設定する。
80      *
81      * @param type ファイル種別
82      * @throws NullPointerException 引数がnull
83      * @throws IllegalArgumentException 具体的な種別を渡さなかった
84      */
85     public void setOutType(ModelFileType type)
86             throws NullPointerException, IllegalArgumentException {
87         if(type == null) throw new NullPointerException();
88         if(type == ModelFileType.NONE) throw new IllegalArgumentException();
89         this.outTypes = type;
90         return;
91     }
92
93     /**
94      * 出力ファイル種別を返す。
95      *
96      * @return ファイル種別
97      */
98     public ModelFileType getOutTypes(){
99         return this.outTypes;
100     }
101
102     /**
103      * XML出力用改行文字列を設定する。
104      *
105      * @param newline 改行文字
106      */
107     public void setNewline(String newline){
108         this.newLine = newline;
109         return;
110     }
111
112     /**
113      * XML出力用改行文字列を返す。
114      *
115      * @return 改行文字
116      */
117     public String getNewline(){
118         return this.newLine;
119     }
120
121     /**
122      * ジェネレータ名を設定する。
123      *
124      * @param generator ジェネレータ名。表示したくない場合はnull
125      */
126     public void setGenerator(String generator){
127         this.generator = generator;
128         return;
129     }
130
131     /**
132      * ジェネレータ名を返す。
133      *
134      * @return ジェネレータ名。非表示の場合はnullを返す。
135      */
136     public String getGenerator(){
137         return this.generator;
138     }
139
140     /**
141      * ファイル変換を行う。
142      *
143      * <p>XML入力の場合は{@link #convert(InputSource, OutputStream)}を
144      * 推奨する。
145      *
146      * @param is 入力ストリーム
147      * @param os 出力ストリーム
148      * @throws IOException 入力エラー
149      * @throws MmdFormatException フォーマットエラー
150      * @throws SAXException XMLエラー
151      * @throws TogaXmlException XMLエラー
152      * @throws IllegalPmdDataException 内部エラー
153      */
154     public void convert(InputStream is, OutputStream os)
155             throws IOException,
156                    MmdFormatException,
157                    SAXException,
158                    TogaXmlException,
159                    IllegalPmdDataException {
160         PmdModel model = readModel(is);
161         writeModel(model, os);
162         return;
163     }
164
165     /**
166      * ファイル変換を行う。
167      *
168      * <p>PMD入力の場合は{@link InputStream}に
169      * バイトストリームが直接設定されていなければならない。
170      *
171      * <p>XML入力の場合は{@link InputStream}に
172      * URL(systemId)のみの設定を推奨する。
173      *
174      * @param source 入力ソース
175      * @param os 出力ストリーム
176      * @throws IOException 入力エラー
177      * @throws MmdFormatException フォーマットエラー
178      * @throws SAXException XMLエラー
179      * @throws TogaXmlException XMLエラー
180      * @throws IllegalPmdDataException 内部エラー
181      */
182     public void convert(InputSource source, OutputStream os)
183             throws IOException,
184                    MmdFormatException,
185                    SAXException,
186                    TogaXmlException,
187                    IllegalPmdDataException {
188         PmdModel model = readModel(source);
189         writeModel(model, os);
190         return;
191     }
192
193     /**
194      * モデルファイルを読み込む。
195      *
196      * <p>XML読み込みの場合は、
197      * こちらより{@link #readModel(InputSource)}版を推奨する。
198      *
199      * @param is 入力ストリーム
200      * @return モデルデータ
201      * @throws IOException 入力エラー
202      * @throws MmdFormatException フォーマットエラー
203      * @throws SAXException XMLエラー
204      * @throws TogaXmlException XMLエラー
205      */
206     public PmdModel readModel(InputStream is)
207             throws IOException,
208                    MmdFormatException,
209                    SAXException,
210                    TogaXmlException {
211         InputSource source = new InputSource(is);
212
213         PmdModel model;
214
215         try{
216             model = readModel(source);
217         }finally{
218             is.close();
219         }
220
221         return model;
222     }
223
224     /**
225      * モデルファイルを読み込む。
226      *
227      * @param source 入力ソース
228      * @return モデルデータ
229      * @throws IOException 入力エラー
230      * @throws MmdFormatException フォーマットエラー
231      * @throws SAXException XMLエラー
232      * @throws TogaXmlException XMLエラー
233      */
234     public PmdModel readModel(InputSource source)
235             throws IOException,
236                    MmdFormatException,
237                    SAXException,
238                    TogaXmlException {
239         PmdModel model = null;
240
241         if(this.inTypes.isPmd()){
242             InputStream is = XmlInputUtil.openInputSource(source);
243             try{
244                 model = pmdRead(is);
245             }finally{
246                 is.close();
247             }
248         }else if(this.inTypes.isXml()){
249             model = xmlRead(source);
250         }else{
251             throw new IllegalStateException();
252         }
253
254         return model;
255     }
256
257     /**
258      * モデルファイルを出力する。
259      *
260      * @param model モデルデータ
261      * @param os 出力ストリーム
262      * @throws IOException 出力エラー
263      * @throws IllegalPmdDataException データの不備
264      */
265     public void writeModel(PmdModel model, OutputStream os)
266             throws IOException,
267                    IllegalPmdDataException {
268         if(this.outTypes.isPmd()){
269             pmdOut(model, os);
270         }else if(this.outTypes.isXml()){
271             xmlOut(model, os);
272         }else{
273             throw new IllegalStateException();
274         }
275
276         return;
277     }
278
279     /**
280      * PMDファイルからモデルデータを読み込む。
281      *
282      * @param is 入力ストリーム
283      * @return モデルデータ
284      * @throws IOException 入力エラー
285      * @throws MmdFormatException 不正なPMDファイルフォーマット
286      */
287     private PmdModel pmdRead(InputStream is)
288             throws IOException, MmdFormatException{
289         PmdLoader loader = new PmdLoader();
290         PmdModel model = loader.load(is);
291         return model;
292     }
293
294     /**
295      * XMLファイルからモデルデータを読み込む。
296      *
297      * @param source 入力ソース
298      * @return モデルデータ
299      * @throws IOException 入力エラー
300      * @throws SAXException XML構文エラー
301      * @throws TogaXmlException 不正なXMLデータ
302      */
303     private PmdModel xmlRead(InputSource source)
304             throws IOException,
305                    SAXException,
306                    TogaXmlException {
307         XMLReader reader = XmlInputUtil.buildReader(this.inTypes);
308         XmlPmdLoader loader = new XmlPmdLoader(reader);
309         PmdModel model = loader.parse(source);
310         return model;
311     }
312
313     /**
314      * モデルデータをPMDファイルに出力する。
315      *
316      * @param model モデルデータ
317      * @param ostream 出力ストリーム
318      * @throws IOException 出力エラー
319      * @throws IllegalPmdDataException 不正なモデルデータ
320      */
321     private void pmdOut(PmdModel model, OutputStream ostream)
322             throws IOException, IllegalPmdDataException{
323         PmdExporter exporter = new PmdExporter(ostream);
324         exporter.dumpPmdModel(model);
325         ostream.close();
326         return;
327     }
328
329     /**
330      * モデルデータをXMLファイルに出力する。
331      *
332      * @param model モデルデータ
333      * @param ostream 出力ストリーム
334      * @throws IOException 出力エラー
335      * @throws IllegalPmdDataException 不正なモデルデータ
336      */
337     private void xmlOut(PmdModel model, OutputStream ostream)
338             throws IOException, IllegalPmdDataException{
339         PmdXmlExporter exporter = new PmdXmlExporter();
340
341         XmlModelFileType xmlType = this.outTypes.toXmlType();
342         exporter.setXmlFileType(xmlType);
343         exporter.setNewLine(this.newLine);
344         exporter.setGenerator(this.generator);
345
346         Writer writer;
347         writer = new OutputStreamWriter(ostream, CS_UTF8);
348         writer = new BufferedWriter(writer);
349
350         exporter.putPmdXml(model, writer);
351
352         exporter.close();
353
354         return;
355     }
356
357 }