1 package org.apache.torque.engine.database.transform;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.net.URL;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.xml.sax.EntityResolver;
26 import org.xml.sax.InputSource;
27 import org.xml.sax.SAXException;
28
29 /***
30 * A resolver to get the database.dtd file for the XML parser from the jar.
31 *
32 * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
33 * @author <a href="mailto:kschrader@karmalab.org">Kurt Schrader</a>
34 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
35 * @version $Id: DTDResolver.java 239624 2005-08-24 12:18:03Z henning $
36 */
37 public class DTDResolver implements EntityResolver
38 {
39 /*** Where the DTD is located on the web. */
40 public static final String WEB_SITE_DTD
41 = "http://db.apache.org/torque/dtd/database_3_2.dtd";
42
43 /*** InputSource for <code>database.dtd</code>. */
44 private InputSource databaseDTD = null;
45
46 /*** Logging class from commons.logging */
47 private static Log log = LogFactory.getLog(DTDResolver.class);
48
49 /***
50 * constructor
51 */
52 public DTDResolver()
53 throws SAXException
54 {
55 try
56 {
57 InputStream dtdStream
58 = getClass().getResourceAsStream("database.dtd");
59
60
61
62
63 if (dtdStream != null)
64 {
65 databaseDTD = new InputSource(dtdStream);
66 }
67 else
68 {
69 log.warn("Could not locate database.dtd");
70 }
71 }
72 catch (Exception ex)
73 {
74 throw new SAXException("Could not get stream for database.dtd", ex);
75 }
76 }
77
78 /***
79 * An implementation of the SAX <code>EntityResolver</code>
80 * interface to be called by the XML parser.
81 *
82 * @param publicId The public identifier of the external entity
83 * @param systemId The system identifier of the external entity
84 * @return An <code>InputSource</code> for the
85 * <code>database.dtd</code> file.
86 */
87 public InputSource resolveEntity(String publicId, String systemId)
88 throws IOException
89 {
90 if (databaseDTD != null && WEB_SITE_DTD.equals(systemId))
91 {
92 String pkg = getClass().getName().substring(0,
93 getClass().getName().lastIndexOf('.'));
94 log.info("Resolver: used database.dtd from '"
95 + pkg + "' package");
96 return databaseDTD;
97 }
98 else if (systemId == null || "".equals(systemId.trim()))
99 {
100 log.info("Resolver: used '" + WEB_SITE_DTD + '\'');
101 return getInputSource(WEB_SITE_DTD);
102 }
103 else
104 {
105 log.info("Resolver: used '" + systemId + '\'');
106 return getInputSource(systemId);
107 }
108 }
109
110 /***
111 * Retrieves a XML input source for the specified URL.
112 *
113 * @param urlString The URL of the input source.
114 * @return <code>InputSource</code> for the URL.
115 */
116 private InputSource getInputSource(String urlString)
117 throws IOException
118 {
119 URL url = new URL(urlString);
120 return new InputSource(url.openStream());
121 }
122 }