4 * License : The MIT License
5 * Copyright(c) 2010 MikuToga Partners
8 package jp.sfjp.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.DocumentBuilderFactory;
15 import javax.xml.parsers.ParserConfigurationException;
16 import javax.xml.validation.Schema;
17 import jp.sfjp.mikutoga.bin.parser.MmdFormatException;
18 import jp.sfjp.mikutoga.pmd.IllegalPmdDataException;
19 import jp.sfjp.mikutoga.pmd.binio.PmdExporter;
20 import jp.sfjp.mikutoga.pmd.binio.PmdLoader;
21 import jp.sfjp.mikutoga.pmd.model.PmdModel;
22 import jp.sfjp.mikutoga.pmd.xml.Schema101009;
23 import jp.sfjp.mikutoga.pmd.xml.Schema130128;
24 import jp.sfjp.mikutoga.pmd.xml.XmlExporter;
25 import jp.sfjp.mikutoga.pmd.xml.XmlLoader;
26 import jp.sfjp.mikutoga.pmd.xml.XmlModelFileType;
27 import jp.sourceforge.mikutoga.xml.BotherHandler;
28 import jp.sourceforge.mikutoga.xml.LocalSchema;
29 import jp.sourceforge.mikutoga.xml.TogaXmlException;
30 import jp.sourceforge.mikutoga.xml.XmlResourceResolver;
31 import org.xml.sax.InputSource;
32 import org.xml.sax.SAXException;
37 public class Pmd2XmlConv {
39 private ModelFileType inTypes = ModelFileType.NONE;
40 private ModelFileType outTypes = ModelFileType.NONE;
41 private String newLine = "\r\n";
42 private String generator = null;
55 * ドキュメントビルダファクトリを初期化する。
56 * @param builderFactory ドキュメントビルダファクトリ
58 private static void initBuilderFactory(
59 DocumentBuilderFactory builderFactory ){
60 builderFactory.setCoalescing(true);
61 builderFactory.setExpandEntityReferences(true);
62 builderFactory.setIgnoringComments(true);
63 builderFactory.setIgnoringElementContentWhitespace(false);
64 builderFactory.setNamespaceAware(true);
65 builderFactory.setValidating(false);
66 builderFactory.setXIncludeAware(false);
68 // builderFactory.setFeature(name, value);
69 // builderFactory.setAttribute(name, value);
78 private DocumentBuilder buildBuilder(){
79 XmlResourceResolver resolver = new XmlResourceResolver();
85 schema = LocalSchema.newSchema(resolver, new Schema101009());
88 schema = LocalSchema.newSchema(resolver, new Schema130128());
91 schema = LocalSchema.newSchema(resolver,
92 new Schema101009(), new Schema130128());
95 throw new IllegalStateException();
98 DocumentBuilderFactory builderFactory =
99 DocumentBuilderFactory.newInstance();
100 initBuilderFactory(builderFactory);
101 builderFactory.setSchema(schema);
103 DocumentBuilder result;
105 result = builderFactory.newDocumentBuilder();
106 }catch(ParserConfigurationException e){
108 throw new AssertionError(e);
110 result.setEntityResolver(resolver);
111 result.setErrorHandler(BotherHandler.HANDLER);
120 * @throws IllegalArgumentException 具体的な種別を渡さなかった
122 public void setInType(ModelFileType type){
123 if(type == null) throw new NullPointerException();
124 if(type == ModelFileType.NONE) throw new IllegalArgumentException();
133 public ModelFileType getInTypes(){
140 * @throws IllegalArgumentException 具体的な種別を渡さなかった
142 public void setOutType(ModelFileType type){
143 if(type == null) throw new NullPointerException();
144 if(type == ModelFileType.NONE) throw new IllegalArgumentException();
145 this.outTypes = type;
153 public ModelFileType getOutTypes(){
154 return this.outTypes;
159 * @param newline 改行文字
161 public void setNewline(String newline){
162 this.newLine = newline;
170 public String getNewLine(){
176 * @param generator ジェネレータ名。表示したくない場合はnull
178 public void setGenerator(String generator){
179 this.generator = generator;
185 * @return ジェネレータ名。非表示の場合はnullを返す。
187 public String getGenerator(){
188 return this.generator;
195 * @throws IOException 入力エラー
196 * @throws MmdFormatException フォーマットエラー
197 * @throws SAXException XMLエラー
198 * @throws TogaXmlException XMLエラー
199 * @throws IllegalPmdDataException 内部エラー
201 public void convert(InputStream is, OutputStream os)
206 IllegalPmdDataException {
207 PmdModel model = readModel(is);
208 writeModel(model, os);
216 * @throws IOException 入力エラー
217 * @throws MmdFormatException フォーマットエラー
218 * @throws SAXException XMLエラー
219 * @throws TogaXmlException XMLエラー
220 * @throws IllegalStateException ファイル種別がまた指定されていない
222 public PmdModel readModel(InputStream is)
227 PmdModel model = null;
229 if(this.inTypes.isPmd()){
231 }else if(this.inTypes.isXml()){
234 throw new IllegalStateException();
242 * @param model モデルデータ
244 * @throws IOException 出力エラー
245 * @throws IllegalPmdDataException データの不備
246 * @throws IllegalStateException ファイル種別がまた指定されていない
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(is);
272 PmdModel model = loader.load();
277 * XMLファイルからモデルデータを読み込む。
280 * @throws IOException 入力エラー
281 * @throws SAXException XML構文エラー
282 * @throws TogaXmlException 不正なXMLデータ
284 private PmdModel xmlRead(InputStream is)
288 InputSource source = new InputSource(is);
289 PmdModel result = xmlRead(source);
294 * XMLファイルからモデルデータを読み込む。
295 * @param source 入力ソース
297 * @throws IOException 入力エラー
298 * @throws SAXException XML構文エラー
299 * @throws TogaXmlException 不正なXMLデータ
301 private PmdModel xmlRead(InputSource source)
305 DocumentBuilder builder = buildBuilder();
306 XmlLoader loader = new XmlLoader();
307 PmdModel model = loader.parse(builder, source);
312 * モデルデータをPMDファイルに出力する。
313 * @param model モデルデータ
314 * @param ostream 出力ストリーム
315 * @throws IOException 出力エラー
316 * @throws IllegalPmdDataException 不正なモデルデータ
318 private void pmdOut(PmdModel model, OutputStream ostream)
319 throws IOException, IllegalPmdDataException{
320 PmdExporter exporter = new PmdExporter(ostream);
321 exporter.dumpPmdModel(model);
327 * モデルデータをXMLファイルに出力する。
328 * @param model モデルデータ
329 * @param ostream 出力ストリーム
330 * @throws IOException 出力エラー
331 * @throws IllegalPmdDataException 不正なモデルデータ
333 private void xmlOut(PmdModel model, OutputStream ostream)
334 throws IOException, IllegalPmdDataException{
335 XmlExporter exporter = new XmlExporter(ostream);
337 XmlModelFileType xmlType = this.outTypes.toXmlType();
338 exporter.setXmlFileType(xmlType);
339 exporter.setNewLine(this.newLine);
340 exporter.setGenerator(this.generator);
342 exporter.putPmdModel(model);