Source code: joelib/io/types/cml/CMLResolver.java
1 ///////////////////////////////////////////////////////////////////////////////
2 //Filename: $RCSfile: CMLResolver.java,v $
3 //Purpose: Chemical Markup Language.
4 //Language: Java
5 //Compiler: JDK 1.4
6 //Authors: steinbeck@ice.mpg.de, gezelter@maul.chem.nd.edu,
7 // egonw@sci.kun.nl, wegnerj@informatik.uni-tuebingen.de
8 //Version: $Revision: 1.5 $
9 // $Date: 2003/08/22 15:56:18 $
10 // $Author: wegner $
11 //
12 //Copyright (C) 1997-2003 The Chemistry Development Kit (CDK) project
13 //Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
14 //
15 //This program is free software; you can redistribute it and/or
16 //modify it under the terms of the GNU Lesser General Public License
17 //as published by the Free Software Foundation; either version 2.1
18 //of the License, or (at your option) any later version.
19 //All we ask is that proper credit is given for our work, which includes
20 //- but is not limited to - adding the above copyright notice to the beginning
21 //of your source code files, and to any copyright notice that you may distribute
22 //with programs based on this work.
23 //
24 //This program is distributed in the hope that it will be useful,
25 //but WITHOUT ANY WARRANTY; without even the implied warranty of
26 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 //GNU Lesser General Public License for more details.
28 //
29 //You should have received a copy of the GNU Lesser General Public License
30 //along with this program; if not, write to the Free Software
31 //Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 ///////////////////////////////////////////////////////////////////////////////
33 package joelib.io.types.cml;
34
35 import java.io.*;
36
37 //import org.xml.sax.ext.EntityResolver2;
38 import java.net.URL;
39
40 import org.apache.log4j.*;
41
42 import org.xml.sax.*;
43
44
45 /**
46 * This class resolves DOCTYPE declaration for Chemical Markup Language (CML)
47 * files and uses a local version for validation. More information about
48 * CML can be found at http://www.xml-cml.org/.
49 *
50 * @author egonw
51 * @author c.steinbeck@uni-koeln.de
52 * @author gezelter@maul.chem.nd.edu
53 * @author wegnerj
54 * @license LGPL
55 * @cvsversion $Revision: 1.5 $, $Date: 2003/08/22 15:56:18 $
56 * @cite rr99b
57 * @cite mr01
58 * @cite gmrw01
59 * @cite wil01
60 **/
61 public class CMLResolver implements EntityResolver
62 {
63 //~ Static fields/initializers /////////////////////////////////////////////
64
65 // Obtain a suitable logger.
66 private static Category logger = Category.getInstance(
67 "joelib.io.types.cml.CMLResolver");
68
69 //~ Instance fields ////////////////////////////////////////////////////////
70
71 private String ddtResourceDir = "joelib/io/types/cml/data/";
72
73 //~ Constructors ///////////////////////////////////////////////////////////
74
75 public CMLResolver()
76 {
77 }
78
79 public CMLResolver(String _ddtResourceDir)
80 {
81 if (_ddtResourceDir != null)
82 {
83 ddtResourceDir = _ddtResourceDir;
84 }
85 }
86
87 //~ Methods ////////////////////////////////////////////////////////////////
88
89 /**
90 * Not implemented: always returns null.
91 **/
92 public InputSource getExternalSubset(String name, String baseURI)
93 {
94 return null;
95 }
96
97 /**
98 * Not implemented, but uses resolveEntity(String publicId, String systemId)
99 * instead.
100 **/
101 public InputSource resolveEntity(String name, String publicId,
102 String baseURI, String systemId)
103 {
104 return resolveEntity(publicId, systemId);
105 }
106
107 /**
108 * Resolves SYSTEM and PUBLIC identifiers for CML DTDs.
109 *
110 * @param publicId the PUBLIC identifier of the DTD (unused)
111 * @param systemId the SYSTEM identifier of the DTD
112 * @return the CML DTD as an InputSource or null if id's unresolvable
113 */
114 public InputSource resolveEntity(String publicId, String systemId)
115 {
116 if (logger.isDebugEnabled())
117 {
118 logger.debug("CMLResolver: resolving " + publicId + ", " +
119 systemId);
120 }
121
122 systemId = systemId.toLowerCase();
123
124 if ((systemId.indexOf("cml-1999-05-15.dtd") != -1) ||
125 (systemId.indexOf("cml.dtd") != -1) ||
126 (systemId.indexOf("cml1_0.dtd") != -1))
127 {
128 return getCMLType("cml1_0.dtd");
129 }
130 else if ((systemId.indexOf("cml-2001-04-06.dtd") != -1) ||
131 (systemId.indexOf("cml1_0_1.dtd") != -1))
132 {
133 return getCMLType("cml1_0_1.dtd");
134 }
135 else
136 {
137 logger.warn("Could not resolve " + systemId);
138
139 return null;
140 }
141 }
142
143 /**
144 * Returns an InputSource of the appropriate CML DTD. It accepts
145 * two CML DTD names: cml1_0.dtd and cml1_0_1.dtd. Returns null
146 * for any other name.
147 *
148 * @param type the name of the CML DTD version
149 * @return the InputSource to the CML DTD
150 */
151 private InputSource getCMLType(String type)
152 {
153 try
154 {
155 URL url = ClassLoader.getSystemResource(ddtResourceDir + type);
156
157 if (url == null)
158 {
159 logger.error("Error while trying to read CML DTD (" + type +
160 ") from " + ddtResourceDir);
161
162 return null;
163 }
164
165 return new InputSource(new BufferedReader(
166 new InputStreamReader(url.openStream())));
167 }
168 catch (Exception e)
169 {
170 logger.error("Error while trying to read CML DTD (" + type +
171 ") from " + ddtResourceDir + ":" + e.toString());
172
173 return null;
174 }
175 }
176 }
177 ///////////////////////////////////////////////////////////////////////////////
178 // END OF FILE.
179 ///////////////////////////////////////////////////////////////////////////////