1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
35
36
37
38
39
40
41 public class ClasspathResolver implements EntityResolver, LSResourceResolver {
42
43
44 public static final String CLASSPATH_URI_SCHEME = "classpath:";
45
46
47 private final Logger log = LoggerFactory.getLogger(ClasspathResolver.class);
48
49
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
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
66
67
68
69
70
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
101
102 protected class LSInputImpl implements LSInput {
103
104
105 private String publicId;
106
107
108 private String systemId;
109
110
111 private BufferedInputStream buffInput;
112
113
114
115
116
117
118
119
120 public LSInputImpl(String pubId, String sysId, InputStream input) {
121 publicId = pubId;
122 systemId = sysId;
123 buffInput = new BufferedInputStream(input);
124 }
125
126
127 public String getBaseURI() {
128 return null;
129 }
130
131
132 public InputStream getByteStream() {
133 return buffInput;
134 }
135
136
137 public boolean getCertifiedText() {
138 return false;
139 }
140
141
142 public Reader getCharacterStream() {
143 return new InputStreamReader(buffInput);
144 }
145
146
147 public String getEncoding() {
148 return null;
149 }
150
151
152 public String getPublicId() {
153 return publicId;
154 }
155
156
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
171 public String getSystemId() {
172 return systemId;
173 }
174
175
176 public void setBaseURI(String uri) {
177 }
178
179
180 public void setByteStream(InputStream byteStream) {
181 }
182
183
184 public void setCertifiedText(boolean isCertifiedText) {
185 }
186
187
188 public void setCharacterStream(Reader characterStream) {
189 }
190
191
192 public void setEncoding(String encoding) {
193 }
194
195
196 public void setPublicId(String id) {
197 }
198
199
200 public void setStringData(String stringData) {
201 }
202
203
204 public void setSystemId(String id) {
205 }
206 }
207 }