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.LocalXmlResource;
29 import jp.sourceforge.mikutoga.xml.SchemaUtil;
30 import jp.sourceforge.mikutoga.xml.TogaXmlException;
31 import jp.sourceforge.mikutoga.xml.XmlResourceResolver;
32 import org.xml.sax.InputSource;
33 import org.xml.sax.SAXException;
38 public class Pmd2XmlConv {
40 private ModelFileType inTypes = ModelFileType.NONE;
41 private ModelFileType outTypes = ModelFileType.NONE;
42 private String newLine = "\r\n";
43 private String generator = null;
56 * ドキュメントビルダファクトリを初期化する。
57 * @param builderFactory ドキュメントビルダファクトリ
59 private static void initBuilderFactory(
60 DocumentBuilderFactory builderFactory ){
61 builderFactory.setCoalescing(true);
62 builderFactory.setExpandEntityReferences(true);
63 builderFactory.setIgnoringComments(true);
64 builderFactory.setIgnoringElementContentWhitespace(false);
65 builderFactory.setNamespaceAware(true);
66 builderFactory.setValidating(false);
67 builderFactory.setXIncludeAware(false);
69 // builderFactory.setFeature(name, value);
70 // builderFactory.setAttribute(name, value);
79 private DocumentBuilder buildBuilder(){
80 XmlResourceResolver resolver = new XmlResourceResolver();
82 LocalXmlResource[] schemaArray;
85 schemaArray = new LocalXmlResource[]{
86 Schema101009.SINGLETON,
90 schemaArray = new LocalXmlResource[]{
91 Schema130128.SINGLETON,
95 schemaArray = new LocalXmlResource[]{
96 Schema101009.SINGLETON,
97 Schema130128.SINGLETON,
101 throw new IllegalStateException();
104 Schema schema = SchemaUtil.newSchema(resolver, schemaArray);
106 DocumentBuilderFactory builderFactory =
107 DocumentBuilderFactory.newInstance();
108 initBuilderFactory(builderFactory);
109 builderFactory.setSchema(schema);
111 DocumentBuilder result;
113 result = builderFactory.newDocumentBuilder();
114 }catch(ParserConfigurationException e){
116 throw new AssertionError(e);
118 result.setEntityResolver(resolver);
119 result.setErrorHandler(BotherHandler.HANDLER);
128 * @throws NullPointerException 引数がnull
129 * @throws IllegalArgumentException 具体的な種別を渡さなかった
131 public void setInType(ModelFileType type)
132 throws NullPointerException, IllegalArgumentException {
133 if(type == null) throw new NullPointerException();
134 if(type == ModelFileType.NONE) throw new IllegalArgumentException();
143 public ModelFileType getInTypes(){
150 * @throws NullPointerException 引数がnull
151 * @throws IllegalArgumentException 具体的な種別を渡さなかった
153 public void setOutType(ModelFileType type)
154 throws NullPointerException, IllegalArgumentException {
155 if(type == null) throw new NullPointerException();
156 if(type == ModelFileType.NONE) throw new IllegalArgumentException();
157 this.outTypes = type;
165 public ModelFileType getOutTypes(){
166 return this.outTypes;
171 * @param newline 改行文字
173 public void setNewline(String newline){
174 this.newLine = newline;
182 public String getNewline(){
188 * @param generator ジェネレータ名。表示したくない場合はnull
190 public void setGenerator(String generator){
191 this.generator = generator;
197 * @return ジェネレータ名。非表示の場合はnullを返す。
199 public String getGenerator(){
200 return this.generator;
207 * @throws IOException 入力エラー
208 * @throws MmdFormatException フォーマットエラー
209 * @throws SAXException XMLエラー
210 * @throws TogaXmlException XMLエラー
211 * @throws IllegalPmdDataException 内部エラー
213 public void convert(InputStream is, OutputStream os)
218 IllegalPmdDataException {
219 PmdModel model = readModel(is);
220 writeModel(model, os);
228 * @throws IOException 入力エラー
229 * @throws MmdFormatException フォーマットエラー
230 * @throws SAXException XMLエラー
231 * @throws TogaXmlException XMLエラー
233 public PmdModel readModel(InputStream is)
238 PmdModel model = null;
240 if(this.inTypes.isPmd()){
242 }else if(this.inTypes.isXml()){
245 throw new IllegalStateException();
253 * @param model モデルデータ
255 * @throws IOException 出力エラー
256 * @throws IllegalPmdDataException データの不備
258 public void writeModel(PmdModel model, OutputStream os)
260 IllegalPmdDataException {
261 if(this.outTypes.isPmd()){
263 }else if(this.outTypes.isXml()){
266 throw new IllegalStateException();
273 * PMDファイルからモデルデータを読み込む。
276 * @throws IOException 入力エラー
277 * @throws MmdFormatException 不正なPMDファイルフォーマット
279 private PmdModel pmdRead(InputStream is)
280 throws IOException, MmdFormatException{
281 PmdLoader loader = new PmdLoader();
282 PmdModel model = loader.load(is);
287 * XMLファイルからモデルデータを読み込む。
290 * @throws IOException 入力エラー
291 * @throws SAXException XML構文エラー
292 * @throws TogaXmlException 不正なXMLデータ
294 private PmdModel xmlRead(InputStream is)
298 InputSource source = new InputSource(is);
299 PmdModel result = xmlRead(source);
304 * XMLファイルからモデルデータを読み込む。
305 * @param source 入力ソース
307 * @throws IOException 入力エラー
308 * @throws SAXException XML構文エラー
309 * @throws TogaXmlException 不正なXMLデータ
311 private PmdModel xmlRead(InputSource source)
315 DocumentBuilder builder = buildBuilder();
316 XmlLoader loader = new XmlLoader();
317 PmdModel model = loader.parse(builder, source);
322 * モデルデータをPMDファイルに出力する。
323 * @param model モデルデータ
324 * @param ostream 出力ストリーム
325 * @throws IOException 出力エラー
326 * @throws IllegalPmdDataException 不正なモデルデータ
328 private void pmdOut(PmdModel model, OutputStream ostream)
329 throws IOException, IllegalPmdDataException{
330 PmdExporter exporter = new PmdExporter(ostream);
331 exporter.dumpPmdModel(model);
337 * モデルデータをXMLファイルに出力する。
338 * @param model モデルデータ
339 * @param ostream 出力ストリーム
340 * @throws IOException 出力エラー
341 * @throws IllegalPmdDataException 不正なモデルデータ
343 private void xmlOut(PmdModel model, OutputStream ostream)
344 throws IOException, IllegalPmdDataException{
345 XmlExporter exporter = new XmlExporter(ostream);
347 XmlModelFileType xmlType = this.outTypes.toXmlType();
348 exporter.setXmlFileType(xmlType);
349 exporter.setNewLine(this.newLine);
350 exporter.setGenerator(this.generator);
352 exporter.putPmdModel(model);