KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > intake > transform > DTDResolver


1 package org.apache.turbine.services.intake.transform;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.net.URL JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import org.xml.sax.EntityResolver JavaDoc;
27 import org.xml.sax.InputSource JavaDoc;
28
29 /**
30  * A resolver to get the database.dtd file for the XML parser from the jar.
31  * This does not work with jdk1.3 on linux and OSX, see
32  * <a HREF="http://developer.java.sun.com/developer/bugParade/bugs/4337703.html">
33  * Bug 4337703</a>
34  *
35  * @author <a HREF="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
36  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
37  * @author <a HREF="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
38  * @version $Id: DTDResolver.java,v 1.8.2.2 2004/05/20 03:06:49 seade Exp $
39  */

40 public class DTDResolver implements EntityResolver JavaDoc
41 {
42     private static final String JavaDoc WEB_SITE_DTD =
43             "http://jakarta.apache.org/turbine/dtd/intake_2_3.dtd";
44
45     /** InputSource for <code>intake.dtd</code>. */
46     private InputSource JavaDoc intakeDTD = null;
47
48     /** Logging */
49     private static Log log = LogFactory.getLog(DTDResolver.class);
50
51     /**
52      * constructor
53      */

54     public DTDResolver()
55     {
56         try
57         {
58             InputStream JavaDoc dtdStream =
59                     getClass().getResourceAsStream("intake.dtd");
60
61             // getResource was buggy on many systems including Linux,
62
// OSX, and some versions of windows in jdk1.3.
63
// getResourceAsStream works on linux, maybe others?
64
if (dtdStream != null)
65             {
66                 intakeDTD = new InputSource JavaDoc(dtdStream);
67             }
68             else
69             {
70                 log.warn("Could not located the intake.dtd");
71             }
72         }
73         catch (Exception JavaDoc ex)
74         {
75             log.error("Could not get stream for dtd", ex);
76         }
77     }
78
79     /**
80      * called by the XML parser
81      *
82      * @return an InputSource for the intake.dtd file
83      */

84     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
85     {
86         if (intakeDTD != null && WEB_SITE_DTD.equals(systemId))
87         {
88             String JavaDoc pkg = getClass().getName()
89                     .substring(0, getClass().getName().lastIndexOf("."));
90
91             log.info("Resolver: used intake.dtd from " +
92                     pkg + " package ");
93
94             return intakeDTD;
95         }
96         else if (systemId == null)
97         {
98             log.info("Resolver: used intake.dtd from Jakarta Web site");
99             return getInputSource(WEB_SITE_DTD);
100         }
101         else
102         {
103             log.info("Resolver: used System DTD for " + systemId);
104             return getInputSource(systemId);
105         }
106     }
107
108     /**
109      * Retrieves a XML input source for the specified URL.
110      *
111      * @param urlString The URL of the input source.
112      * @return <code>InputSource</code> for the URL.
113      */

114     private InputSource JavaDoc getInputSource(String JavaDoc urlString)
115     {
116         try
117         {
118             URL JavaDoc url = new URL JavaDoc(urlString);
119             return new InputSource JavaDoc(url.openStream());
120         }
121         catch (IOException JavaDoc ex)
122         {
123             log.error("Could not get InputSource for " + urlString, ex);
124         }
125         return new InputSource JavaDoc();
126     }
127 }
128
Popular Tags