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;
57 * @throws NullPointerException 引数がnull
58 * @throws IllegalArgumentException 具体的な種別を渡さなかった
60 public void setInType(ModelFileType type)
61 throws NullPointerException, IllegalArgumentException {
62 if(type == null) throw new NullPointerException();
63 if(type == ModelFileType.NONE) throw new IllegalArgumentException();
72 public ModelFileType getInTypes(){
79 * @throws NullPointerException 引数がnull
80 * @throws IllegalArgumentException 具体的な種別を渡さなかった
82 public void setOutType(ModelFileType type)
83 throws NullPointerException, IllegalArgumentException {
84 if(type == null) throw new NullPointerException();
85 if(type == ModelFileType.NONE) throw new IllegalArgumentException();
94 public ModelFileType getOutTypes(){
100 * @param newline 改行文字
102 public void setNewline(String newline){
103 this.newLine = newline;
111 public String getNewline(){
117 * @param generator ジェネレータ名。表示したくない場合はnull
119 public void setGenerator(String generator){
120 this.generator = generator;
126 * @return ジェネレータ名。非表示の場合はnullを返す。
128 public String getGenerator(){
129 return this.generator;
136 * @throws IOException 入力エラー
137 * @throws MmdFormatException フォーマットエラー
138 * @throws SAXException XMLエラー
139 * @throws TogaXmlException XMLエラー
140 * @throws IllegalPmdDataException 内部エラー
142 public void convert(InputStream is, OutputStream os)
147 IllegalPmdDataException {
148 PmdModel model = readModel(is);
149 writeModel(model, os);
157 * @throws IOException 入力エラー
158 * @throws MmdFormatException フォーマットエラー
159 * @throws SAXException XMLエラー
160 * @throws TogaXmlException XMLエラー
162 public PmdModel readModel(InputStream is)
167 PmdModel model = null;
169 if(this.inTypes.isPmd()){
171 }else if(this.inTypes.isXml()){
174 throw new IllegalStateException();
182 * @param model モデルデータ
184 * @throws IOException 出力エラー
185 * @throws IllegalPmdDataException データの不備
187 public void writeModel(PmdModel model, OutputStream os)
189 IllegalPmdDataException {
190 if(this.outTypes.isPmd()){
192 }else if(this.outTypes.isXml()){
195 throw new IllegalStateException();
202 * PMDファイルからモデルデータを読み込む。
205 * @throws IOException 入力エラー
206 * @throws MmdFormatException 不正なPMDファイルフォーマット
208 private PmdModel pmdRead(InputStream is)
209 throws IOException, MmdFormatException{
210 PmdLoader loader = new PmdLoader();
211 PmdModel model = loader.load(is);
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 XMLReader reader = XmlInputUtil.buildReader(this.inTypes);
245 XmlPmdLoader loader = new XmlPmdLoader(reader);
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 PmdXmlExporter exporter = new PmdXmlExporter();
276 XmlModelFileType xmlType = this.outTypes.toXmlType();
277 exporter.setXmlFileType(xmlType);
278 exporter.setNewLine(this.newLine);
279 exporter.setGenerator(this.generator);
282 writer = new OutputStreamWriter(ostream, CS_UTF8);
283 writer = new BufferedWriter(writer);
285 exporter.putPmdXml(model, writer);