View Javadoc

1   /* This file is part of the project "Hilbert II" - http://www.qedeq.org" target="alexandria_uri">http://www.qedeq.org
2    *
3    * Copyright 2000-2014,  Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU General Public License for more details.
14   */
15  
16  package org.qedeq.kernel.xml.parser;
17  
18  import org.qedeq.base.io.SourceArea;
19  import org.qedeq.base.io.SourcePosition;
20  import org.qedeq.base.trace.Trace;
21  import org.qedeq.kernel.se.common.ModuleService;
22  import org.qedeq.kernel.se.common.SourceFileException;
23  import org.qedeq.kernel.se.common.SourceFileExceptionList;
24  import org.xml.sax.ErrorHandler;
25  import org.xml.sax.SAXException;
26  import org.xml.sax.SAXParseException;
27  
28  
29  /**
30   * Error handler for XML parsing.
31   *
32   * @author  Michael Meyling
33   */
34  public class SaxErrorHandler implements ErrorHandler {
35  
36      /** This class. */
37      private static final Class CLASS = SaxErrorHandler.class;
38  
39      /** Error code for Exceptions thrown by the SAXParser. */
40      public static final int SAX_PARSER_EXCEPTION = 9001;
41  
42      /** This plugin is currently working. */
43      private final ModuleService plugin;
44  
45      /** File that is parsed. */
46      private final String url;
47  
48      /** Collects errors. */
49      private final SourceFileExceptionList list;
50  
51      /**
52       * Constructor.
53       *
54       * @param   plugin  This plugin generated the error.
55       * @param   url     URL that is parsed.
56       * @param   list    Collector for the SAX exceptions.
57       */
58      public SaxErrorHandler(final ModuleService plugin, final String url,
59              final SourceFileExceptionList list) {
60          super();
61          Trace.param(CLASS, this, "SaxErrorHandler", "url", url);
62          this.plugin = plugin;
63          this.url = url;
64          this.list = list;
65      }
66  
67      /* (non-Javadoc)
68       * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
69       */
70      public final void warning(final SAXParseException e) throws SAXException {
71          final SourceFileException sf = new SourceFileException(plugin, SAX_PARSER_EXCEPTION, e.getMessage(),
72              e, new SourceArea(url, new SourcePosition(e.getLineNumber(), 1),
73              new SourcePosition(e.getLineNumber(), e.getColumnNumber())), null);
74          Trace.trace(CLASS, this, "warning", e);
75          Trace.trace(CLASS, this, "warning", sf);
76          list.add(sf);
77      }
78  
79      /* (non-Javadoc)
80       * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
81       */
82      public final void error(final SAXParseException e) throws SAXException {
83          final SourceFileException sf = new SourceFileException(plugin, SAX_PARSER_EXCEPTION, e.getMessage(),
84              e, new SourceArea(url, new SourcePosition(e.getLineNumber(), 1),
85              new SourcePosition(e.getLineNumber(), e.getColumnNumber())), null);
86          Trace.trace(CLASS, this, "error", e);
87          Trace.trace(CLASS, this, "error", sf);
88          list.add(sf);
89      }
90  
91      /* (non-Javadoc)
92       * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
93       */
94      public final void fatalError(final SAXParseException e) throws SAXException {
95          final SourceFileException sf = new SourceFileException(plugin, SAX_PARSER_EXCEPTION, e.getMessage(),
96              e, new SourceArea(url, new SourcePosition(e.getLineNumber(), 1),
97              new SourcePosition(e.getLineNumber(), e.getColumnNumber())), null);
98          Trace.trace(CLASS, this, "fatalError", e);
99          Trace.trace(CLASS, this, "fatalError", sf);
100         list.add(sf);
101     }
102 
103 }