KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > GrammarException


1 /*
2  * GrammarException.java
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2.1
7  * of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307, USA.
18  *
19  * Copyright (c) 2003-2005 Per Cederberg. All rights reserved.
20  */

21
22 package net.percederberg.grammatica;
23
24 /**
25  * A grammar validation exception. This exception is used for
26  * signalling an error in the grammar file.
27  *
28  * @author Per Cederberg, <per at percederberg dot net>
29  * @version 1.0
30  */

31 public class GrammarException extends Exception JavaDoc {
32
33     /**
34      * The grammar file name.
35      */

36     private String JavaDoc file;
37
38     /**
39      * The detailed error message.
40      */

41     private String JavaDoc message;
42
43     /**
44      * The first error line, or -1 for unknown.
45      */

46     private int startLine;
47
48     /**
49      * The last error line, or -1 for unknown.
50      */

51     private int endLine;
52
53     /**
54      * Creates a new grammar exception.
55      *
56      * @param file the grammar file name
57      * @param message the detailed error message
58      */

59     public GrammarException(String JavaDoc file, String JavaDoc message) {
60         this(file, message, -1, -1);
61     }
62
63     /**
64      * Creates a new grammar exception.
65      *
66      * @param file the grammar file name
67      * @param message the detailed error message
68      * @param startLine the starting line number, or -1 for unknown
69      * @param endLine the ending line number, or -1 for unknown
70      */

71     public GrammarException(String JavaDoc file,
72                             String JavaDoc message,
73                             int startLine,
74                             int endLine) {
75
76         this.file = file;
77         this.message = message;
78         this.startLine = startLine;
79         this.endLine = endLine;
80     }
81
82     /**
83      * Returns the grammar file name.
84      *
85      * @return the grammar file name
86      */

87     public String JavaDoc getFile() {
88         return file;
89     }
90
91     /**
92      * Returns the start line number for the error.
93      *
94      * @return the starting line number, or
95      * -1 if unknown
96      */

97     public int getStartLine() {
98         return startLine;
99     }
100
101     /**
102      * Returns the end line number for the error.
103      *
104      * @return the ending line number, or
105      * -1 if unknown
106      */

107     public int getEndLine() {
108         return endLine;
109     }
110
111     /**
112      * Returns the detailed error message. This message will contain
113      * the same string as getErrorMessage(), but with line number
114      * information appended.
115      *
116      * @return the detailed error message
117      */

118     public String JavaDoc getMessage() {
119         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
120
121         // Add error description
122
buffer.append(getErrorMessage());
123
124         // Add line numbers
125
if (startLine > 0 && endLine > 0) {
126             if (startLine == endLine) {
127                 buffer.append(", on line ");
128                 buffer.append(startLine);
129             } else {
130                 buffer.append(", on lines ");
131                 buffer.append(startLine);
132                 buffer.append("-");
133                 buffer.append(endLine);
134             }
135         }
136
137         return buffer.toString();
138     }
139
140     /**
141      * Returns the error message.
142      *
143      * @return the error message.
144      */

145     public String JavaDoc getErrorMessage() {
146         return message;
147     }
148 }
149
Popular Tags