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

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/io/cml/CMLErrorHandler.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: 7636 $ $Author: egonw $ $Date: 2007-01-04 18:46:10 +0100 (Thu, 04 Jan 2007) $
 
2
 *
 
3
 * Copyright (C) 1997-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.cml;
 
27
 
 
28
import org.openscience.cdk.tools.LoggingTool;
 
29
import org.xml.sax.ErrorHandler;
 
30
import org.xml.sax.SAXException;
 
31
import org.xml.sax.SAXParseException;
 
32
 
 
33
/**
 
34
 * CDK's SAX2 ErrorHandler for giving feedback on XML errors in the CML document.
 
35
 * Output is redirected to org.openscience.cdk.tools.LoggingTool.
 
36
 *
 
37
 * @cdk.module io
 
38
 *
 
39
 * @author Egon Willighagen <egonw@sci.kun.nl>
 
40
 **/
 
41
public class CMLErrorHandler implements ErrorHandler {
 
42
 
 
43
    private LoggingTool logger;
 
44
 
 
45
    public boolean reportErrors = true;
 
46
    public boolean abortOnErrors = false;
 
47
 
 
48
    /**
 
49
     * Constructor a SAX2 ErrorHandler that uses the cdk.tools.LoggingTool
 
50
     * class to output errors and warnings to.
 
51
     **/
 
52
    public CMLErrorHandler() {
 
53
        logger = new LoggingTool(this);
 
54
        logger.info("instantiated");
 
55
    }
 
56
 
 
57
    /**
 
58
     * Internal procedure that outputs an SAXParseException with a significance level
 
59
     * to the cdk.tools.LoggingTool logger.
 
60
     *
 
61
     * @param level     significance level
 
62
     * @param exception Exception to output
 
63
     */
 
64
    private void print (String level, SAXParseException exception) {
 
65
        if (level.equals("warning")) {
 
66
            logger.warn("** " + level + ": " + exception.getMessage ());
 
67
            logger.warn("   URI  = " + exception.getSystemId ());
 
68
            logger.warn("   line = " + exception.getLineNumber ());
 
69
        } else {
 
70
            logger.error("** " + level + ": " + exception.getMessage ());
 
71
            logger.error("   URI  = " + exception.getSystemId ());
 
72
            logger.error("   line = " + exception.getLineNumber ());
 
73
        }
 
74
    }
 
75
 
 
76
    // for recoverable errors, like validity problems
 
77
 
 
78
    /**
 
79
     * Outputs a SAXParseException error to the logger.
 
80
     *
 
81
     * @param exception   Exception to output
 
82
     **/
 
83
    public void error (SAXParseException exception) throws SAXException {
 
84
        if (reportErrors) print("error", exception);
 
85
        if (abortOnErrors) throw exception;
 
86
    }
 
87
 
 
88
    /**
 
89
     * Outputs as fatal SAXParseException error to the logger.
 
90
     *
 
91
     * @param exception   Exception to output
 
92
     **/
 
93
    public void fatalError (SAXParseException exception) throws SAXException {
 
94
        if (reportErrors) print("fatal", exception);
 
95
        if (abortOnErrors) throw exception;
 
96
    }
 
97
 
 
98
    /**
 
99
     * Outputs a SAXParseException warning to the logger.
 
100
     *
 
101
     * @param exception   Exception to output
 
102
     **/
 
103
    public void warning (SAXParseException exception) throws SAXException {
 
104
        if (reportErrors) print("warning", exception);
 
105
    }
 
106
 
 
107
}
 
108