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 javax.xml.parsers.DocumentBuilder;
18 import javax.xml.parsers.DocumentBuilderFactory;
19 import javax.xml.parsers.ParserConfigurationException;
20 import javax.xml.validation.Schema;
21 import jp.sfjp.mikutoga.bin.parser.MmdFormatException;
22 import jp.sfjp.mikutoga.pmd.IllegalPmdDataException;
23 import jp.sfjp.mikutoga.pmd.binio.PmdExporter;
24 import jp.sfjp.mikutoga.pmd.binio.PmdLoader;
25 import jp.sfjp.mikutoga.pmd.model.PmdModel;
26 import jp.sfjp.mikutoga.pmd.xml.PmdXmlExporter;
27 import jp.sfjp.mikutoga.pmd.xml.Schema101009;
28 import jp.sfjp.mikutoga.pmd.xml.Schema130128;
29 import jp.sfjp.mikutoga.pmd.xml.XmlLoader;
30 import jp.sfjp.mikutoga.pmd.xml.XmlModelFileType;
31 import jp.sourceforge.mikutoga.xml.BotherHandler;
32 import jp.sourceforge.mikutoga.xml.LocalXmlResource;
33 import jp.sourceforge.mikutoga.xml.SchemaUtil;
34 import jp.sourceforge.mikutoga.xml.TogaXmlException;
35 import jp.sourceforge.mikutoga.xml.XmlResourceResolver;
36 import org.xml.sax.InputSource;
37 import org.xml.sax.SAXException;
42 public class Pmd2XmlConv {
45 private static final Charset CS_UTF8 = Charset.forName("UTF-8");
48 private ModelFileType inTypes = ModelFileType.NONE;
49 private ModelFileType outTypes = ModelFileType.NONE;
50 private String newLine = "\r\n";
51 private String generator = null;
64 * ドキュメントビルダファクトリを初期化する。
65 * @param builderFactory ドキュメントビルダファクトリ
67 private static void initBuilderFactory(
68 DocumentBuilderFactory builderFactory ){
69 builderFactory.setCoalescing(true);
70 builderFactory.setExpandEntityReferences(true);
71 builderFactory.setIgnoringComments(true);
72 builderFactory.setIgnoringElementContentWhitespace(false);
73 builderFactory.setNamespaceAware(true);
74 builderFactory.setValidating(false);
75 builderFactory.setXIncludeAware(false);
77 // builderFactory.setFeature(name, value);
78 // builderFactory.setAttribute(name, value);
87 private DocumentBuilder buildBuilder(){
88 XmlResourceResolver resolver = new XmlResourceResolver();
90 LocalXmlResource[] schemaArray;
93 schemaArray = new LocalXmlResource[]{
94 Schema101009.SINGLETON,
98 schemaArray = new LocalXmlResource[]{
99 Schema130128.SINGLETON,
103 schemaArray = new LocalXmlResource[]{
104 Schema101009.SINGLETON,
105 Schema130128.SINGLETON,
109 throw new IllegalStateException();
112 Schema schema = SchemaUtil.newSchema(resolver, schemaArray);
114 DocumentBuilderFactory builderFactory =
115 DocumentBuilderFactory.newInstance();
116 initBuilderFactory(builderFactory);
117 builderFactory.setSchema(schema);
119 DocumentBuilder result;
121 result = builderFactory.newDocumentBuilder();
122 }catch(ParserConfigurationException e){
124 throw new AssertionError(e);
126 result.setEntityResolver(resolver);
127 result.setErrorHandler(BotherHandler.HANDLER);
136 * @throws NullPointerException 引数がnull
137 * @throws IllegalArgumentException 具体的な種別を渡さなかった
139 public void setInType(ModelFileType type)
140 throws NullPointerException, IllegalArgumentException {
141 if(type == null) throw new NullPointerException();
142 if(type == ModelFileType.NONE) throw new IllegalArgumentException();
151 public ModelFileType getInTypes(){
158 * @throws NullPointerException 引数がnull
159 * @throws IllegalArgumentException 具体的な種別を渡さなかった
161 public void setOutType(ModelFileType type)
162 throws NullPointerException, IllegalArgumentException {
163 if(type == null) throw new NullPointerException();
164 if(type == ModelFileType.NONE) throw new IllegalArgumentException();
165 this.outTypes = type;
173 public ModelFileType getOutTypes(){
174 return this.outTypes;
179 * @param newline 改行文字
181 public void setNewline(String newline){
182 this.newLine = newline;
190 public String getNewline(){
196 * @param generator ジェネレータ名。表示したくない場合はnull
198 public void setGenerator(String generator){
199 this.generator = generator;
205 * @return ジェネレータ名。非表示の場合はnullを返す。
207 public String getGenerator(){
208 return this.generator;
215 * @throws IOException 入力エラー
216 * @throws MmdFormatException フォーマットエラー
217 * @throws SAXException XMLエラー
218 * @throws TogaXmlException XMLエラー
219 * @throws IllegalPmdDataException 内部エラー
221 public void convert(InputStream is, OutputStream os)
226 IllegalPmdDataException {
227 PmdModel model = readModel(is);
228 writeModel(model, os);
236 * @throws IOException 入力エラー
237 * @throws MmdFormatException フォーマットエラー
238 * @throws SAXException XMLエラー
239 * @throws TogaXmlException XMLエラー
241 public PmdModel readModel(InputStream is)
246 PmdModel model = null;
248 if(this.inTypes.isPmd()){
250 }else if(this.inTypes.isXml()){
253 throw new IllegalStateException();
261 * @param model モデルデータ
263 * @throws IOException 出力エラー
264 * @throws IllegalPmdDataException データの不備
266 public void writeModel(PmdModel model, OutputStream os)
268 IllegalPmdDataException {
269 if(this.outTypes.isPmd()){
271 }else if(this.outTypes.isXml()){
274 throw new IllegalStateException();
281 * 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ファイルからモデルデータを読み込む。
298 * @throws IOException 入力エラー
299 * @throws SAXException XML構文エラー
300 * @throws TogaXmlException 不正なXMLデータ
302 private PmdModel xmlRead(InputStream is)
306 InputSource source = new InputSource(is);
307 PmdModel result = xmlRead(source);
312 * XMLファイルからモデルデータを読み込む。
313 * @param source 入力ソース
315 * @throws IOException 入力エラー
316 * @throws SAXException XML構文エラー
317 * @throws TogaXmlException 不正なXMLデータ
319 private PmdModel xmlRead(InputSource source)
323 DocumentBuilder builder = buildBuilder();
324 XmlLoader loader = new XmlLoader();
325 PmdModel model = loader.parse(builder, source);
330 * モデルデータをPMDファイルに出力する。
331 * @param model モデルデータ
332 * @param ostream 出力ストリーム
333 * @throws IOException 出力エラー
334 * @throws IllegalPmdDataException 不正なモデルデータ
336 private void pmdOut(PmdModel model, OutputStream ostream)
337 throws IOException, IllegalPmdDataException{
338 PmdExporter exporter = new PmdExporter(ostream);
339 exporter.dumpPmdModel(model);
345 * モデルデータをXMLファイルに出力する。
346 * @param model モデルデータ
347 * @param ostream 出力ストリーム
348 * @throws IOException 出力エラー
349 * @throws IllegalPmdDataException 不正なモデルデータ
351 private void xmlOut(PmdModel model, OutputStream ostream)
352 throws IOException, IllegalPmdDataException{
353 PmdXmlExporter exporter = new PmdXmlExporter();
355 XmlModelFileType xmlType = this.outTypes.toXmlType();
356 exporter.setXmlFileType(xmlType);
357 exporter.setNewLine(this.newLine);
358 exporter.setGenerator(this.generator);
361 writer = new OutputStreamWriter(ostream, CS_UTF8);
362 writer = new BufferedWriter(writer);
364 exporter.putPmdModel(model, writer);