1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.exolab.castor.util;
47
48 import java.net.URL;
49 import java.net.MalformedURLException;
50 import java.io.IOException;
51 import org.xml.sax.SAXException;
52 import org.xml.sax.EntityResolver;
53 import org.xml.sax.InputSource;
54 import org.exolab.castor.net.util.URIUtils;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 public class DTDResolver implements EntityResolver {
80
81
82
83
84 static class DTDInfo {
85
86
87
88
89 private final String publicId;
90
91
92
93
94 private final String systemId;
95
96
97
98
99 private final String resource;
100
101
102
103
104
105
106
107
108
109
110 DTDInfo(final String publicId,
111 final String systemId,
112 final String namespace,
113 final String prefix,
114 final String resource) {
115 this.publicId = publicId;
116 this.systemId = systemId;
117 this.resource = resource;
118 }
119 }
120
121
122
123
124
125 private final DTDInfo[] _dtdInfo = new DTDInfo[] {
126
127 new DTDInfo("-//EXOLAB/Castor Mapping DTD Version 1.0//EN",
128 "http://castor.exolab.org/mapping.dtd",
129 "castor.exolab.org", "castor",
130 "/org/exolab/castor/mapping/mapping.dtd"),
131 new DTDInfo("-//EXOLAB/Castor Mapping Schema Version 1.0//EN",
132 "http://castor.exolab.org/mapping.xsd",
133 "castor.exolab.org", "castor",
134 "/org/exolab/castor/mapping/mapping.xsd"),
135 new DTDInfo("-//EXOLAB/Castor Mapping DTD Version 1.0//EN",
136 "http://castor.org/mapping.dtd", "castor.org", "castor",
137 "/org/exolab/castor/mapping/mapping.dtd"),
138 new DTDInfo("-//EXOLAB/Castor Mapping Schema Version 1.0//EN",
139 "http://castor.org/mapping.xsd", "castor.org", "castor",
140 "/org/exolab/castor/mapping/mapping.xsd"),
141
142 new DTDInfo(
143 "-//EXOLAB/Castor JDO Configuration DTD Version 1.0//EN",
144 "http://castor.exolab.org/jdo-conf.dtd",
145 "castor.exolab.org", "castor",
146 "/org/castor/jdo/conf/jdo-conf.dtd"),
147 new DTDInfo(
148 "-//EXOLAB/Castor JDO Configuration Schema Version 1.0//EN",
149 "http://castor.exolab.org/jdo-conf.xsd",
150 "castor.exolab.org", "castor",
151 "/org/castor/jdo/conf/jdo-conf.xsd"),
152 new DTDInfo(
153 "-//EXOLAB/Castor JDO Configuration DTD Version 1.0//EN",
154 "http://castor.org/jdo-conf.dtd", "castor.org", "castor",
155 "/org/castor/jdo/conf/jdo-conf.dtd"),
156 new DTDInfo(
157 "-//EXOLAB/Castor JDO Configuration Schema Version 1.0//EN",
158 "http://castor.org/jdo-conf.xsd", "castor.org", "castor",
159 "/org/castor/jdo/conf/jdo-conf.xsd"),
160
161 new DTDInfo(
162 "-//W3C//DTD XMLSCHEMA 19991216//EN",
163 "http://www.w3.org/TR/2000/WD-xmlschema-1-20000225/structures.dtd",
164 null, null,
165 "/org/exolab/castor/util/resources/structures.dtd"),
166 new DTDInfo(
167 null,
168 "http://www.w3.org/TR/2000/WD-xmlschema-2-20000225/datatypes.dtd",
169 null, null,
170 "/org/exolab/castor/util/resources/datatypes.dtd"),
171 new DTDInfo(
172 null,
173 "http://www.w3.org/TR/2000/WD-xmlschema-1-20000225/structures.xsd",
174 null, null,
175 "/org/exolab/castor/util/resources/structures.xsd"),
176
177 };
178
179
180
181
182 private EntityResolver _resolver;
183
184
185
186
187 private URL _baseUrl;
188
189
190
191
192
193
194
195 public DTDResolver(EntityResolver resolver) {
196 _resolver = resolver;
197 }
198
199
200
201
202 public DTDResolver() {
203 }
204
205
206
207
208
209 public void setBaseURL(final URL baseUrl) {
210 _baseUrl = baseUrl;
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235 }
236
237
238
239
240
241 public URL getBaseURL() {
242 return _baseUrl;
243 }
244
245
246
247
248
249
250
251 public InputSource resolveEntity(final String publicId,
252 final String systemId) throws IOException, SAXException {
253 int i;
254 InputSource source = null;
255
256
257 for (i = 0; i < _dtdInfo.length; ++i) {
258 if (publicId != null && publicId.equals(_dtdInfo[i].publicId)) {
259 source = new InputSource(getClass().getResourceAsStream(
260 _dtdInfo[i].resource));
261 source.setPublicId(publicId);
262 return source;
263 }
264 if (systemId != null && systemId.equals(_dtdInfo[i].systemId)) {
265 source = new InputSource(getClass().getResourceAsStream(
266 _dtdInfo[i].resource));
267 source.setSystemId(systemId);
268 return source;
269 }
270 }
271
272
273 if (_resolver != null) {
274 source = _resolver.resolveEntity(publicId, systemId);
275 if (source != null) {
276 return source;
277 }
278 }
279
280
281
282 if (systemId != null && _baseUrl != null) {
283 URL url;
284
285 try {
286 url = new URL(_baseUrl, systemId);
287 source = new InputSource(url.openStream());
288 source.setSystemId(systemId);
289 return source;
290 } catch (MalformedURLException except) {
291 try {
292 String absURL = URIUtils.resolveAsString(systemId, _baseUrl
293 .toString());
294 url = new URL(absURL);
295 source = new InputSource(url.openStream());
296 source.setSystemId(systemId);
297 return source;
298 } catch (MalformedURLException ex2) {
299
300 }
301 } catch (java.io.FileNotFoundException fnfe) {
302 try {
303 String absURL = URIUtils.resolveAsString(systemId, _baseUrl
304 .toString());
305 url = new URL(absURL);
306 source = new InputSource(url.openStream());
307 source.setSystemId(systemId);
308 return source;
309 } catch (MalformedURLException ex2) {
310
311 }
312 }
313 return null;
314 }
315
316
317 return null;
318 }
319
320 }