OSDN Git Service

checkstyle対応
[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      * @param is 入力ストリーム
135      * @param os 出力ストリーム
136      * @throws IOException 入力エラー
137      * @throws MmdFormatException フォーマットエラー
138      * @throws SAXException XMLエラー
139      * @throws TogaXmlException XMLエラー
140      * @throws IllegalPmdDataException 内部エラー
141      */
142     public void convert(InputStream is, OutputStream os)
143             throws IOException,
144                    MmdFormatException,
145                    SAXException,
146                    TogaXmlException,
147                    IllegalPmdDataException {
148         PmdModel model = readModel(is);
149         writeModel(model, os);
150         return;
151     }
152
153     /**
154      * モデルファイルを読み込む。
155      * @param is 入力ストリーム
156      * @return モデルデータ
157      * @throws IOException 入力エラー
158      * @throws MmdFormatException フォーマットエラー
159      * @throws SAXException XMLエラー
160      * @throws TogaXmlException XMLエラー
161      */
162     public PmdModel readModel(InputStream is)
163             throws IOException,
164                    MmdFormatException,
165                    SAXException,
166                    TogaXmlException {
167         PmdModel model = null;
168
169         if(this.inTypes.isPmd()){
170             model = pmdRead(is);
171         }else if(this.inTypes.isXml()){
172             model = xmlRead(is);
173         }else{
174             throw new IllegalStateException();
175         }
176
177         return model;
178     }
179
180     /**
181      * モデルファイルを出力する。
182      * @param model モデルデータ
183      * @param os 出力ストリーム
184      * @throws IOException 出力エラー
185      * @throws IllegalPmdDataException データの不備
186      */
187     public void writeModel(PmdModel model, OutputStream os)
188             throws IOException,
189                    IllegalPmdDataException {
190         if(this.outTypes.isPmd()){
191             pmdOut(model, os);
192         }else if(this.outTypes.isXml()){
193             xmlOut(model, os);
194         }else{
195             throw new IllegalStateException();
196         }
197
198         return;
199     }
200
201     /**
202      * PMDファイルからモデルデータを読み込む。
203      * @param is 入力ストリーム
204      * @return モデルデータ
205      * @throws IOException 入力エラー
206      * @throws MmdFormatException 不正なPMDファイルフォーマット
207      */
208     private PmdModel pmdRead(InputStream is)
209             throws IOException, MmdFormatException{
210         PmdLoader loader = new PmdLoader();
211         PmdModel model = loader.load(is);
212         return model;
213     }
214
215     /**
216      * XMLファイルからモデルデータを読み込む。
217      * @param is 入力ストリーム
218      * @return モデルデータ
219      * @throws IOException 入力エラー
220      * @throws SAXException XML構文エラー
221      * @throws TogaXmlException 不正なXMLデータ
222      */
223     private PmdModel xmlRead(InputStream is)
224             throws IOException,
225                    SAXException,
226                    TogaXmlException {
227         InputSource source = new InputSource(is);
228         PmdModel result = xmlRead(source);
229         return result;
230     }
231
232     /**
233      * XMLファイルからモデルデータを読み込む。
234      * @param source 入力ソース
235      * @return モデルデータ
236      * @throws IOException 入力エラー
237      * @throws SAXException XML構文エラー
238      * @throws TogaXmlException 不正なXMLデータ
239      */
240     private PmdModel xmlRead(InputSource source)
241             throws IOException,
242                    SAXException,
243                    TogaXmlException {
244         XMLReader reader = XmlInputUtil.buildReader(this.inTypes);
245         XmlPmdLoader loader = new XmlPmdLoader(reader);
246         PmdModel model = loader.parse(source);
247         return model;
248     }
249
250     /**
251      * モデルデータをPMDファイルに出力する。
252      * @param model モデルデータ
253      * @param ostream 出力ストリーム
254      * @throws IOException 出力エラー
255      * @throws IllegalPmdDataException 不正なモデルデータ
256      */
257     private void pmdOut(PmdModel model, OutputStream ostream)
258             throws IOException, IllegalPmdDataException{
259         PmdExporter exporter = new PmdExporter(ostream);
260         exporter.dumpPmdModel(model);
261         ostream.close();
262         return;
263     }
264
265     /**
266      * モデルデータをXMLファイルに出力する。
267      * @param model モデルデータ
268      * @param ostream 出力ストリーム
269      * @throws IOException 出力エラー
270      * @throws IllegalPmdDataException 不正なモデルデータ
271      */
272     private void xmlOut(PmdModel model, OutputStream ostream)
273             throws IOException, IllegalPmdDataException{
274         PmdXmlExporter exporter = new PmdXmlExporter();
275
276         XmlModelFileType xmlType = this.outTypes.toXmlType();
277         exporter.setXmlFileType(xmlType);
278         exporter.setNewLine(this.newLine);
279         exporter.setGenerator(this.generator);
280
281         Writer writer;
282         writer = new OutputStreamWriter(ostream, CS_UTF8);
283         writer = new BufferedWriter(writer);
284
285         exporter.putPmdXml(model, writer);
286
287         exporter.close();
288
289         return;
290     }
291
292 }