1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration2.resolver;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.net.FileNameMap;
22 import java.net.URL;
23 import java.net.URLConnection;
24 import java.util.Vector;
25
26 import org.apache.commons.configuration2.ex.ConfigurationException;
27 import org.apache.commons.configuration2.interpol.ConfigurationInterpolator;
28 import org.apache.commons.configuration2.io.ConfigurationLogger;
29 import org.apache.commons.configuration2.io.FileLocatorUtils;
30 import org.apache.commons.configuration2.io.FileSystem;
31 import org.apache.xml.resolver.CatalogException;
32 import org.apache.xml.resolver.readers.CatalogReader;
33 import org.xml.sax.EntityResolver;
34 import org.xml.sax.InputSource;
35 import org.xml.sax.SAXException;
36
37
38
39
40
41
42 public class CatalogResolver implements EntityResolver {
43
44
45
46 private static final int DEBUG_ALL = 9;
47
48
49
50
51 private static final int DEBUG_NORMAL = 4;
52
53
54
55
56 private static final int DEBUG_NONE = 0;
57
58
59
60
61 private final CatalogManager manager = new CatalogManager();
62
63
64
65
66 private FileSystem fs = FileLocatorUtils.DEFAULT_FILE_SYSTEM;
67
68
69
70
71 private org.apache.xml.resolver.tools.CatalogResolver resolver;
72
73
74
75
76 private ConfigurationLogger log;
77
78
79
80
81 public CatalogResolver() {
82 manager.setIgnoreMissingProperties(true);
83 manager.setUseStaticCatalog(false);
84 manager.setFileSystem(fs);
85 initLogger(null);
86 }
87
88
89
90
91
92
93 public void setCatalogFiles(final String catalogs) {
94 manager.setCatalogFiles(catalogs);
95 }
96
97
98
99
100
101
102 public void setFileSystem(final FileSystem fileSystem) {
103 this.fs = fileSystem;
104 manager.setFileSystem(fileSystem);
105 }
106
107
108
109
110
111
112 public void setBaseDir(final String baseDir) {
113 manager.setBaseDir(baseDir);
114 }
115
116
117
118
119
120
121 public void setInterpolator(final ConfigurationInterpolator ci) {
122 manager.setInterpolator(ci);
123 }
124
125
126
127
128
129
130 public void setDebug(final boolean debug) {
131 manager.setVerbosity(debug ? DEBUG_ALL : DEBUG_NONE);
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 @SuppressWarnings("resource")
158 @Override
159 public InputSource resolveEntity(final String publicId, final String systemId) throws SAXException {
160 String resolved = getResolver().getResolvedEntity(publicId, systemId);
161
162 if (resolved != null) {
163 final String badFilePrefix = "file://";
164 final String correctFilePrefix = "file:///";
165
166
167 if (resolved.startsWith(badFilePrefix) && !resolved.startsWith(correctFilePrefix)) {
168 resolved = correctFilePrefix + resolved.substring(badFilePrefix.length());
169 }
170
171 try {
172 final URL url = locate(fs, null, resolved);
173 if (url == null) {
174 throw new ConfigurationException("Could not locate " + resolved);
175 }
176 final InputStream inputStream = fs.getInputStream(url);
177 final InputSource inputSource = new InputSource(resolved);
178 inputSource.setPublicId(publicId);
179 inputSource.setByteStream(inputStream);
180 return inputSource;
181 } catch (final Exception e) {
182 log.warn("Failed to create InputSource for " + resolved, e);
183 }
184 }
185
186 return null;
187 }
188
189
190
191
192
193
194 public ConfigurationLogger getLogger() {
195 return log;
196 }
197
198
199
200
201
202
203
204
205
206 public void setLogger(final ConfigurationLogger log) {
207 initLogger(log);
208 }
209
210
211
212
213
214
215 private void initLogger(final ConfigurationLogger log) {
216 this.log = log != null ? log : ConfigurationLogger.newDummyLogger();
217 }
218
219 private synchronized org.apache.xml.resolver.tools.CatalogResolver getResolver() {
220 if (resolver == null) {
221 resolver = new org.apache.xml.resolver.tools.CatalogResolver(manager);
222 }
223 return resolver;
224 }
225
226
227
228
229
230
231
232
233
234 private static URL locate(final FileSystem fs, final String basePath, final String name) {
235 return FileLocatorUtils.locate(FileLocatorUtils.fileLocator().fileSystem(fs).basePath(basePath).fileName(name).create());
236 }
237
238
239
240
241 public static class CatalogManager extends org.apache.xml.resolver.CatalogManager {
242
243 private static org.apache.xml.resolver.Catalog staticCatalog;
244
245
246 private FileSystem fs;
247
248
249 private String baseDir = System.getProperty("user.dir");
250
251
252 private ConfigurationInterpolator interpolator;
253
254
255
256
257
258
259 public void setFileSystem(final FileSystem fileSystem) {
260 this.fs = fileSystem;
261 }
262
263
264
265
266
267
268 public FileSystem getFileSystem() {
269 return this.fs;
270 }
271
272
273
274
275
276
277 public void setBaseDir(final String baseDir) {
278 if (baseDir != null) {
279 this.baseDir = baseDir;
280 }
281 }
282
283
284
285
286
287
288 public String getBaseDir() {
289 return this.baseDir;
290 }
291
292
293
294
295
296
297 public void setInterpolator(final ConfigurationInterpolator configurationInterpolator) {
298 interpolator = configurationInterpolator;
299 }
300
301
302
303
304
305
306 public ConfigurationInterpolator getInterpolator() {
307 return interpolator;
308 }
309
310
311
312
313
314
315
316
317
318 @Override
319 public org.apache.xml.resolver.Catalog getPrivateCatalog() {
320 org.apache.xml.resolver.Catalog catalog = staticCatalog;
321
322 if (catalog == null || !getUseStaticCatalog()) {
323 try {
324 catalog = new Catalog();
325 catalog.setCatalogManager(this);
326 catalog.setupReaders();
327 catalog.loadSystemCatalogs();
328 } catch (final Exception ex) {
329 ex.printStackTrace();
330 }
331
332 if (getUseStaticCatalog()) {
333 staticCatalog = catalog;
334 }
335 }
336
337 return catalog;
338 }
339
340
341
342
343
344
345
346
347
348 @Override
349 public org.apache.xml.resolver.Catalog getCatalog() {
350 return getPrivateCatalog();
351 }
352 }
353
354
355
356
357 public static class Catalog extends org.apache.xml.resolver.Catalog {
358
359 private FileSystem fs;
360
361
362 private final FileNameMap fileNameMap = URLConnection.getFileNameMap();
363
364
365
366
367
368
369 @Override
370 public void loadSystemCatalogs() throws IOException {
371 fs = ((CatalogManager) catalogManager).getFileSystem();
372 final String base = ((CatalogManager) catalogManager).getBaseDir();
373
374
375 final Vector<String> catalogs = catalogManager.getCatalogFiles();
376 if (catalogs != null) {
377 for (int count = 0; count < catalogs.size(); count++) {
378 final String fileName = catalogs.elementAt(count);
379
380 URL url = null;
381 InputStream inputStream = null;
382
383 try {
384 url = locate(fs, base, fileName);
385 if (url != null) {
386 inputStream = fs.getInputStream(url);
387 }
388 } catch (final ConfigurationException ce) {
389 final String name = url.toString();
390
391 catalogManager.debug.message(DEBUG_ALL, "Unable to get input stream for " + name + ". " + ce.getMessage());
392 }
393 if (inputStream != null) {
394 final String mimeType = fileNameMap.getContentTypeFor(fileName);
395 try {
396 if (mimeType != null) {
397 parseCatalog(mimeType, inputStream);
398 continue;
399 }
400 } catch (final Exception ex) {
401
402 catalogManager.debug.message(DEBUG_ALL, "Exception caught parsing input stream for " + fileName + ". " + ex.getMessage());
403 } finally {
404 inputStream.close();
405 }
406 }
407 parseCatalog(base, fileName);
408 }
409 }
410
411 }
412
413
414
415
416
417
418
419
420 public void parseCatalog(final String baseDir, final String fileName) throws IOException {
421 base = locate(fs, baseDir, fileName);
422 catalogCwd = base;
423 default_override = catalogManager.getPreferPublic();
424 catalogManager.debug.message(DEBUG_NORMAL, "Parse catalog: " + fileName);
425
426 boolean parsed = false;
427
428 for (int count = 0; !parsed && count < readerArr.size(); count++) {
429 final CatalogReader reader = (CatalogReader) readerArr.get(count);
430 InputStream inputStream;
431
432 try {
433 inputStream = fs.getInputStream(base);
434 } catch (final Exception ex) {
435 catalogManager.debug.message(DEBUG_NORMAL, "Unable to access " + base + ex.getMessage());
436 break;
437 }
438
439 try {
440 reader.readCatalog(this, inputStream);
441 parsed = true;
442 } catch (final CatalogException ce) {
443 catalogManager.debug.message(DEBUG_NORMAL, "Parse failed for " + fileName + ce.getMessage());
444 if (ce.getExceptionType() == CatalogException.PARSE_FAILED) {
445 break;
446 }
447
448 continue;
449 } finally {
450 try {
451 inputStream.close();
452 } catch (final IOException ioe) {
453
454 inputStream = null;
455 }
456 }
457 }
458
459 if (parsed) {
460 parsePendingCatalogs();
461 }
462 }
463
464
465
466
467
468
469
470 @Override
471 protected String normalizeURI(final String uriref) {
472 final ConfigurationInterpolator ci = ((CatalogManager) catalogManager).getInterpolator();
473 final String resolved = ci != null ? String.valueOf(ci.interpolate(uriref)) : uriref;
474 return super.normalizeURI(resolved);
475 }
476 }
477 }