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
20
21
22 import java.io.IOException;
23 import java.io.InputStream;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.xml.sax.EntityResolver;
28 import org.xml.sax.InputSource;
29 import org.xml.sax.SAXException;
30
31 /***
32 * A resolver to get the database.dtd file for the XML parser from the jar.
33 *
34 * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
35 * @author <a href="mailto:kschrader@karmalab.org">Kurt Schrader</a>
36 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
37 * @version $Id: DTDResolver.java 491216 2006-12-30 12:14:45Z tfischer $
38 */
39 public class DTDResolver implements EntityResolver
40 {
41 /*** Where the DTD is located on the web. */
42 public static final String WEB_SITE_DTD
43 = "http://db.apache.org/torque/dtd/database_3_3.dtd";
44
45 /*** Where the 3.2 DTD is located on the web. */
46 public static final String WEB_SITE_DTD_3_2
47 = "http://db.apache.org/torque/dtd/database_3_2.dtd";
48
49 /*** Logging class from commons.logging */
50 private static Log log = LogFactory.getLog(DTDResolver.class);
51
52 /***
53 * constructor
54 */
55 public DTDResolver()
56 throws SAXException
57 {
58 }
59
60 /***
61 * An implementation of the SAX <code>EntityResolver</code>
62 * interface to be called by the XML parser. If the dtd is the
63 * current Torque DTD, the DTD is read from the generator jar.
64 * In all other cases, null is returned to indicate that the parser
65 * should open a regular connection to the systemId URI.
66 *
67 * @param publicId The public identifier of the external entity
68 * @param systemId The system identifier of the external entity
69 * @return An <code>InputSource</code> for the
70 * <code>database.dtd</code> file in the generator jar, or null.
71 */
72 public InputSource resolveEntity(String publicId, String systemId)
73 throws IOException, SAXException
74 {
75 if (WEB_SITE_DTD.equals(systemId))
76 {
77 return readFromClasspath("database.dtd");
78 }
79 else if (WEB_SITE_DTD_3_2.equals(systemId))
80 {
81 return readFromClasspath("database_3_2.dtd");
82 }
83 else
84 {
85 log.debug("Resolver: used default behaviour");
86 return null;
87 }
88 }
89
90 /***
91 * Reads the resource with the given name from the classpath.
92 *
93 * @param resourceName the name of the resource to read
94 * @return an Inputsource witht the content of the resource.
95 *
96 * @throws SAXException if the resource cannot be read.
97 */
98 private InputSource readFromClasspath(String resourceName)
99 throws SAXException
100 {
101 try
102 {
103 InputStream dtdStream
104 = getClass().getResourceAsStream(resourceName);
105
106
107
108
109 if (dtdStream != null)
110 {
111 String pkg = getClass().getName().substring(0,
112 getClass().getName().lastIndexOf('.'));
113 log.debug("Resolver: used " + resourceName + " from '"
114 + pkg + "' package");
115 return new InputSource(dtdStream);
116 }
117 else
118 {
119 log.warn("Could not locate database.dtd");
120 return null;
121 }
122 }
123 catch (Exception ex)
124 {
125 throw new SAXException(
126 "Could not get stream for " + resourceName,
127 ex);
128 }
129 }
130 }