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