OSDN Git Service

テストセット整備
[mikutoga/Pmd2XML.git] / src / main / java / jp / sourceforge / 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.sourceforge.mikutoga.pmd2xml;
9
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.OutputStream;
13 import javax.xml.parsers.DocumentBuilder;
14 import javax.xml.parsers.ParserConfigurationException;
15 import jp.sfjp.mikutoga.pmd.xml101009.Xml101009Exporter;
16 import jp.sfjp.mikutoga.pmd.xml101009.Xml101009Loader;
17 import jp.sfjp.mikutoga.pmd.xml101009.Xml101009Resources;
18 import jp.sourceforge.mikutoga.parser.MmdFormatException;
19 import jp.sourceforge.mikutoga.pmd.IllegalPmdDataException;
20 import jp.sourceforge.mikutoga.pmd.ModelFileType;
21 import jp.sourceforge.mikutoga.pmd.model.PmdModel;
22 import jp.sourceforge.mikutoga.pmd.model.binio.PmdExporter;
23 import jp.sourceforge.mikutoga.pmd.model.binio.PmdLoader;
24 import jp.sourceforge.mikutoga.xml.TogaXmlException;
25 import org.xml.sax.InputSource;
26 import org.xml.sax.SAXException;
27
28 /**
29  * PMD-XML間コンバータ本体。
30  */
31 public class Pmd2XmlConv {
32
33     private ModelFileType inTypes  = ModelFileType.NONE;
34     private ModelFileType outTypes = ModelFileType.NONE;
35     private String newLine = "\r\n";
36     private String generator = null;
37
38     private final DocumentBuilder builder;
39
40
41     /**
42      * コンストラクタ。
43      */
44     public Pmd2XmlConv(){
45         super();
46
47         try{
48             this.builder = Xml101009Resources.newBuilder(XmlHandler.HANDLER);
49         }catch(SAXException e){
50             throw new AssertionError(e);
51         }catch(ParserConfigurationException e){
52             throw new AssertionError(e);
53         }
54
55         return;
56     }
57
58
59     /**
60      * 入力ファイル種別を設定する。
61      * @param type ファイル種別
62      */
63     public void setInType(ModelFileType type){
64         if(type == null) throw new NullPointerException();
65         this.inTypes = type;
66         return;
67     }
68
69     /**
70      * 入力ファイル種別を返す。
71      * @return ファイル種別
72      */
73     public ModelFileType getInTypes(){
74         return this.inTypes;
75     }
76
77     /**
78      * 出力ファイル種別を設定する。
79      * @param type ファイル種別
80      */
81     public void setOutType(ModelFileType type){
82         if(type == null) throw new NullPointerException();
83         this.outTypes = type;
84         return;
85     }
86
87     /**
88      * 出力ファイル種別を返す。
89      * @return ファイル種別
90      */
91     public ModelFileType getOutTypes(){
92         return this.outTypes;
93     }
94
95     /**
96      * XML出力用改行文字列を設定する。
97      * @param newline 改行文字
98      */
99     public void setNewline(String newline){
100         this.newLine = newline;
101         return;
102     }
103
104     /**
105      * XML出力用改行文字列を返す。
106      * @return 改行文字
107      */
108     public String getNewLine(){
109         return this.newLine;
110     }
111
112     /**
113      * ジェネレータ名を設定する。
114      * @param generator ジェネレータ名。表示したくない場合はnull
115      */
116     public void setGenerator(String generator){
117         this.generator = generator;
118         return;
119     }
120
121     /**
122      * ジェネレータ名を返す。
123      * @return ジェネレータ名。非表示の場合はnullを返す。
124      */
125     public String getGenerator(){
126         return this.generator;
127     }
128
129     /**
130      * ファイル変換を行う。
131      * @param is 入力ストリーム
132      * @param os 出力ストリーム
133      * @throws IOException 入力エラー
134      * @throws MmdFormatException フォーマットエラー
135      * @throws SAXException XMLエラー
136      * @throws TogaXmlException XMLエラー
137      * @throws IllegalPmdDataException 内部エラー
138      */
139     public void convert(InputStream is, OutputStream os)
140             throws IOException,
141                    MmdFormatException,
142                    SAXException,
143                    TogaXmlException,
144                    IllegalPmdDataException {
145         PmdModel model = readModel(is);
146         writeModel(model, os);
147         return;
148     }
149
150     /**
151      * モデルファイルを読み込む。
152      * @param is 入力ストリーム
153      * @return モデルデータ
154      * @throws IOException 入力エラー
155      * @throws MmdFormatException フォーマットエラー
156      * @throws SAXException XMLエラー
157      * @throws TogaXmlException XMLエラー
158      */
159     public PmdModel readModel(InputStream is)
160             throws IOException,
161                    MmdFormatException,
162                    SAXException,
163                    TogaXmlException {
164         PmdModel model = null;
165         switch(this.inTypes){
166         case PMD:
167             model = pmdRead(is);
168             break;
169         case XML_101009:
170             model = xmlRead(is);
171             break;
172         default:
173             assert false;
174             break;
175         }
176         return model;
177     }
178
179     /**
180      * モデルファイルを出力する。
181      * @param model モデルデータ
182      * @param os 出力ストリーム
183      * @throws IOException 出力エラー
184      * @throws IllegalPmdDataException データの不備
185      */
186     public void writeModel(PmdModel model, OutputStream os)
187             throws IOException,
188                    IllegalPmdDataException {
189         switch(this.outTypes){
190         case PMD:
191             pmdOut(model, os);
192             break;
193         case XML_101009:
194             xmlOut(model, os);
195             break;
196         default:
197             assert false;
198             break;
199         }
200     }
201
202     /**
203      * PMDファイルからモデルデータを読み込む。
204      * @param is 入力ストリーム
205      * @return モデルデータ
206      * @throws IOException 入力エラー
207      * @throws MmdFormatException 不正なPMDファイルフォーマット
208      */
209     private PmdModel pmdRead(InputStream is)
210             throws IOException, MmdFormatException{
211         PmdLoader loader = new PmdLoader(is);
212         PmdModel model = loader.load();
213         return model;
214     }
215
216     /**
217      * XMLファイルからモデルデータを読み込む。
218      * @param is 入力ストリーム
219      * @return モデルデータ
220      * @throws IOException 入力エラー
221      * @throws SAXException XML構文エラー
222      * @throws TogaXmlException 不正なXMLデータ
223      */
224     private PmdModel xmlRead(InputStream is)
225             throws IOException,
226                    SAXException,
227                    TogaXmlException {
228         InputSource source = new InputSource(is);
229         PmdModel result = xmlRead(source);
230         return result;
231     }
232
233     /**
234      * XMLファイルからモデルデータを読み込む。
235      * @param source 入力ソース
236      * @return モデルデータ
237      * @throws IOException 入力エラー
238      * @throws SAXException XML構文エラー
239      * @throws TogaXmlException 不正なXMLデータ
240      */
241     private PmdModel xmlRead(InputSource source)
242             throws IOException,
243                    SAXException,
244                    TogaXmlException {
245         Xml101009Loader loader = new Xml101009Loader(this.builder);
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         Xml101009Exporter exporter = new Xml101009Exporter(ostream);
275         exporter.setNewLine(this.newLine);
276         exporter.setGenerator(this.generator);
277         exporter.putPmdModel(model);
278         exporter.close();
279         return;
280     }
281
282 }