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.model.PmdModel;
24 import jp.sfjp.mikutoga.pmd.model.binio.PmdExporter;
25 import jp.sfjp.mikutoga.pmd.model.binio.PmdLoader;
26 import jp.sfjp.mikutoga.pmd.model.xml.PmdXmlExporter;
27 import jp.sfjp.mikutoga.pmd.model.xml.Schema101009;
28 import jp.sfjp.mikutoga.pmd.model.xml.Schema130128;
29 import jp.sfjp.mikutoga.pmd.model.xml.XmlModelFileType;
30 import jp.sfjp.mikutoga.pmd.model.xml.XmlPmdLoader;
31 import jp.sfjp.mikutoga.xml.BotherHandler;
32 import jp.sfjp.mikutoga.xml.LocalXmlResource;
33 import jp.sfjp.mikutoga.xml.SchemaUtil;
34 import jp.sfjp.mikutoga.xml.TogaXmlException;
35 import jp.sfjp.mikutoga.xml.XmlResourceResolver;
36 import org.xml.sax.InputSource;
37 import org.xml.sax.SAXException;
38 import org.xml.sax.XMLReader;
43 public class Pmd2XmlConv {
46 private static final Charset CS_UTF8 = Charset.forName("UTF-8");
49 private ModelFileType inTypes = ModelFileType.NONE;
50 private ModelFileType outTypes = ModelFileType.NONE;
51 private String newLine = "\r\n";
52 private String generator = null;
65 * ドキュメントビルダファクトリを初期化する。
66 * @param builderFactory ドキュメントビルダファクトリ
68 private static void initBuilderFactory(
69 DocumentBuilderFactory builderFactory ){
70 builderFactory.setCoalescing(true);
71 builderFactory.setExpandEntityReferences(true);
72 builderFactory.setIgnoringComments(true);
73 builderFactory.setIgnoringElementContentWhitespace(false);
74 builderFactory.setNamespaceAware(true);
75 builderFactory.setValidating(false);
76 builderFactory.setXIncludeAware(false);
78 // builderFactory.setFeature(name, value);
79 // builderFactory.setAttribute(name, value);
88 private DocumentBuilder buildBuilder(){
89 XmlResourceResolver resolver = new XmlResourceResolver();
91 LocalXmlResource[] schemaArray;
94 schemaArray = new LocalXmlResource[]{
95 Schema101009.SINGLETON,
99 schemaArray = new LocalXmlResource[]{
100 Schema130128.SINGLETON,
104 schemaArray = new LocalXmlResource[]{
105 Schema101009.SINGLETON,
106 Schema130128.SINGLETON,
110 throw new IllegalStateException();
113 Schema schema = SchemaUtil.newSchema(resolver, schemaArray);
115 DocumentBuilderFactory builderFactory =
116 DocumentBuilderFactory.newInstance();
117 initBuilderFactory(builderFactory);
118 builderFactory.setSchema(schema);
120 DocumentBuilder result;
122 result = builderFactory.newDocumentBuilder();
123 }catch(ParserConfigurationException e){
125 throw new AssertionError(e);
127 result.setEntityResolver(resolver);
128 result.setErrorHandler(BotherHandler.HANDLER);
137 * @throws NullPointerException 引数がnull
138 * @throws IllegalArgumentException 具体的な種別を渡さなかった
140 public void setInType(ModelFileType type)
141 throws NullPointerException, IllegalArgumentException {
142 if(type == null) throw new NullPointerException();
143 if(type == ModelFileType.NONE) throw new IllegalArgumentException();
152 public ModelFileType getInTypes(){
159 * @throws NullPointerException 引数がnull
160 * @throws IllegalArgumentException 具体的な種別を渡さなかった
162 public void setOutType(ModelFileType type)
163 throws NullPointerException, IllegalArgumentException {
164 if(type == null) throw new NullPointerException();
165 if(type == ModelFileType.NONE) throw new IllegalArgumentException();
166 this.outTypes = type;
174 public ModelFileType getOutTypes(){
175 return this.outTypes;
180 * @param newline 改行文字
182 public void setNewline(String newline){
183 this.newLine = newline;
191 public String getNewline(){
197 * @param generator ジェネレータ名。表示したくない場合はnull
199 public void setGenerator(String generator){
200 this.generator = generator;
206 * @return ジェネレータ名。非表示の場合はnullを返す。
208 public String getGenerator(){
209 return this.generator;
216 * @throws IOException 入力エラー
217 * @throws MmdFormatException フォーマットエラー
218 * @throws SAXException XMLエラー
219 * @throws TogaXmlException XMLエラー
220 * @throws IllegalPmdDataException 内部エラー
222 public void convert(InputStream is, OutputStream os)
227 IllegalPmdDataException {
228 PmdModel model = readModel(is);
229 writeModel(model, os);
237 * @throws IOException 入力エラー
238 * @throws MmdFormatException フォーマットエラー
239 * @throws SAXException XMLエラー
240 * @throws TogaXmlException XMLエラー
242 public PmdModel readModel(InputStream is)
247 PmdModel model = null;
249 if(this.inTypes.isPmd()){
251 }else if(this.inTypes.isXml()){
254 throw new IllegalStateException();
262 * @param model モデルデータ
264 * @throws IOException 出力エラー
265 * @throws IllegalPmdDataException データの不備
267 public void writeModel(PmdModel model, OutputStream os)
269 IllegalPmdDataException {
270 if(this.outTypes.isPmd()){
272 }else if(this.outTypes.isXml()){
275 throw new IllegalStateException();
282 * PMDファイルからモデルデータを読み込む。
285 * @throws IOException 入力エラー
286 * @throws MmdFormatException 不正なPMDファイルフォーマット
288 private PmdModel pmdRead(InputStream is)
289 throws IOException, MmdFormatException{
290 PmdLoader loader = new PmdLoader();
291 PmdModel model = loader.load(is);
296 * XMLファイルからモデルデータを読み込む。
299 * @throws IOException 入力エラー
300 * @throws SAXException XML構文エラー
301 * @throws TogaXmlException 不正なXMLデータ
303 private PmdModel xmlRead(InputStream is)
307 InputSource source = new InputSource(is);
308 PmdModel result = xmlRead(source);
313 * XMLファイルからモデルデータを読み込む。
314 * @param source 入力ソース
316 * @throws IOException 入力エラー
317 * @throws SAXException XML構文エラー
318 * @throws TogaXmlException 不正なXMLデータ
320 private PmdModel xmlRead(InputSource source)
324 XMLReader reader = XmlInputUtil.buildReader(this.inTypes);
325 XmlPmdLoader loader = new XmlPmdLoader(reader);
326 PmdModel model = loader.parse(source);
331 * モデルデータをPMDファイルに出力する。
332 * @param model モデルデータ
333 * @param ostream 出力ストリーム
334 * @throws IOException 出力エラー
335 * @throws IllegalPmdDataException 不正なモデルデータ
337 private void pmdOut(PmdModel model, OutputStream ostream)
338 throws IOException, IllegalPmdDataException{
339 PmdExporter exporter = new PmdExporter(ostream);
340 exporter.dumpPmdModel(model);
346 * モデルデータをXMLファイルに出力する。
347 * @param model モデルデータ
348 * @param ostream 出力ストリーム
349 * @throws IOException 出力エラー
350 * @throws IllegalPmdDataException 不正なモデルデータ
352 private void xmlOut(PmdModel model, OutputStream ostream)
353 throws IOException, IllegalPmdDataException{
354 PmdXmlExporter exporter = new PmdXmlExporter();
356 XmlModelFileType xmlType = this.outTypes.toXmlType();
357 exporter.setXmlFileType(xmlType);
358 exporter.setNewLine(this.newLine);
359 exporter.setGenerator(this.generator);
362 writer = new OutputStreamWriter(ostream, CS_UTF8);
363 writer = new BufferedWriter(writer);
365 exporter.putPmdXml(model, writer);