View Javadoc

1   package org.apache.torque.engine.database.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  import org.xml.sax.EntityResolver;
26  import org.xml.sax.InputSource;
27  import org.xml.sax.SAXException;
28  
29  /***
30   * A resolver to get the database.dtd file for the XML parser from the jar.
31   *
32   * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
33   * @author <a href="mailto:kschrader@karmalab.org">Kurt Schrader</a>
34   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
35   * @version $Id: DTDResolver.java 239624 2005-08-24 12:18:03Z henning $
36   */
37  public class DTDResolver implements EntityResolver
38  {
39      /*** Where the DTD is located on the web. */
40      public static final String WEB_SITE_DTD
41              = "http://db.apache.org/torque/dtd/database_3_2.dtd";
42  
43      /*** InputSource for <code>database.dtd</code>. */
44      private InputSource databaseDTD = null;
45  
46      /*** Logging class from commons.logging */
47      private static Log log = LogFactory.getLog(DTDResolver.class);
48  
49      /***
50       * constructor
51       */
52      public DTDResolver()
53              throws SAXException
54      {
55          try
56          {
57              InputStream dtdStream
58                      = getClass().getResourceAsStream("database.dtd");
59  
60              // getResource was buggy on many systems including Linux,
61              // OSX, and some versions of windows in jdk1.3.
62              // getResourceAsStream works on linux, maybe others?
63              if (dtdStream != null)
64              {
65                  databaseDTD = new InputSource(dtdStream);
66              }
67              else
68              {
69                  log.warn("Could not locate database.dtd");
70              }
71          }
72          catch (Exception ex)
73          {
74              throw new SAXException("Could not get stream for database.dtd", ex);
75          }
76      }
77  
78      /***
79       * An implementation of the SAX <code>EntityResolver</code>
80       * interface to be called by the XML parser.
81       *
82       * @param publicId The public identifier of the external entity
83       * @param systemId The system identifier of the external entity
84       * @return An <code>InputSource</code> for the
85       * <code>database.dtd</code> file.
86       */
87      public InputSource resolveEntity(String publicId, String systemId)
88              throws IOException
89      {
90          if (databaseDTD != null && WEB_SITE_DTD.equals(systemId))
91          {
92              String pkg = getClass().getName().substring(0,
93                      getClass().getName().lastIndexOf('.'));
94              log.info("Resolver: used database.dtd from '"
95                       + pkg + "' package");
96              return databaseDTD;
97          }
98          else if (systemId == null || "".equals(systemId.trim()))
99          {
100             log.info("Resolver: used '" + WEB_SITE_DTD + '\'');
101             return getInputSource(WEB_SITE_DTD);
102         }
103         else
104         {
105             log.info("Resolver: used '" + systemId + '\'');
106             return getInputSource(systemId);
107         }
108     }
109 
110     /***
111      * Retrieves a XML input source for the specified URL.
112      *
113      * @param urlString The URL of the input source.
114      * @return <code>InputSource</code> for the URL.
115      */
116     private InputSource getInputSource(String urlString)
117             throws IOException
118     {
119         URL url = new URL(urlString);
120         return new InputSource(url.openStream());
121     }
122 }