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;
134 * <p>XML入力の場合は{@link #convert(InputSource, OutputStream)}を
138 * @throws IOException 入力エラー
139 * @throws MmdFormatException フォーマットエラー
140 * @throws SAXException XMLエラー
141 * @throws TogaXmlException XMLエラー
142 * @throws IllegalPmdDataException 内部エラー
144 public void convert(InputStream is, OutputStream os)
149 IllegalPmdDataException {
150 PmdModel model = readModel(is);
151 writeModel(model, os);
157 * <p>PMD入力の場合は{@link InputStream}に
158 * バイトストリームが直接設定されていなければならない。
159 * <p>XML入力の場合は{@link InputStream}に
160 * URL(systemId)のみの設定を推奨する。
161 * @param source 入力ソース
163 * @throws IOException 入力エラー
164 * @throws MmdFormatException フォーマットエラー
165 * @throws SAXException XMLエラー
166 * @throws TogaXmlException XMLエラー
167 * @throws IllegalPmdDataException 内部エラー
169 public void convert(InputSource source, OutputStream os)
174 IllegalPmdDataException {
175 PmdModel model = readModel(source);
176 writeModel(model, os);
183 * こちらより{@link #readModel(InputSource)}版を推奨する。
186 * @throws IOException 入力エラー
187 * @throws MmdFormatException フォーマットエラー
188 * @throws SAXException XMLエラー
189 * @throws TogaXmlException XMLエラー
191 public PmdModel readModel(InputStream is)
196 InputSource source = new InputSource(is);
201 model = readModel(source);
211 * @param source 入力ソース
213 * @throws IOException 入力エラー
214 * @throws MmdFormatException フォーマットエラー
215 * @throws SAXException XMLエラー
216 * @throws TogaXmlException XMLエラー
218 public PmdModel readModel(InputSource source)
223 PmdModel model = null;
225 if(this.inTypes.isPmd()){
226 InputStream is = XmlInputUtil.openInputSource(source);
232 }else if(this.inTypes.isXml()){
233 model = xmlRead(source);
235 throw new IllegalStateException();
243 * @param model モデルデータ
245 * @throws IOException 出力エラー
246 * @throws IllegalPmdDataException データの不備
248 public void writeModel(PmdModel model, OutputStream os)
250 IllegalPmdDataException {
251 if(this.outTypes.isPmd()){
253 }else if(this.outTypes.isXml()){
256 throw new IllegalStateException();
263 * PMDファイルからモデルデータを読み込む。
266 * @throws IOException 入力エラー
267 * @throws MmdFormatException 不正なPMDファイルフォーマット
269 private PmdModel pmdRead(InputStream is)
270 throws IOException, MmdFormatException{
271 PmdLoader loader = new PmdLoader();
272 PmdModel model = loader.load(is);
277 * XMLファイルからモデルデータを読み込む。
278 * @param source 入力ソース
280 * @throws IOException 入力エラー
281 * @throws SAXException XML構文エラー
282 * @throws TogaXmlException 不正なXMLデータ
284 private PmdModel xmlRead(InputSource source)
288 XMLReader reader = XmlInputUtil.buildReader(this.inTypes);
289 XmlPmdLoader loader = new XmlPmdLoader(reader);
290 PmdModel model = loader.parse(source);
295 * モデルデータをPMDファイルに出力する。
296 * @param model モデルデータ
297 * @param ostream 出力ストリーム
298 * @throws IOException 出力エラー
299 * @throws IllegalPmdDataException 不正なモデルデータ
301 private void pmdOut(PmdModel model, OutputStream ostream)
302 throws IOException, IllegalPmdDataException{
303 PmdExporter exporter = new PmdExporter(ostream);
304 exporter.dumpPmdModel(model);
310 * モデルデータをXMLファイルに出力する。
311 * @param model モデルデータ
312 * @param ostream 出力ストリーム
313 * @throws IOException 出力エラー
314 * @throws IllegalPmdDataException 不正なモデルデータ
316 private void xmlOut(PmdModel model, OutputStream ostream)
317 throws IOException, IllegalPmdDataException{
318 PmdXmlExporter exporter = new PmdXmlExporter();
320 XmlModelFileType xmlType = this.outTypes.toXmlType();
321 exporter.setXmlFileType(xmlType);
322 exporter.setNewLine(this.newLine);
323 exporter.setGenerator(this.generator);
326 writer = new OutputStreamWriter(ostream, CS_UTF8);
327 writer = new BufferedWriter(writer);
329 exporter.putPmdXml(model, writer);