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.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;
31 public class Pmd2XmlConv {
33 private ModelFileType inTypes = ModelFileType.NONE;
34 private ModelFileType outTypes = ModelFileType.NONE;
35 private String newLine = "\r\n";
36 private String generator = null;
38 private final DocumentBuilder builder;
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);
63 public void setInType(ModelFileType type){
64 if(type == null) throw new NullPointerException();
73 public ModelFileType getInTypes(){
81 public void setOutType(ModelFileType type){
82 if(type == null) throw new NullPointerException();
91 public ModelFileType getOutTypes(){
99 public void setNewline(String newline){
100 this.newLine = newline;
108 public String getNewLine(){
114 * @param generator ジェネレータ名。表示したくない場合はnull
116 public void setGenerator(String generator){
117 this.generator = generator;
123 * @return ジェネレータ名。非表示の場合はnullを返す。
125 public String getGenerator(){
126 return this.generator;
133 * @throws IOException 入力エラー
134 * @throws MmdFormatException フォーマットエラー
135 * @throws SAXException XMLエラー
136 * @throws TogaXmlException XMLエラー
137 * @throws IllegalPmdDataException 内部エラー
139 public void convert(InputStream is, OutputStream os)
144 IllegalPmdDataException {
145 PmdModel model = readModel(is);
146 writeModel(model, os);
154 * @throws IOException 入力エラー
155 * @throws MmdFormatException フォーマットエラー
156 * @throws SAXException XMLエラー
157 * @throws TogaXmlException XMLエラー
159 public PmdModel readModel(InputStream is)
164 PmdModel model = null;
165 switch(this.inTypes){
181 * @param model モデルデータ
183 * @throws IOException 出力エラー
184 * @throws IllegalPmdDataException データの不備
186 public void writeModel(PmdModel model, OutputStream os)
188 IllegalPmdDataException {
189 switch(this.outTypes){
203 * PMDファイルからモデルデータを読み込む。
206 * @throws IOException 入力エラー
207 * @throws MmdFormatException 不正なPMDファイルフォーマット
209 private PmdModel pmdRead(InputStream is)
210 throws IOException, MmdFormatException{
211 PmdLoader loader = new PmdLoader(is);
212 PmdModel model = loader.load();
217 * XMLファイルからモデルデータを読み込む。
220 * @throws IOException 入力エラー
221 * @throws SAXException XML構文エラー
222 * @throws TogaXmlException 不正なXMLデータ
224 private PmdModel xmlRead(InputStream is)
228 InputSource source = new InputSource(is);
229 PmdModel result = xmlRead(source);
234 * XMLファイルからモデルデータを読み込む。
235 * @param source 入力ソース
237 * @throws IOException 入力エラー
238 * @throws SAXException XML構文エラー
239 * @throws TogaXmlException 不正なXMLデータ
241 private PmdModel xmlRead(InputSource source)
245 Xml101009Loader loader = new Xml101009Loader(this.builder);
246 PmdModel model = loader.parse(source);
251 * モデルデータをPMDファイルに出力する。
252 * @param model モデルデータ
253 * @param ostream 出力ストリーム
254 * @throws IOException 出力エラー
255 * @throws IllegalPmdDataException 不正なモデルデータ
257 private void pmdOut(PmdModel model, OutputStream ostream)
258 throws IOException, IllegalPmdDataException{
259 PmdExporter exporter = new PmdExporter(ostream);
260 exporter.dumpPmdModel(model);
266 * モデルデータをXMLファイルに出力する。
267 * @param model モデルデータ
268 * @param ostream 出力ストリーム
269 * @throws IOException 出力エラー
270 * @throws IllegalPmdDataException 不正なモデルデータ
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);