KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > engine > database > transform > DTDResolver


1 package org.apache.torque.engine.database.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 import org.xml.sax.EntityResolver JavaDoc;
26 import org.xml.sax.InputSource JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
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,v 1.12 2004/10/12 22:02:01 dlr Exp $
36  */

37 public class DTDResolver implements EntityResolver JavaDoc
38 {
39     /** Where the DTD is located on the web. */
40     public static final String JavaDoc 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 JavaDoc 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 JavaDoc
54     {
55         try
56         {
57             InputStream JavaDoc 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 JavaDoc(dtdStream);
66             }
67             else
68             {
69                 log.warn("Could not locate database.dtd");
70             }
71         }
72         catch (Exception JavaDoc ex)
73         {
74             throw new SAXException JavaDoc("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 JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
88             throws IOException JavaDoc
89     {
90         if (databaseDTD != null && WEB_SITE_DTD.equals(systemId))
91         {
92             String JavaDoc 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 JavaDoc getInputSource(String JavaDoc urlString)
117             throws IOException JavaDoc
118     {
119         URL JavaDoc url = new URL JavaDoc(urlString);
120         return new InputSource JavaDoc(url.openStream());
121     }
122 }
123
Popular Tags