01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.workflow.loader;
06:
07: import org.xml.sax.EntityResolver;
08: import org.xml.sax.InputSource;
09: import org.xml.sax.SAXException;
10:
11: import java.io.IOException;
12: import java.io.InputStream;
13:
14: import java.net.MalformedURLException;
15: import java.net.URL;
16:
17: /**
18: * @author Hani Suleiman (hani@formicary.net)
19: * Date: Sep 14, 2003
20: * Time: 4:25:40 PM
21: */
22: public class DTDEntityResolver implements EntityResolver {
23: //~ Methods ////////////////////////////////////////////////////////////////
24:
25: public InputSource resolveEntity(String publicId, String systemId)
26: throws SAXException, IOException {
27: if (systemId == null) {
28: return null;
29: }
30:
31: try {
32: URL url = new URL(systemId);
33: String file = url.getFile();
34:
35: if ((file != null) && (file.indexOf('/') > -1)) {
36: file = file.substring(file.lastIndexOf('/') + 1);
37: }
38:
39: if ("www.opensymphony.com".equals(url.getHost())
40: && systemId.endsWith(".dtd")) {
41: InputStream is = getClass().getResourceAsStream(
42: "/META-INF/" + file);
43:
44: if (is == null) {
45: is = getClass().getResourceAsStream('/' + file);
46: }
47:
48: if (is != null) {
49: return new InputSource(is);
50: }
51: }
52: }
53: //modified by mbussetti - 15 nov 2004
54: //if the systemId isn't an URL, it is searched in the usual classpath
55: catch (MalformedURLException e) {
56: InputStream is = getClass().getResourceAsStream(
57: "/META-INF/" + systemId);
58:
59: if (is == null) {
60: is = getClass().getResourceAsStream('/' + systemId);
61: }
62:
63: if (is != null) {
64: return new InputSource(is);
65: }
66: }
67:
68: return null;
69: }
70: }
|