2 * xml resource resolver
\r
4 * License : The MIT License
\r
5 * Copyright(c) 2009 olyutorskii
\r
8 package jp.sourceforge.mikutoga.xml;
\r
10 import java.io.IOException;
\r
11 import java.io.InputStream;
\r
12 import java.io.Reader;
\r
13 import java.net.URI;
\r
14 import java.net.URISyntaxException;
\r
15 import java.net.URL;
\r
16 import java.util.HashMap;
\r
17 import java.util.Map;
\r
18 import org.w3c.dom.ls.LSInput;
\r
19 import org.w3c.dom.ls.LSResourceResolver;
\r
20 import org.xml.sax.EntityResolver;
\r
21 import org.xml.sax.InputSource;
\r
22 import org.xml.sax.SAXException;
\r
25 * URL変換マップに従い、XML文書からの外部参照をリダイレクトする。
\r
26 * 相対URIはこのクラスをベースに解決される。
\r
27 * 主な用途は外部スキーマのリソース化など。
\r
29 public class XmlResourceResolver
\r
30 implements LSResourceResolver, EntityResolver {
\r
32 public static final String SCHEMA_XML =
\r
33 "http://www.w3.org/2001/xml.xsd";
\r
34 public static final String NS_XSD =
\r
35 "http://www.w3.org/2001/XMLSchema-instance";
\r
37 private static final String LOCAL_SCHEMA_XML =
\r
38 "resources/xml-2009-01.xsd";
\r
39 private static final URI EMPTY_URI = URI.create("");
\r
40 private static final Class THISCLASS = XmlResourceResolver.class;
\r
42 private final Map<URI, URI> uriMap = new HashMap<URI, URI>();
\r
47 public XmlResourceResolver(){
\r
50 assert this.getClass().equals(THISCLASS);
\r
52 URI originalURI = URI.create(SCHEMA_XML);
\r
53 URL redirectURL = THISCLASS.getResource(LOCAL_SCHEMA_XML);
\r
56 redirectURI = redirectURL.toURI();
\r
57 }catch(URISyntaxException e){
\r
59 throw new AssertionError(e);
\r
62 this.uriMap.put(originalURI, redirectURI);
\r
68 * 絶対URIと相対URIを合成したURIを返す。
\r
70 * @param base 絶対URIでなければならない。nullでもよい。
\r
71 * @param relative 絶対URIでもよいがその場合baseは無視される。null可。
\r
72 * @return 合成結果のURLオブジェクト。必ず絶対URIになる。
\r
73 * @throws java.net.URISyntaxException URIとして変。
\r
74 * @throws java.lang.IllegalArgumentException 絶対URIが生成できない。
\r
76 protected static URI buildBaseRelativeURI(String base, String relative)
\r
77 throws URISyntaxException,
\r
78 IllegalArgumentException {
\r
81 baseURI = new URI(base);
\r
82 if( ! baseURI.isAbsolute() ) throw new IllegalArgumentException();
\r
85 URI relativeURI = EMPTY_URI;
\r
86 if(relative != null){
\r
87 relativeURI = new URI(relative);
\r
91 if(baseURI == null || relativeURI.isAbsolute()){
\r
92 resultURI = relativeURI;
\r
94 resultURI = baseURI.resolve(relativeURI);
\r
97 if( ! resultURI.isAbsolute() ) throw new IllegalArgumentException();
\r
99 resultURI = resultURI.normalize();
\r
106 * @return LSInput実装
\r
108 public static LSInput createLSInput(){
\r
109 LSInput input = new LSInputImpl();
\r
114 * オリジナルURIとリダイレクト先のURIを登録する。
\r
115 * オリジナルURIへのアクセスはリダイレクトされる。
\r
116 * @param original オリジナルURI
\r
117 * @param redirect リダイレクトURI
\r
119 public void putURIMap(URI original, URI redirect){
\r
120 this.uriMap.put(original.normalize(), redirect.normalize());
\r
125 * 変換後のリソースの入力ストリームを得る。
\r
126 * @param originalURI オリジナルURI
\r
128 * @throws java.io.IOException 入出力エラー
\r
130 public InputStream getXMLResourceAsStream(URI originalURI)
\r
131 throws IOException{
\r
132 URI resourceURI = this.uriMap.get(originalURI.normalize());
\r
133 URL resourceURL = resourceURI.toURL();
\r
134 InputStream is = resourceURL.openStream();
\r
141 * URL変換したあとの入力ソースを返す。
\r
142 * @param type {@inheritDoc}
\r
143 * @param namespaceURI {@inheritDoc}
\r
144 * @param publicId {@inheritDoc}
\r
145 * @param systemId {@inheritDoc}
\r
146 * @param baseURI {@inheritDoc}
\r
147 * @return {@inheritDoc}
\r
150 public LSInput resolveResource(String type,
\r
151 String namespaceURI,
\r
155 if(systemId == null) return null;
\r
159 originalURI = buildBaseRelativeURI(baseURI, systemId);
\r
160 }catch(URISyntaxException e){
\r
166 is = getXMLResourceAsStream(originalURI);
\r
167 }catch(IOException e){
\r
171 LSInput input = createLSInput();
\r
172 input.setBaseURI(baseURI);
\r
173 input.setPublicId(publicId);
\r
174 input.setSystemId(systemId);
\r
175 input.setByteStream(is);
\r
182 * URL変換したあとの入力ソースを返す。
\r
183 * @param publicId {@inheritDoc}
\r
184 * @param systemId {@inheritDoc}
\r
185 * @return {@inheritDoc}
\r
186 * @throws org.xml.sax.SAXException {@inheritDoc}
\r
187 * @throws java.io.IOException {@inheritDoc}
\r
190 public InputSource resolveEntity(String publicId, String systemId)
\r
191 throws SAXException, IOException{
\r
192 if(systemId == null) return null;
\r
196 originalUri = new URI(systemId);
\r
197 }catch(URISyntaxException e){
\r
201 InputStream is = getXMLResourceAsStream(originalUri);
\r
203 InputSource source = new InputSource(is);
\r
204 source.setPublicId(publicId);
\r
205 source.setSystemId(systemId);
\r
211 * JRE1.5用LSInput実装。
\r
213 * org.w3c.dom.ls.DOMImplementationLS#createLSInput()
\r
216 private static final class LSInputImpl implements LSInput {
\r
218 private String baseURI = null;
\r
219 private InputStream byteStream = null;
\r
220 private boolean certifiedText = false;
\r
221 private Reader characterStream = null;
\r
222 private String encoding = null;
\r
223 private String publicId = null;
\r
224 private String stringData = null;
\r
225 private String systemId = null;
\r
230 private LSInputImpl(){
\r
237 * @return {@inheritDoc}
\r
240 public String getBaseURI(){
\r
241 return this.baseURI;
\r
246 * @param baseURI {@inheritDoc}
\r
249 public void setBaseURI(String baseURI){
\r
250 this.baseURI = baseURI;
\r
256 * @return {@inheritDoc}
\r
259 public InputStream getByteStream(){
\r
260 return this.byteStream;
\r
265 * @param byteStream {@inheritDoc}
\r
268 public void setByteStream(InputStream byteStream){
\r
269 this.byteStream = byteStream;
\r
274 * @return {@inheritDoc}
\r
277 public boolean getCertifiedText(){
\r
278 return this.certifiedText;
\r
283 * @param certifiedText {@inheritDoc}
\r
286 public void setCertifiedText(boolean certifiedText){
\r
287 this.certifiedText = certifiedText;
\r
293 * @return {@inheritDoc}
\r
296 public Reader getCharacterStream(){
\r
297 return this.characterStream;
\r
302 * @param characterStream {@inheritDoc}
\r
305 public void setCharacterStream(Reader characterStream){
\r
306 this.characterStream = characterStream;
\r
311 * @return {@inheritDoc}
\r
314 public String getEncoding(){
\r
315 return this.encoding;
\r
320 * @param encoding {@inheritDoc}
\r
323 public void setEncoding(String encoding){
\r
324 this.encoding = encoding;
\r
330 * @return {@inheritDoc}
\r
333 public String getPublicId(){
\r
334 return this.publicId;
\r
339 * @param publicId {@inheritDoc}
\r
342 public void setPublicId(String publicId){
\r
343 this.publicId = publicId;
\r
349 * @return {@inheritDoc}
\r
352 public String getStringData(){
\r
353 return this.stringData;
\r
358 * @param stringData {@inheritDoc}
\r
361 public void setStringData(String stringData){
\r
362 this.stringData = stringData;
\r
368 * @return {@inheritDoc}
\r
371 public String getSystemId(){
\r
372 return this.systemId;
\r
377 * @param systemId {@inheritDoc}
\r
380 public void setSystemId(String systemId){
\r
381 this.systemId = systemId;
\r
387 // TODO OASIS XML Catalog などと調和したい。
\r