OSDN Git Service

XML出力改善
[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.sourceforge.mikutoga.parser.MmdFormatException;
16 import jp.sourceforge.mikutoga.pmd.IllegalPmdDataException;
17 import jp.sourceforge.mikutoga.pmd.model.PmdModel;
18 import jp.sourceforge.mikutoga.pmd.model.binio.PmdExporter;
19 import jp.sourceforge.mikutoga.pmd.model.binio.PmdLoader;
20 import jp.sourceforge.mikutoga.pmd.model.xml.PmdXmlExporter;
21 import jp.sourceforge.mikutoga.pmd.model.xml.PmdXmlResources;
22 import jp.sourceforge.mikutoga.pmd.model.xml.Xml2PmdLoader;
23 import jp.sourceforge.mikutoga.xml.TogaXmlException;
24 import org.xml.sax.InputSource;
25 import org.xml.sax.SAXException;
26
27 /**
28  * PMD-XML間コンバータ本体。
29  */
30 public class Pmd2XmlConv {
31
32     private ModelFileTypes inTypes  = ModelFileTypes.NONE;
33     private ModelFileTypes outTypes = ModelFileTypes.NONE;
34     private String newLine = "\r\n";
35     private String generator = null;
36
37     private final DocumentBuilder builder;
38
39
40     /**
41      * コンストラクタ。
42      */
43     public Pmd2XmlConv(){
44         super();
45
46         try{
47             this.builder = PmdXmlResources.newBuilder(XmlHandler.HANDLER);
48         }catch(SAXException e){
49             throw new AssertionError(e);
50         }catch(ParserConfigurationException e){
51             throw new AssertionError(e);
52         }
53
54         return;
55     }
56
57
58     /**
59      * 入力ファイル種別を設定する。
60      * @param type ファイル種別
61      */
62     public void setInType(ModelFileTypes type){
63         if(type == null) throw new NullPointerException();
64         this.inTypes = type;
65         return;
66     }
67
68     /**
69      * 入力ファイル種別を返す。
70      * @return ファイル種別
71      */
72     public ModelFileTypes getInTypes(){
73         return this.inTypes;
74     }
75
76     /**
77      * 出力ファイル種別を設定する。
78      * @param type ファイル種別
79      */
80     public void setOutType(ModelFileTypes type){
81         if(type == null) throw new NullPointerException();
82         this.outTypes = type;
83         return;
84     }
85
86     /**
87      * 出力ファイル種別を返す。
88      * @return ファイル種別
89      */
90     public ModelFileTypes getOutTypes(){
91         return this.outTypes;
92     }
93
94     /**
95      * XML出力用改行文字列を設定する。
96      * @param newline 改行文字
97      */
98     public void setNewline(String newline){
99         this.newLine = newline;
100         return;
101     }
102
103     /**
104      * XML出力用改行文字列を返す。
105      * @return 改行文字
106      */
107     public String getNewLine(){
108         return this.newLine;
109     }
110
111     /**
112      * ジェネレータ名を設定する。
113      * @param generator ジェネレータ名。表示したくない場合はnull
114      */
115     public void setGenerator(String generator){
116         this.generator = generator;
117         return;
118     }
119
120     /**
121      * ジェネレータ名を返す。
122      * @return ジェネレータ名。非表示の場合はnullを返す。
123      */
124     public String getGenerator(){
125         return this.generator;
126     }
127
128     /**
129      * ファイル変換を行う。
130      * @param is 入力ストリーム
131      * @param os 出力ストリーム
132      * @throws IOException 入力エラー
133      * @throws MmdFormatException フォーマットエラー
134      * @throws SAXException XMLエラー
135      * @throws TogaXmlException XMLエラー
136      * @throws IllegalPmdDataException 内部エラー
137      */
138     public void convert(InputStream is, OutputStream os)
139             throws IOException,
140                    MmdFormatException,
141                    SAXException,
142                    TogaXmlException,
143                    IllegalPmdDataException {
144         PmdModel model = readModel(is);
145         writeModel(model, os);
146         return;
147     }
148
149     /**
150      * モデルファイルを読み込む。
151      * @param is 入力ストリーム
152      * @return モデルデータ
153      * @throws IOException 入力エラー
154      * @throws MmdFormatException フォーマットエラー
155      * @throws SAXException XMLエラー
156      * @throws TogaXmlException XMLエラー
157      */
158     public PmdModel readModel(InputStream is)
159             throws IOException,
160                    MmdFormatException,
161                    SAXException,
162                    TogaXmlException {
163         PmdModel model = null;
164         switch(this.inTypes){
165         case PMD:
166             model = pmdRead(is);
167             break;
168         case XML_101009:
169             model = xmlRead(is);
170             break;
171         default:
172             assert false;
173             break;
174         }
175         return model;
176     }
177
178     /**
179      * モデルファイルを出力する。
180      * @param model モデルデータ
181      * @param os 出力ストリーム
182      * @throws IOException 出力エラー
183      * @throws IllegalPmdDataException データの不備
184      */
185     public void writeModel(PmdModel model, OutputStream os)
186             throws IOException,
187                    IllegalPmdDataException {
188         switch(this.outTypes){
189         case PMD:
190             pmdOut(model, os);
191             break;
192         case XML_101009:
193             xmlOut(model, os);
194             break;
195         default:
196             assert false;
197             break;
198         }
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(is);
211         PmdModel model = loader.load();
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         Xml2PmdLoader loader = new Xml2PmdLoader(this.builder);
245         PmdModel model = loader.parse(source);
246         return model;
247     }
248
249     /**
250      * モデルデータをPMDファイルに出力する。
251      * @param model モデルデータ
252      * @param ostream 出力ストリーム
253      * @throws IOException 出力エラー
254      * @throws IllegalPmdDataException 不正なモデルデータ
255      */
256     private void pmdOut(PmdModel model, OutputStream ostream)
257             throws IOException, IllegalPmdDataException{
258         PmdExporter exporter = new PmdExporter(ostream);
259         exporter.dumpPmdModel(model);
260         ostream.close();
261         return;
262     }
263
264     /**
265      * モデルデータをXMLファイルに出力する。
266      * @param model モデルデータ
267      * @param ostream 出力ストリーム
268      * @throws IOException 出力エラー
269      * @throws IllegalPmdDataException 不正なモデルデータ
270      */
271     private void xmlOut(PmdModel model, OutputStream ostream)
272             throws IOException, IllegalPmdDataException{
273         PmdXmlExporter exporter = new PmdXmlExporter(ostream);
274         exporter.setNewLine(this.newLine);
275         exporter.setGenerator(this.generator);
276         exporter.putPmdModel(model);
277         exporter.close();
278         return;
279     }
280
281 }