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