001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021
022package org.biojava.utils;
023
024/**
025 * Exception thrown when an error occurs in document parsing.
026 * It may optionally include the following fields:
027 *
028 * <pre>
029 *   Locator (file name, URL, etc.)
030 *   Line number (negative for unknown)
031 *   The text of the actual offending line (Null if unknown)
032 *   Character offset (negative for unknown)
033 * </pre>
034 * @author Matthew Pocock
035 * @author Greg Cox
036 */
037
038public class ParserException extends Exception {
039    private String locator = null;
040    private int lineNumber = -1;
041    private int character = -1;
042    private String line = null;
043
044    public ParserException(String detail) {
045        super(detail);
046    }
047
048    public ParserException(String detail, String locator) {
049        super(detail);
050        this.locator = locator;
051    }
052
053    public ParserException(String detail, String locator, int line) {
054        super(detail);
055        this.locator = locator;
056        this.lineNumber = line;
057    }
058
059    public ParserException(String detail,
060                          String locator,
061                          int lineNumber,
062                          String line)
063    {
064        super(detail);
065        this.locator = locator;
066        this.lineNumber = lineNumber;
067        this.line = line;
068    }
069
070    public ParserException(String detail,
071                          String locator,
072                          int lineNumber,
073                          String line,
074                          int character)
075    {
076        super(detail);
077        this.locator = locator;
078        this.lineNumber = lineNumber;
079        this.character = character;
080        this.line = line;
081    }
082
083    public ParserException(Throwable t) {
084      super(t);
085    }
086
087    /**
088     * @deprecated use new ParserException(detail, t)
089     */
090    public ParserException(Throwable t, String detail) {
091      this(detail, t);
092    }
093    
094    public ParserException(String message, Throwable cause) {
095      super(message, cause);
096    }
097
098    public ParserException(Throwable t, String detail, String locator) {
099        super(detail, t);
100        this.locator = locator;
101    }
102
103    public ParserException(Throwable t, String detail, String locator, int line) {
104        super(detail, t);
105        this.locator = locator;
106        this.lineNumber = line;
107    }
108
109    public ParserException(
110        Throwable t,
111        String detail,
112                          String locator,
113                          int lineNumber,
114                          String line)
115    {
116        super(detail, t);
117        this.locator = locator;
118        this.lineNumber = lineNumber;
119        this.line = line;
120    }
121
122    public ParserException(
123        Throwable t,
124        String detail,
125                          String locator,
126                          int lineNumber,
127                          String line,
128                          int character)
129    {
130        super(detail, t);
131        this.locator = locator;
132        this.lineNumber = lineNumber;
133        this.character = character;
134        this.line = line;
135    }
136
137    /**
138     * Get a locator for the stream which caused this exception.
139     *
140     * @return A locator string, or <code>null</code> if none is
141     *         known.
142     */
143
144    public String getLocator() {
145        return locator;
146    }
147
148    /**
149     * Get the line number in the stream where this exception occured.
150     *
151     * @return A positive integer line number, or -1 if not known.
152     */
153
154    public int getLineNumber() {
155        return lineNumber;
156    }
157
158    /**
159     * Get the character offset in the line where an error was detected.
160     *
161     * @return The first character in the line where the parser detected
162     *         an error, or -1 if the exception effects the whole line.
163     */
164
165    public int getCharacterOffset() {
166        return character;
167    }
168
169    /**
170     * Get the text of the line where the exception occured.
171     *
172     * @return The text of the line, or <code>null</code> if not known.
173     */
174
175    public String getLine() {
176        return line;
177    }
178
179    /**
180     * Represent this exception as a string.  This includes
181     * the default exception toString representation, followed
182     * by details of the location where the error occured, if
183     * they were supplied when constructing this exception.
184     *
185     * @return A string representation of this exception.
186     */
187
188    public String toString() {
189        StringBuffer sb = new StringBuffer(super.toString());
190        if (locator != null) {
191            sb.append('\n');
192            sb.append("Parsing location: ");
193            sb.append(locator);
194        }
195
196        if (lineNumber >= 0) {
197            sb.append('\n');
198            sb.append("Parsing line: ");
199            sb.append(lineNumber);
200        }
201
202        if (line != null) {
203            sb.append('\n');
204            sb.append(line);
205            if (character >= 0) {
206                sb.append('\n');
207                for (int i = 0; i < character; ++i)
208                    sb.append(' ');
209                sb.append('^');
210            }
211        }
212        sb.append('\n');
213        return sb.substring(0);
214    }
215}