1
/* $Revision: 8042 $ $Author: egonw $ $Date: 2007-03-07 16:14:47 +0100 (Wed, 07 Mar 2007) $
3
* Copyright (C) 2001-2007 Egon Willighagen <egonw@users.sf.net>
5
* Contact: cdk-devel@lists.sourceforge.net
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public License
9
* as published by the Free Software Foundation; either version 2.1
10
* of the License, or (at your option) any later version.
11
* All we ask is that proper credit is given for our work, which includes
12
* - but is not limited to - adding the above copyright notice to the beginning
13
* of your source code files, and to any copyright notice that you may distribute
14
* with programs based on this work.
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU Lesser General Public License for more details.
21
* You should have received a copy of the GNU Lesser General Public License
22
* along with this program; if not, write to the Free Software
23
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26
package org.openscience.cdk.io;
28
import java.io.IOException;
29
import java.io.InputStream;
30
import java.io.InputStreamReader;
31
import java.io.Reader;
32
import java.io.StringReader;
33
import java.util.HashMap;
34
import java.util.Iterator;
37
import org.openscience.cdk.exception.CDKException;
38
import org.openscience.cdk.interfaces.IChemFile;
39
import org.openscience.cdk.interfaces.IChemObject;
40
import org.openscience.cdk.io.cml.CMLErrorHandler;
41
import org.openscience.cdk.io.cml.CMLHandler;
42
import org.openscience.cdk.io.cml.CMLResolver;
43
import org.openscience.cdk.io.cml.ICMLModule;
44
import org.openscience.cdk.io.formats.CMLFormat;
45
import org.openscience.cdk.io.formats.IResourceFormat;
46
import org.openscience.cdk.tools.LoggingTool;
47
import org.xml.sax.InputSource;
48
import org.xml.sax.SAXException;
49
import org.xml.sax.SAXParseException;
50
import org.xml.sax.XMLReader;
53
* Reads a molecule in CML 1.x and 2.0 format.
54
* CML is an XML based application {@cdk.cite PMR99}, and this Reader
55
* applies the method described in {@cdk.cite WIL01}.
57
* @author Egon L. Willighagen
58
* @cdk.created 2001-02-01
60
* @cdk.keyword file format, CML
64
public class CMLReader extends DefaultChemObjectReader {
66
private XMLReader parser;
70
private Map userConventions = new HashMap();
72
private LoggingTool logger;
75
* Define this CMLReader to take the input from a java.io.Reader
76
* class. Possible readers are (among others) StringReader and FileReader.
77
* FIXME: this can not be used in combination with Aelfred2 yet.
79
* @param input Reader type input
81
* @deprecated XML reading should not be done with a Reader, but with an
82
* InputStream instead.
84
public CMLReader(Reader input) {
90
* Reads CML from an java.io.InputStream, for example the FileInputStream.
92
* @param input InputStream type input
94
public CMLReader(InputStream input) {
95
this(new InputStreamReader(input));
99
this(new StringReader(""));
102
public void registerConvention(String convention, ICMLModule conv) {
103
userConventions.put(convention, conv);
107
* Define this CMLReader to take the input from a java.io.Reader
108
* class. Possible readers are (among others) StringReader and FileReader.
110
* @param url String url which points to the file to be read
112
public CMLReader(String url) {
117
public IResourceFormat getFormat() {
118
return CMLFormat.getInstance();
121
public void setReader(Reader reader) throws CDKException {
125
public void setReader(InputStream input) throws CDKException {
126
setReader(new InputStreamReader(input));
129
private void init() {
130
logger = new LoggingTool(this);
132
url = ""; // make sure it is not null
134
boolean success = false;
135
// If JAXP is prefered (comes with Sun JVM 1.4.0 and higher)
138
javax.xml.parsers.SAXParserFactory spf = javax.xml.parsers.SAXParserFactory.newInstance();
139
spf.setNamespaceAware(true);
140
javax.xml.parsers.SAXParser saxParser = spf.newSAXParser();
141
parser = saxParser.getXMLReader();
142
logger.info("Using JAXP/SAX XML parser.");
144
} catch (Exception e) {
145
logger.warn("Could not instantiate JAXP/SAX XML reader: ", e.getMessage());
149
// Aelfred is first alternative.
152
parser = (XMLReader)this.getClass().getClassLoader().
153
loadClass("gnu.xml.aelfred2.XmlReader").
155
logger.info("Using Aelfred2 XML parser.");
157
} catch (Exception e) {
158
logger.warn("Could not instantiate Aelfred2 XML reader!");
162
// Xerces is second alternative
165
parser = (XMLReader)this.getClass().getClassLoader().
166
loadClass("org.apache.xerces.parsers.SAXParser").
168
logger.info("Using Xerces XML parser.");
170
} catch (Exception e) {
171
logger.warn("Could not instantiate Xerces XML reader!");
176
logger.error("Could not instantiate any XML parser!");
180
public boolean accepts(Class classObject) {
181
Class[] interfaces = classObject.getInterfaces();
182
for (int i=0; i<interfaces.length; i++) {
183
if (IChemFile.class.equals(interfaces[i])) return true;
186
if (IChemFile.class.equals(classObject))
192
* Read a IChemObject from input
194
* @return the content in a ChemFile object
196
public IChemObject read(IChemObject object) throws CDKException {
197
if (object instanceof IChemFile) {
198
return readChemFile((IChemFile)object);
200
throw new CDKException("Only supported is reading of ChemFile objects.");
206
private IChemFile readChemFile(IChemFile file) throws CDKException {
207
logger.debug("Started parsing from input...");
209
parser.setFeature("http://xml.org/sax/features/validation", false);
210
logger.info("Deactivated validation");
211
} catch (SAXException e) {
212
logger.warn("Cannot deactivate validation.");
215
CMLHandler handler = new CMLHandler(file);
216
// copy the manually added conventions
217
Iterator conventions = userConventions.keySet().iterator();
218
while (conventions.hasNext()) {
219
String conv = (String)conventions.next();
220
handler.registerConvention(conv, (ICMLModule)userConventions.get(conv));
222
parser.setContentHandler(handler);
223
parser.setEntityResolver(new CMLResolver());
224
parser.setErrorHandler(new CMLErrorHandler());
227
logger.debug("Parsing from URL: ", url);
230
logger.debug("Parsing from Reader");
231
parser.parse(new InputSource(input));
233
} catch (IOException e) {
234
String error = "Error while reading file: " + e.getMessage();
237
throw new CDKException(error, e);
238
} catch (SAXParseException saxe) {
239
SAXParseException spe = (SAXParseException)saxe;
240
String error = "Found well-formedness error in line " + spe.getLineNumber();
243
throw new CDKException(error, saxe);
244
} catch (SAXException saxe) {
245
String error = "Error while parsing XML: " + saxe.getMessage();
248
throw new CDKException(error, saxe);
253
public void close() throws IOException {