1 package org.apache.turbine.services.intake.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 import java.net.URL;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.xml.sax.EntityResolver;
30 import org.xml.sax.InputSource;
31
32 /***
33 * A resolver to get the database.dtd file for the XML parser from the jar.
34 * This does not work with jdk1.3 on linux and OSX, see
35 * <a href="http://developer.java.sun.com/developer/bugParade/bugs/4337703.html">
36 * Bug 4337703</a>
37 *
38 * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
39 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
40 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
41 * @version $Id: DTDResolver.java 542370 2007-05-29 01:26:13Z seade $
42 */
43 public class DTDResolver implements EntityResolver
44 {
45 private static final String WEB_SITE_DTD =
46 "http://turbine.apache.org/dtd/intake_2_3_3.dtd";
47
48 /*** InputSource for <code>intake.dtd</code>. */
49 private InputSource intakeDTD = null;
50
51 /*** Logging */
52 private static Log log = LogFactory.getLog(DTDResolver.class);
53
54 /***
55 * constructor
56 */
57 public DTDResolver()
58 {
59 try
60 {
61 InputStream dtdStream =
62 getClass().getResourceAsStream("intake.dtd");
63
64
65
66
67 if (dtdStream != null)
68 {
69 intakeDTD = new InputSource(dtdStream);
70 }
71 else
72 {
73 log.warn("Could not located the intake.dtd");
74 }
75 }
76 catch (Exception ex)
77 {
78 log.error("Could not get stream for dtd", ex);
79 }
80 }
81
82 /***
83 * called by the XML parser
84 *
85 * @return an InputSource for the intake.dtd file
86 */
87 public InputSource resolveEntity(String publicId, String systemId)
88 {
89 if (intakeDTD != null && WEB_SITE_DTD.equals(systemId))
90 {
91 String pkg = getClass().getName()
92 .substring(0, getClass().getName().lastIndexOf("."));
93
94 log.info("Resolver: used intake.dtd from " +
95 pkg + " package ");
96
97 return intakeDTD;
98 }
99 else if (systemId == null)
100 {
101 log.info("Resolver: used intake.dtd from Turbine Web site");
102 return getInputSource(WEB_SITE_DTD);
103 }
104 else
105 {
106 log.info("Resolver: used System DTD for " + systemId);
107 return getInputSource(systemId);
108 }
109 }
110
111 /***
112 * Retrieves a XML input source for the specified URL.
113 *
114 * @param urlString The URL of the input source.
115 * @return <code>InputSource</code> for the URL.
116 */
117 private InputSource getInputSource(String urlString)
118 {
119 try
120 {
121 URL url = new URL(urlString);
122 return new InputSource(url.openStream());
123 }
124 catch (IOException ex)
125 {
126 log.error("Could not get InputSource for " + urlString, ex);
127 }
128 return new InputSource();
129 }
130 }