4 * License : The MIT License
5 * Copyright(c) 2010 MikuToga Partners
8 package jp.sourceforge.mikutoga.pmd2xml;
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;
30 public class Pmd2XmlConv {
32 private ModelFileTypes inTypes = ModelFileTypes.NONE;
33 private ModelFileTypes outTypes = ModelFileTypes.NONE;
34 private String newLine = "\r\n";
35 private String generator = null;
37 private final DocumentBuilder builder;
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);
62 public void setInType(ModelFileTypes type){
63 if(type == null) throw new NullPointerException();
72 public ModelFileTypes getInTypes(){
80 public void setOutType(ModelFileTypes type){
81 if(type == null) throw new NullPointerException();
90 public ModelFileTypes getOutTypes(){
98 public void setNewline(String newline){
99 this.newLine = newline;
107 public String getNewLine(){
113 * @param generator ジェネレータ名。表示したくない場合はnull
115 public void setGenerator(String generator){
116 this.generator = generator;
122 * @return ジェネレータ名。非表示の場合はnullを返す。
124 public String getGenerator(){
125 return this.generator;
132 * @throws IOException 入力エラー
133 * @throws MmdFormatException フォーマットエラー
134 * @throws SAXException XMLエラー
135 * @throws TogaXmlException XMLエラー
136 * @throws IllegalPmdDataException 内部エラー
138 public void convert(InputStream is, OutputStream os)
143 IllegalPmdDataException {
144 PmdModel model = readModel(is);
145 writeModel(model, os);
153 * @throws IOException 入力エラー
154 * @throws MmdFormatException フォーマットエラー
155 * @throws SAXException XMLエラー
156 * @throws TogaXmlException XMLエラー
158 public PmdModel readModel(InputStream is)
163 PmdModel model = null;
164 switch(this.inTypes){
180 * @param model モデルデータ
182 * @throws IOException 出力エラー
183 * @throws IllegalPmdDataException データの不備
185 public void writeModel(PmdModel model, OutputStream os)
187 IllegalPmdDataException {
188 switch(this.outTypes){
202 * PMDファイルからモデルデータを読み込む。
205 * @throws IOException 入力エラー
206 * @throws MmdFormatException 不正なPMDファイルフォーマット
208 private PmdModel pmdRead(InputStream is)
209 throws IOException, MmdFormatException{
210 PmdLoader loader = new PmdLoader(is);
211 PmdModel model = loader.load();
216 * XMLファイルからモデルデータを読み込む。
219 * @throws IOException 入力エラー
220 * @throws SAXException XML構文エラー
221 * @throws TogaXmlException 不正なXMLデータ
223 private PmdModel xmlRead(InputStream is)
227 InputSource source = new InputSource(is);
228 PmdModel result = xmlRead(source);
233 * XMLファイルからモデルデータを読み込む。
234 * @param source 入力ソース
236 * @throws IOException 入力エラー
237 * @throws SAXException XML構文エラー
238 * @throws TogaXmlException 不正なXMLデータ
240 private PmdModel xmlRead(InputSource source)
244 Xml2PmdLoader loader = new Xml2PmdLoader(this.builder);
245 PmdModel model = loader.parse(source);
250 * モデルデータをPMDファイルに出力する。
251 * @param model モデルデータ
252 * @param ostream 出力ストリーム
253 * @throws IOException 出力エラー
254 * @throws IllegalPmdDataException 不正なモデルデータ
256 private void pmdOut(PmdModel model, OutputStream ostream)
257 throws IOException, IllegalPmdDataException{
258 PmdExporter exporter = new PmdExporter(ostream);
259 exporter.dumpPmdModel(model);
265 * モデルデータをXMLファイルに出力する。
266 * @param model モデルデータ
267 * @param ostream 出力ストリーム
268 * @throws IOException 出力エラー
269 * @throws IllegalPmdDataException 不正なモデルデータ
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);