View Javadoc

1   package org.devaki.nextobjects.util;
2   /*
3    *  nextobjects Copyright (C) 2001-2005 Emmanuel Florent
4    *  This program is free software; you can redistribute it and/or modify
5    *  it under the terms of the GNU General Public License as published by the
6    *  Free Software Foundation; either version 2 of the License, or (at your
7    *  option) any later version.
8    *  This program is distributed in the hope that it will
9    *  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
10   *  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   *  PURPOSE. See the GNU General Public License for more details.
12   *  You should have received a copy of the GNU General Public License along
13   *  with this program; if not, write to the Free Software Foundation, Inc., 59
14   *  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15   */
16  import java.io.IOException;
17  import java.io.InputStream;
18  import java.net.URL;
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.xml.sax.EntityResolver;
22  import org.xml.sax.InputSource;
23  /***
24   * A resolver to get the database.dtd file for the XML parser from the jar.
25   *  */
26  public class NODTDResolver implements EntityResolver
27  {
28      /*** the link to CDM dtd */
29      public static final String WEB_SITE_DTD_CDM =
30          "http://www.devaki.org/nextobjects/dtd/cdm_1_0.dtd";
31      /*** the old CDM dtd */
32      public static final String DTD_OLD_CDM = "dtd/cdm.dtd";
33      /*** the link to PDM dtd */
34      public static final String WEB_SITE_DTD_PDM =
35          "http://www.devaki.org/nextobjects/dtd/pdm_1_0.dtd";
36      /*** the link old PDM dtd */
37      public static final String DTD_OLD_PDM = "dtd/pdm.dtd";
38      /*** the link to Torque dtd */
39      public static final String WEB_SITE_DTD_TORQUE =
40          "http://db.apache.org/torque/dtd/database_3_1.dtd";
41      /*** the old Torque dtd */
42      public static final String WEB_SITE_DTD_TORQUE_NATIVE_3_0 =
43          "dtd/database_3_0.dtd";
44      /*** the old PDM dtd */
45      public static final String WEB_SITE_DTD_TORQUE_NATIVE_3_1 =
46          "dtd/database_3_1.dtd";
47      /***  the logger */
48      private static Log log =
49          LogFactory.getLog(NOXMLFactory.class.getName());
50      /***
51       * constructor do nothing
52       */
53      public NODTDResolver()
54      {
55      }
56      /***
57       * called by the XML parser
58       *@param publicId the DTD public id
59       *@param systemId the DTD system Id
60       * @return an InputSource for the intake.dtd file
61       */
62      public final InputSource resolveEntity(
63          final String publicId,
64          final String systemId)
65      {
66          if (WEB_SITE_DTD_CDM.equals(systemId))
67          {
68              String path = "dtd/cdm_1_0.dtd";
69              ClassLoader cl = this.getClass().getClassLoader();
70              InputStream dtdStream = cl.getResourceAsStream(path);
71              if (dtdStream == null)
72              {
73                  log.error("Can't load " + path);
74              }
75              return new InputSource(dtdStream);
76          }
77          else if (
78              WEB_SITE_DTD_PDM.equals(systemId)
79                  || DTD_OLD_PDM.equals(systemId))
80          {
81              String path = "dtd/pdm_1_0.dtd";
82              ClassLoader cl = this.getClass().getClassLoader();
83              InputStream dtdStream = cl.getResourceAsStream(path);
84              if (dtdStream == null)
85              {
86                  log.error("Can't load " + path);
87              }
88              return new InputSource(dtdStream);
89          }
90          else if (
91              WEB_SITE_DTD_TORQUE.equals(systemId)
92                  || WEB_SITE_DTD_TORQUE_NATIVE_3_0.equals(systemId)
93                  || WEB_SITE_DTD_TORQUE_NATIVE_3_1.equals(systemId))
94          {
95              String path = "dtd/database_3_1.dtd";
96              ClassLoader cl = this.getClass().getClassLoader();
97              InputStream dtdStream = cl.getResourceAsStream(path);
98              if (dtdStream == null)
99              {
100                 log.error("Can't load " + path);
101             }
102             return new InputSource(dtdStream);
103         }
104         else if (systemId == null)
105         {
106             log.error("Cannot solve this dtd.");
107             return null;
108         }
109         else
110         {
111             log.info("Resolver: used System DTD for " + systemId);
112             return getInputSource(systemId);
113         }
114     }
115     /***
116      * Retrieves a XML input source for the specified URL.
117      *
118      * @param urlString The URL of the input source.
119      * @return <code>InputSource</code> for the URL.
120      */
121     private InputSource getInputSource(final String urlString)
122     {
123         try
124         {
125             URL url = new URL(urlString);
126             return new InputSource(url.openStream());
127         }
128         catch (IOException ex)
129         {
130             log.error("Could not get InputSource for " + urlString, ex);
131         }
132         return new InputSource();
133     }
134 }