001    /*
002     * Copyright (c) 2005 Peter Palotas, Fredrik Johansson, Einar Pehrson,
003     * Sebastian Kekkonen, Lars Magnus Lång, Malin Johansson and Sofia Nilsson
004     *
005     * This file is part of
006     * CleanSheets Extension for Assertions
007     *
008     * CleanSheets Extension for Assertions is free software; you can
009     * redistribute it and/or modify it under the terms of the GNU General Public
010     * License as published by the Free Software Foundation; either version 2 of
011     * the License, or (at your option) any later version.
012     *
013     * CleanSheets Extension for Assertions is distributed in the hope that
014     * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
015     * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with CleanSheets Extension for Assertions; if not, write to the
020     * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
021     * Boston, MA  02111-1307  USA
022     */
023    package csheets.ext.assertion;
024    
025    /** Representing a parse error of an assertion. Thrown by Assertion().
026            @author Fredrik Johansson
027            @author Peter Palotas
028    */
029    public class AssertionException extends Exception {
030            static final long serialVersionUID = 2950555036431465919L;
031            private int line;
032            private int column;
033            private String errorMsg;
034    
035            public AssertionException(int line, int column, String errorMsg) {
036                    this.line = line;
037                    this.column = column;
038                    this.errorMsg = errorMsg;
039            }
040    
041            public AssertionException(String errorMsg) {
042                    this.line = this.column = -1;
043                    this.errorMsg = errorMsg;
044            }
045    
046            public AssertionException(antlr.RecognitionException re) {
047                    this.line = re.getLine();
048                    this.column = re.getColumn();
049                    this.errorMsg = re.toString();
050            }
051    
052            public AssertionException(antlr.TokenStreamException tse) {
053                    this.line = -1;
054                    this.column = -1;
055                    this.errorMsg = tse.toString();
056            }
057    
058            public AssertionException(antlr.CharStreamException cse) {
059                    this.line = -1;
060                    this.column = -1;
061                    this.errorMsg = cse.toString();
062            }
063    
064            public AssertionException(antlr.MismatchedCharException ex) {
065                    this.line = ex.getLine();
066                    this.column = ex.getColumn();
067                    this.errorMsg = ex.getMessage();
068            }
069    
070            public AssertionException(antlr.MismatchedTokenException ex) {
071                    this.line = ex.getLine();
072                    this.column = ex.getColumn();
073                    this.errorMsg = ex.getMessage();
074            }
075    
076            public AssertionException(antlr.NoViableAltException ex) {
077                    this.line = ex.getLine();
078                    this.column = ex.getColumn();
079                    this.errorMsg = ex.getMessage();
080            }
081    
082            public AssertionException(antlr.NoViableAltForCharException ex) {
083                    this.line = ex.getLine();
084                    this.column = ex.getColumn();
085                    this.errorMsg = ex.getMessage();
086            }
087    
088            /** Returns <code>true</code> if this exception contains information
089                    about where in the assertion string the error occured,
090                    meaning getLine() and getColumn() will return
091                    correct values. Returns <code>false</code> otherwise.
092            */
093            public boolean hasLocationInfo() {
094                    return (this.column != -1);
095            }
096    
097            /** Returns the line of the assertion on which this
098                    error occured if hasLocationInfo() returns
099                    <code>true</code>. Otherwise <code>-1</code> will
100                    be returned.
101            */
102            public int getLine() {
103                    return line;
104            }
105    
106            /** Returns the column of the assertion on which this
107                    error occured if hasLocationInfo() returns
108                    <code>true</code>. Otherwise <code>-1</code> will
109                    be returned.
110            */
111            public int getColumn() {
112                    return column;
113            }
114    
115            /** Returns the message of this error. */
116            public String getMessage() {
117                    return errorMsg;
118            }
119    
120            /** Returns a message describing this error.
121            */
122            public String toString() {
123                    String s = "Error parsing assertion:\n";
124                    if (hasLocationInfo())
125                            s += "col " + getColumn() + ": ";
126                    s += getMessage();
127                    return s;
128            }
129    }