4 * License : The MIT License
5 * Copyright(c) 2010 MikuToga Partners
8 package jp.sfjp.mikutoga.pmd2xml;
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;
33 public class Pmd2XmlConv {
36 private static final Charset CS_UTF8 = Charset.forName("UTF-8");
39 private ModelFileType inTypes = ModelFileType.NONE;
40 private ModelFileType outTypes = ModelFileType.NONE;
41 private String newLine = "\r\n";
42 private String generator = null;
58 * @throws NullPointerException 引数がnull
59 * @throws IllegalArgumentException 具体的な種別を渡さなかった
61 public void setInType(ModelFileType type)
62 throws NullPointerException, IllegalArgumentException {
63 if(type == null) throw new NullPointerException();
64 if(type == ModelFileType.NONE) throw new IllegalArgumentException();
74 public ModelFileType getInTypes(){
82 * @throws NullPointerException 引数がnull
83 * @throws IllegalArgumentException 具体的な種別を渡さなかった
85 public void setOutType(ModelFileType type)
86 throws NullPointerException, IllegalArgumentException {
87 if(type == null) throw new NullPointerException();
88 if(type == ModelFileType.NONE) throw new IllegalArgumentException();
98 public ModelFileType getOutTypes(){
105 * @param newline 改行文字
107 public void setNewline(String newline){
108 this.newLine = newline;
117 public String getNewline(){
124 * @param generator ジェネレータ名。表示したくない場合はnull
126 public void setGenerator(String generator){
127 this.generator = generator;
134 * @return ジェネレータ名。非表示の場合はnullを返す。
136 public String getGenerator(){
137 return this.generator;
143 * <p>XML入力の場合は{@link #convert(InputSource, OutputStream)}を
148 * @throws IOException 入力エラー
149 * @throws MmdFormatException フォーマットエラー
150 * @throws SAXException XMLエラー
151 * @throws TogaXmlException XMLエラー
152 * @throws IllegalPmdDataException 内部エラー
154 public void convert(InputStream is, OutputStream os)
159 IllegalPmdDataException {
160 PmdModel model = readModel(is);
161 writeModel(model, os);
168 * <p>PMD入力の場合は{@link InputStream}に
169 * バイトストリームが直接設定されていなければならない。
171 * <p>XML入力の場合は{@link InputStream}に
172 * URL(systemId)のみの設定を推奨する。
174 * @param source 入力ソース
176 * @throws IOException 入力エラー
177 * @throws MmdFormatException フォーマットエラー
178 * @throws SAXException XMLエラー
179 * @throws TogaXmlException XMLエラー
180 * @throws IllegalPmdDataException 内部エラー
182 public void convert(InputSource source, OutputStream os)
187 IllegalPmdDataException {
188 PmdModel model = readModel(source);
189 writeModel(model, os);
197 * こちらより{@link #readModel(InputSource)}版を推奨する。
201 * @throws IOException 入力エラー
202 * @throws MmdFormatException フォーマットエラー
203 * @throws SAXException XMLエラー
204 * @throws TogaXmlException XMLエラー
206 public PmdModel readModel(InputStream is)
211 InputSource source = new InputSource(is);
216 model = readModel(source);
227 * @param source 入力ソース
229 * @throws IOException 入力エラー
230 * @throws MmdFormatException フォーマットエラー
231 * @throws SAXException XMLエラー
232 * @throws TogaXmlException XMLエラー
234 public PmdModel readModel(InputSource source)
239 PmdModel model = null;
241 if(this.inTypes.isPmd()){
242 InputStream is = XmlInputUtil.openInputSource(source);
248 }else if(this.inTypes.isXml()){
249 model = xmlRead(source);
251 throw new IllegalStateException();
260 * @param model モデルデータ
262 * @throws IOException 出力エラー
263 * @throws IllegalPmdDataException データの不備
265 public void writeModel(PmdModel model, OutputStream os)
267 IllegalPmdDataException {
268 if(this.outTypes.isPmd()){
270 }else if(this.outTypes.isXml()){
273 throw new IllegalStateException();
280 * PMDファイルからモデルデータを読み込む。
284 * @throws IOException 入力エラー
285 * @throws MmdFormatException 不正なPMDファイルフォーマット
287 private PmdModel pmdRead(InputStream is)
288 throws IOException, MmdFormatException{
289 PmdLoader loader = new PmdLoader();
290 PmdModel model = loader.load(is);
295 * XMLファイルからモデルデータを読み込む。
297 * @param source 入力ソース
299 * @throws IOException 入力エラー
300 * @throws SAXException XML構文エラー
301 * @throws TogaXmlException 不正なXMLデータ
303 private PmdModel xmlRead(InputSource source)
307 XMLReader reader = XmlInputUtil.buildReader(this.inTypes);
308 XmlPmdLoader loader = new XmlPmdLoader(reader);
309 PmdModel model = loader.parse(source);
314 * モデルデータをPMDファイルに出力する。
316 * @param model モデルデータ
317 * @param ostream 出力ストリーム
318 * @throws IOException 出力エラー
319 * @throws IllegalPmdDataException 不正なモデルデータ
321 private void pmdOut(PmdModel model, OutputStream ostream)
322 throws IOException, IllegalPmdDataException{
323 PmdExporter exporter = new PmdExporter(ostream);
324 exporter.dumpPmdModel(model);
330 * モデルデータをXMLファイルに出力する。
332 * @param model モデルデータ
333 * @param ostream 出力ストリーム
334 * @throws IOException 出力エラー
335 * @throws IllegalPmdDataException 不正なモデルデータ
337 private void xmlOut(PmdModel model, OutputStream ostream)
338 throws IOException, IllegalPmdDataException{
339 PmdXmlExporter exporter = new PmdXmlExporter();
341 XmlModelFileType xmlType = this.outTypes.toXmlType();
342 exporter.setXmlFileType(xmlType);
343 exporter.setNewLine(this.newLine);
344 exporter.setGenerator(this.generator);
347 writer = new OutputStreamWriter(ostream, CS_UTF8);
348 writer = new BufferedWriter(writer);
350 exporter.putPmdXml(model, writer);