SaxErrorHandler.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.kernel.xml.parser;
017 
018 import org.qedeq.base.io.SourceArea;
019 import org.qedeq.base.io.SourcePosition;
020 import org.qedeq.base.trace.Trace;
021 import org.qedeq.kernel.se.common.Plugin;
022 import org.qedeq.kernel.se.common.SourceFileException;
023 import org.qedeq.kernel.se.common.SourceFileExceptionList;
024 import org.xml.sax.ErrorHandler;
025 import org.xml.sax.SAXException;
026 import org.xml.sax.SAXParseException;
027 
028 
029 /**
030  * Error handler for XML parsing.
031  *
032  @author  Michael Meyling
033  */
034 public class SaxErrorHandler implements ErrorHandler {
035 
036     /** This class. */
037     private static final Class CLASS = SaxErrorHandler.class;
038 
039     /** Error code for Exceptions thrown by the SAXParser. */
040     public static final int SAX_PARSER_EXCEPTION = 9001;
041 
042     /** This plugin is currently working. */
043     private final Plugin plugin;
044 
045     /** File that is parsed. */
046     private final String url;
047 
048     /** Collects errors. */
049     private final SourceFileExceptionList list;
050 
051     /**
052      * Constructor.
053      *
054      @param   plugin  This plugin generated the error.
055      @param   url     URL that is parsed.
056      @param   list    Collector for the SAX exceptions.
057      */
058     public SaxErrorHandler(final Plugin plugin, final String url,
059             final SourceFileExceptionList list) {
060         super();
061         Trace.param(CLASS, this, "SaxErrorHandler""url", url);
062         this.plugin = plugin;
063         this.url = url;
064         this.list = list;
065     }
066 
067     /* (non-Javadoc)
068      * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
069      */
070     public final void warning(final SAXParseException ethrows SAXException {
071         final SourceFileException sf = new SourceFileException(plugin, SAX_PARSER_EXCEPTION, e.getMessage(),
072             e, new SourceArea(url, new SourcePosition(e.getLineNumber()1),
073             new SourcePosition(e.getLineNumber(), e.getColumnNumber()))null);
074         Trace.trace(CLASS, this, "warning", e);
075         Trace.trace(CLASS, this, "warning", sf);
076         list.add(sf);
077     }
078 
079     /* (non-Javadoc)
080      * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
081      */
082     public final void error(final SAXParseException ethrows SAXException {
083         final SourceFileException sf = new SourceFileException(plugin, SAX_PARSER_EXCEPTION, e.getMessage(),
084             e, new SourceArea(url, new SourcePosition(e.getLineNumber()1),
085             new SourcePosition(e.getLineNumber(), e.getColumnNumber()))null);
086         Trace.trace(CLASS, this, "error", e);
087         Trace.trace(CLASS, this, "error", sf);
088         list.add(sf);
089     }
090 
091     /* (non-Javadoc)
092      * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
093      */
094     public final void fatalError(final SAXParseException ethrows SAXException {
095         final SourceFileException sf = new SourceFileException(plugin, SAX_PARSER_EXCEPTION, e.getMessage(),
096             e, new SourceArea(url, new SourcePosition(e.getLineNumber()1),
097             new SourcePosition(e.getLineNumber(), e.getColumnNumber()))null);
098         Trace.trace(CLASS, this, "fatalError", e);
099         Trace.trace(CLASS, this, "fatalError", sf);
100         list.add(sf);
101     }
102 
103 }