View Javadoc

1   /*
2    * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.opensaml.xml.parse;
18  
19  import java.io.BufferedInputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.InputStreamReader;
23  import java.io.Reader;
24  
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  import org.w3c.dom.ls.LSInput;
28  import org.w3c.dom.ls.LSResourceResolver;
29  import org.xml.sax.EntityResolver;
30  import org.xml.sax.InputSource;
31  import org.xml.sax.SAXException;
32  
33  /**
34   * A entity resolver that resolves an entity's location within the classpath.
35   * 
36   * Entity URIs <strong>must</strong> begin with the prefix <code>classpath:</code> and be followed by either an
37   * absolute or relative classpath. Relative classpaths are relative to <strong>this</strong> class.
38   * 
39   * This resolver will <strong>not</strong> attempt to resolve any other URIs.
40   */
41  public class ClasspathResolver implements EntityResolver, LSResourceResolver {
42  
43      /** UR scheme for classpath locations. */
44      public static final String CLASSPATH_URI_SCHEME = "classpath:";
45  
46      /** Class logger. */
47      private final Logger log = LoggerFactory.getLogger(ClasspathResolver.class);
48  
49      /** {@inheritDoc} */
50      public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
51          InputStream resourceStream = resolver(publicId, systemId);
52          if (resourceStream != null) {
53              return new InputSource(resourceStream);
54          }
55  
56          return null;
57      }
58  
59      /** {@inheritDoc} */
60      public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
61          return new LSInputImpl(publicId, systemId, resolver(publicId, systemId));
62      }
63  
64      /**
65       * Resolves an id against the classpath. System ID is tried first, then public ID.
66       * 
67       * @param publicId resources public ID
68       * @param systemId resources system ID
69       * 
70       * @return resolved resource or null
71       */
72      protected InputStream resolver(String publicId, String systemId) {
73          String resource = null;
74          InputStream resourceIns = null;
75  
76          if (systemId.startsWith(CLASSPATH_URI_SCHEME)) {
77              log.trace("Attempting to resolve, within the classpath, the entity with the following system id: {}",
78                      systemId);
79              resource = systemId.replaceFirst("classpath:", "");
80              resourceIns = getClass().getResourceAsStream(resource);
81          }
82  
83          if (resourceIns == null && publicId != null && publicId.startsWith(CLASSPATH_URI_SCHEME)) {
84              log.trace("Attempting to resolve, within the classpath, the entity with the following public id: {}",
85                      resource);
86              resource = publicId.replaceFirst("classpath:", "");
87              resourceIns = getClass().getResourceAsStream(resource);
88          }
89  
90          if (resourceIns == null) {
91              log.trace("Entity was not resolved from classpath");
92              return null;
93          } else {
94              log.trace("Entity resolved from classpath");
95              return resourceIns;
96          }
97      }
98  
99      /**
100      * Implementation of DOM 3 {@link LSInput}.
101      */
102     protected class LSInputImpl implements LSInput {
103 
104         /** Public ID of the resolved resource. */
105         private String publicId;
106 
107         /** System ID of the resolved recource. */
108         private String systemId;
109 
110         /** Resolved resource. */
111         private BufferedInputStream buffInput;
112 
113         /**
114          * Constructor.
115          * 
116          * @param pubId public id of the resolved resource
117          * @param sysId system id of the resolved resource
118          * @param input resolved resource
119          */
120         public LSInputImpl(String pubId, String sysId, InputStream input) {
121             publicId = pubId;
122             systemId = sysId;
123             buffInput = new BufferedInputStream(input);
124         }
125 
126         /** {@inheritDoc} */
127         public String getBaseURI() {
128             return null;
129         }
130 
131         /** {@inheritDoc} */
132         public InputStream getByteStream() {
133             return buffInput;
134         }
135 
136         /** {@inheritDoc} */
137         public boolean getCertifiedText() {
138             return false;
139         }
140 
141         /** {@inheritDoc} */
142         public Reader getCharacterStream() {
143             return new InputStreamReader(buffInput);
144         }
145 
146         /** {@inheritDoc} */
147         public String getEncoding() {
148             return null;
149         }
150 
151         /** {@inheritDoc} */
152         public String getPublicId() {
153             return publicId;
154         }
155 
156         /** {@inheritDoc} */
157         public String getStringData() {
158             synchronized (buffInput) {
159                 try {
160                     buffInput.reset();
161                     byte[] input = new byte[buffInput.available()];
162                     buffInput.read(input);
163                     return new String(input);
164                 } catch (IOException e) {
165                     return null;
166                 }
167             }
168         }
169 
170         /** {@inheritDoc} */
171         public String getSystemId() {
172             return systemId;
173         }
174 
175         /** {@inheritDoc} */
176         public void setBaseURI(String uri) {
177         }
178 
179         /** {@inheritDoc} */
180         public void setByteStream(InputStream byteStream) {
181         }
182 
183         /** {@inheritDoc} */
184         public void setCertifiedText(boolean isCertifiedText) {
185         }
186 
187         /** {@inheritDoc} */
188         public void setCharacterStream(Reader characterStream) {
189         }
190 
191         /** {@inheritDoc} */
192         public void setEncoding(String encoding) {
193         }
194 
195         /** {@inheritDoc} */
196         public void setPublicId(String id) {
197         }
198 
199         /** {@inheritDoc} */
200         public void setStringData(String stringData) {
201         }
202 
203         /** {@inheritDoc} */
204         public void setSystemId(String id) {
205         }
206     }
207 }