View Javadoc

1   package org.apache.turbine.services.intake.transform;
2   
3   /*
4    * Copyright 2001-2005 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;
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  
26  import org.xml.sax.EntityResolver;
27  import org.xml.sax.InputSource;
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 264148 2005-08-29 14:21:04Z henning $
39   */
40  public class DTDResolver implements EntityResolver
41  {
42      private static final String 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 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 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(dtdStream);
67              }
68              else
69              {
70                  log.warn("Could not located the intake.dtd");
71              }
72          }
73          catch (Exception 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 resolveEntity(String publicId, String systemId)
85      {
86          if (intakeDTD != null && WEB_SITE_DTD.equals(systemId))
87          {
88              String 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 getInputSource(String urlString)
115     {
116         try
117         {
118             URL url = new URL(urlString);
119             return new InputSource(url.openStream());
120         }
121         catch (IOException ex)
122         {
123             log.error("Could not get InputSource for " + urlString, ex);
124         }
125         return new InputSource();
126     }
127 }