View Javadoc

1   package org.apache.turbine.services.intake.transform;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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              // getResource was buggy on many systems including Linux,
65              // OSX, and some versions of windows in jdk1.3.
66              // getResourceAsStream works on linux, maybe others?
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 }