4 * License : The MIT License
5 * Copyright(c) 2013 MikuToga Partners
8 package jp.sfjp.mikutoga.pmd2xml;
10 import java.io.BufferedInputStream;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.net.MalformedURLException;
17 import javax.xml.parsers.ParserConfigurationException;
18 import javax.xml.parsers.SAXParser;
19 import javax.xml.parsers.SAXParserFactory;
20 import javax.xml.validation.Schema;
21 import jp.sfjp.mikutoga.pmd.model.xml.Schema101009;
22 import jp.sfjp.mikutoga.pmd.model.xml.Schema130128;
23 import jp.sfjp.mikutoga.xml.BotherHandler;
24 import jp.sfjp.mikutoga.xml.LocalXmlResource;
25 import jp.sfjp.mikutoga.xml.SchemaUtil;
26 import jp.sfjp.mikutoga.xml.XmlResourceResolver;
27 import org.xml.sax.InputSource;
28 import org.xml.sax.SAXException;
29 import org.xml.sax.XMLReader;
34 final class XmlInputUtil {
39 private XmlInputUtil(){
41 throw new AssertionError();
46 * 実在ファイルからXML入力ソースを得る。
50 static InputSource fileToSource(File file){
53 URI uri = file.toURI();
58 }catch(MalformedURLException e){
59 // 実在File由来のURLでは起こりえない
61 throw new AssertionError(e);
64 String systemId = url.toString();
66 InputSource source = new InputSource(systemId);
72 * InputSourceからInputStreamを得る。
73 * <p>入力ソースには、少なくともバイトストリームか
74 * URL文字列(SystemId)のいずれかが設定されていなければならない。
77 * @throws IllegalArgumentException 入力ソースの設定が足りない。
78 * @throws IOException 入力ソースにアクセス不能。
80 static InputStream openInputSource(InputSource source)
81 throws IllegalArgumentException, IOException{
84 is = source.getByteStream();
87 String systemId = source.getSystemId();
88 if(systemId == null) throw new IllegalArgumentException();
90 URL url = new URL(systemId);
91 is = url.openStream();
94 is = new BufferedInputStream(is);
102 * <li>XML名前空間機能は有効になる。
103 * <li>DTDによる形式検証は無効となる。
104 * <li>XIncludeによる差し込み機能は無効となる。
109 private static SAXParserFactory buildFactory(Schema schema){
110 SAXParserFactory factory = SAXParserFactory.newInstance();
112 factory.setNamespaceAware(true);
113 factory.setValidating(false);
114 factory.setXIncludeAware(false);
115 // factory.setFeature(name, value);
117 factory.setSchema(schema);
127 private static SAXParser buildParser(Schema schema){
128 SAXParserFactory factory = buildFactory(schema);
132 parser = factory.newSAXParser();
133 }catch(ParserConfigurationException e){
135 throw new AssertionError(e);
136 }catch(SAXException e){
138 throw new AssertionError(e);
141 // parser.setProperty(name, value);
148 * @param resolver リゾルバ
149 * @param xmlInType 入力XML種別
152 private static Schema builsSchema(XmlResourceResolver resolver,
153 ModelFileType xmlInType ){
154 LocalXmlResource[] schemaArray;
157 schemaArray = new LocalXmlResource[]{
158 Schema101009.SINGLETON,
162 schemaArray = new LocalXmlResource[]{
163 Schema130128.SINGLETON,
167 schemaArray = new LocalXmlResource[]{
168 Schema101009.SINGLETON,
169 Schema130128.SINGLETON,
173 throw new IllegalStateException();
176 Schema schema = SchemaUtil.newSchema(resolver, schemaArray);
183 * <p>エラーハンドラには{@link BotherHandler}が指定される。
184 * @param xmlInType 入力XML種別
187 static XMLReader buildReader(ModelFileType xmlInType){
188 XmlResourceResolver resolver = new XmlResourceResolver();
190 Schema schema = builsSchema(resolver, xmlInType);
192 SAXParser parser = buildParser(schema);
196 reader = parser.getXMLReader();
197 }catch(SAXException e){
199 throw new AssertionError(e);
202 reader.setEntityResolver(resolver);
203 reader.setErrorHandler(BotherHandler.HANDLER);