~ubuntu-branches/ubuntu/trusty/cdk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/io/CMLReader.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Revision: 8042 $ $Author: egonw $ $Date: 2007-03-07 16:14:47 +0100 (Wed, 07 Mar 2007) $
 
2
 *
 
3
 * Copyright (C) 2001-2007  Egon Willighagen <egonw@users.sf.net>
 
4
 *
 
5
 * Contact: cdk-devel@lists.sourceforge.net
 
6
 *
 
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.
 
15
 *
 
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.
 
20
 *
 
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.
 
24
 *
 
25
 */
 
26
package org.openscience.cdk.io;
 
27
 
 
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;
 
35
import java.util.Map;
 
36
 
 
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;
 
51
 
 
52
/**
 
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}.
 
56
 *
 
57
 * @author      Egon L. Willighagen
 
58
 * @cdk.created 2001-02-01
 
59
 * @cdk.module  io
 
60
 * @cdk.keyword file format, CML
 
61
 * @cdk.bug     1455346
 
62
 * @cdk.bug     1544406
 
63
 */
 
64
public class CMLReader extends DefaultChemObjectReader {
 
65
 
 
66
    private XMLReader parser;
 
67
    private Reader input;
 
68
    private String url;
 
69
    
 
70
    private Map userConventions = new HashMap();
 
71
 
 
72
    private LoggingTool logger;
 
73
 
 
74
    /**
 
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.
 
78
     *
 
79
     * @param input Reader type input
 
80
     * 
 
81
     * @deprecated XML reading should not be done with a Reader, but with an
 
82
     *             InputStream instead.
 
83
     */
 
84
    public CMLReader(Reader input) {
 
85
        this.init();
 
86
        this.input = input;
 
87
    }
 
88
    
 
89
    /**
 
90
     * Reads CML from an java.io.InputStream, for example the FileInputStream.
 
91
     *
 
92
     * @param input InputStream type input
 
93
     */
 
94
    public CMLReader(InputStream input) {
 
95
        this(new InputStreamReader(input));
 
96
    }
 
97
    
 
98
    public CMLReader() {
 
99
        this(new StringReader(""));
 
100
    }
 
101
    
 
102
    public void registerConvention(String convention, ICMLModule conv) {
 
103
        userConventions.put(convention, conv);
 
104
    }
 
105
 
 
106
    /**
 
107
     * Define this CMLReader to take the input from a java.io.Reader
 
108
     * class. Possible readers are (among others) StringReader and FileReader.
 
109
     *
 
110
     * @param url String url which points to the file to be read
 
111
     */
 
112
    public CMLReader(String url) {
 
113
        this.init();
 
114
        this.url = url;
 
115
    }
 
116
 
 
117
    public IResourceFormat getFormat() {
 
118
        return CMLFormat.getInstance();
 
119
    }
 
120
 
 
121
    public void setReader(Reader reader) throws CDKException {
 
122
        this.input = reader;
 
123
    }
 
124
 
 
125
    public void setReader(InputStream input) throws CDKException {
 
126
        setReader(new InputStreamReader(input));
 
127
    }
 
128
 
 
129
    private void init() {
 
130
        logger = new LoggingTool(this);
 
131
 
 
132
        url = ""; // make sure it is not null
 
133
 
 
134
        boolean success = false;
 
135
        // If JAXP is prefered (comes with Sun JVM 1.4.0 and higher)
 
136
        if (!success) {
 
137
            try {
 
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.");
 
143
                success = true;
 
144
            } catch (Exception e) {
 
145
                logger.warn("Could not instantiate JAXP/SAX XML reader: ", e.getMessage());
 
146
                logger.debug(e);
 
147
            }
 
148
        }
 
149
        // Aelfred is first alternative.
 
150
        if (!success) {
 
151
            try {
 
152
                parser = (XMLReader)this.getClass().getClassLoader().
 
153
                        loadClass("gnu.xml.aelfred2.XmlReader").
 
154
                        newInstance();
 
155
                logger.info("Using Aelfred2 XML parser.");
 
156
                success = true;
 
157
            } catch (Exception e) {
 
158
                logger.warn("Could not instantiate Aelfred2 XML reader!");
 
159
                logger.debug(e);
 
160
            }
 
161
        }
 
162
        // Xerces is second alternative
 
163
        if (!success) {
 
164
            try {
 
165
                parser = (XMLReader)this.getClass().getClassLoader().
 
166
                        loadClass("org.apache.xerces.parsers.SAXParser").
 
167
                        newInstance();
 
168
                logger.info("Using Xerces XML parser.");
 
169
                success = true;
 
170
            } catch (Exception e) {
 
171
                logger.warn("Could not instantiate Xerces XML reader!");
 
172
                logger.debug(e);
 
173
            }
 
174
        }
 
175
        if (!success) {
 
176
            logger.error("Could not instantiate any XML parser!");
 
177
        }
 
178
    }
 
179
 
 
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;
 
184
                }
 
185
                
 
186
                if (IChemFile.class.equals(classObject))
 
187
                        return true;
 
188
                return false;
 
189
        }
 
190
 
 
191
        /**
 
192
     * Read a IChemObject from input
 
193
     *
 
194
     * @return the content in a ChemFile object
 
195
     */
 
196
    public IChemObject read(IChemObject object) throws CDKException {
 
197
      if (object instanceof IChemFile) {
 
198
        return readChemFile((IChemFile)object);
 
199
      } else {
 
200
        throw new CDKException("Only supported is reading of ChemFile objects.");
 
201
      }
 
202
    }
 
203
 
 
204
    // private functions
 
205
 
 
206
    private IChemFile readChemFile(IChemFile file) throws CDKException {
 
207
        logger.debug("Started parsing from input...");
 
208
        try {
 
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.");
 
213
            return null;
 
214
        }
 
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));
 
221
        }
 
222
        parser.setContentHandler(handler);
 
223
        parser.setEntityResolver(new CMLResolver());
 
224
        parser.setErrorHandler(new CMLErrorHandler());
 
225
        try {
 
226
            if (input == null) {
 
227
                logger.debug("Parsing from URL: ", url);
 
228
                parser.parse(url);
 
229
            } else {
 
230
                logger.debug("Parsing from Reader");
 
231
                parser.parse(new InputSource(input));
 
232
            }
 
233
        } catch (IOException e) {
 
234
            String error = "Error while reading file: " + e.getMessage();
 
235
            logger.error(error);
 
236
            logger.debug(e);
 
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();
 
241
            logger.error(error);
 
242
            logger.debug(saxe);
 
243
            throw new CDKException(error, saxe);
 
244
        } catch (SAXException saxe) {
 
245
            String error = "Error while parsing XML: " + saxe.getMessage();
 
246
            logger.error(error);
 
247
            logger.debug(saxe);
 
248
            throw new CDKException(error, saxe);
 
249
        }
 
250
        return file;
 
251
    }
 
252
 
 
253
    public void close() throws IOException {
 
254
        input.close();
 
255
    }
 
256
 
 
257
}
 
258