KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > lucene > html > ParseException


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: ParseException.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.lucene.html;
21
22 /**
23  * This exception is thrown when parse errors are encountered. You can explicitly create objects of
24  * this exception type by calling the method generateParseException in the generated parser. You
25  * can modify this class to customize your error reporting mechanisms so long as you retain the
26  * public fields.
27  */

28 public class ParseException extends Exception JavaDoc {
29     /**
30      * This variable determines which constructor was used to create this object and thereby
31      * affects the semantics of the "getMessage" method (see below).
32      */

33     protected boolean specialConstructor;
34
35     /**
36      * This is the last token that has been consumed successfully. If this object has been created
37      * due to a parse error, the token followng this token will (therefore) be the first error
38      * token.
39      */

40     public Token currentToken;
41
42     /**
43      * Each entry in this array is an array of integers. Each array of integers represents a
44      * sequence of tokens (by their ordinal values) that is expected at this point of the parse.
45      */

46     public int[][] expectedTokenSequences;
47
48     /**
49      * This is a reference to the "tokenImage" array of the generated parser within which the parse
50      * error occurred. This array is defined in the generated ...Constants interface.
51      */

52     public String JavaDoc[] tokenImage;
53
54     /** The end of line string for this machine. */
55     protected String JavaDoc eol = System.getProperty("line.separator", "\n");
56
57     /**
58      * This constructor is used by the method "generateParseException" in the generated parser.
59      * Calling this constructor generates a new object of this type with the fields
60      * "currentToken", "expectedTokenSequences", and "tokenImage" set. The boolean flag
61      * "specialConstructor" is also set to true to indicate that this constructor was used to
62      * create this object. This constructor calls its super class with the empty string to force
63      * the "toString" method of parent class "Throwable" to print the error message in the form:
64      * ParseException: <result of getMessage>
65      *
66      * @param currentTokenVal DOCUMENT ME!
67      * @param expectedTokenSequencesVal DOCUMENT ME!
68      * @param tokenImageVal DOCUMENT ME!
69      */

70     public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal,
71         String JavaDoc[] tokenImageVal) {
72         super("");
73         specialConstructor = true;
74         currentToken = currentTokenVal;
75         expectedTokenSequences = expectedTokenSequencesVal;
76         tokenImage = tokenImageVal;
77     }
78
79     /**
80      * The following constructors are for use by you for whatever purpose you can think of.
81      * Constructing the exception in this manner makes the exception behave in the normal way -
82      * i.e., as documented in the class "Throwable". The fields "errorToken",
83      * "expectedTokenSequences", and "tokenImage" do not contain relevant information. The JavaCC
84      * generated code does not use these constructors.
85      */

86     public ParseException() {
87         super();
88         specialConstructor = false;
89     }
90
91     /**
92      * Creates a new ParseException object.
93      *
94      * @param message DOCUMENT ME!
95      */

96     public ParseException(String JavaDoc message) {
97         super(message);
98         specialConstructor = false;
99     }
100
101     /**
102      * This method has the standard behavior when this object has been created using the standard
103      * constructors. Otherwise, it uses "currentToken" and "expectedTokenSequences" to generate a
104      * parse error message and returns it. If this object has been created due to a parse error,
105      * and you do not catch it (it gets thrown from the parser), then this method is called during
106      * the printing of the final stack trace, and hence the correct error message gets displayed.
107      *
108      * @return DOCUMENT ME!
109      */

110     public String JavaDoc getMessage() {
111         if (!specialConstructor) {
112             return super.getMessage();
113         }
114
115         String JavaDoc expected = "";
116         int maxSize = 0;
117
118         for (int i = 0; i < expectedTokenSequences.length; i++) {
119             if (maxSize < expectedTokenSequences[i].length) {
120                 maxSize = expectedTokenSequences[i].length;
121             }
122
123             for (int j = 0; j < expectedTokenSequences[i].length; j++) {
124                 expected += (tokenImage[expectedTokenSequences[i][j]] + " ");
125             }
126
127             if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
128                 expected += "...";
129             }
130
131             expected += (eol + " ");
132         }
133
134         String JavaDoc retval = "Encountered \"";
135         Token tok = currentToken.next;
136
137         for (int i = 0; i < maxSize; i++) {
138             if (i != 0) {
139                 retval += " ";
140             }
141
142             if (tok.kind == 0) {
143                 retval += tokenImage[0];
144
145                 break;
146             }
147
148             retval += add_escapes(tok.image);
149             tok = tok.next;
150         }
151
152         retval += ("\" at line " + currentToken.next.beginLine + ", column " +
153         currentToken.next.beginColumn);
154         retval += ("." + eol);
155
156         if (expectedTokenSequences.length == 1) {
157             retval += ("Was expecting:" + eol + " ");
158         } else {
159             retval += ("Was expecting one of:" + eol + " ");
160         }
161
162         retval += expected;
163
164         return retval;
165     }
166
167     /**
168      * Used to convert raw characters to their escaped version when these raw version cannot be
169      * used as part of an ASCII string literal.
170      *
171      * @param str DOCUMENT ME!
172      *
173      * @return DOCUMENT ME!
174      */

175     protected String JavaDoc add_escapes(String JavaDoc str) {
176         StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
177         char ch;
178
179         for (int i = 0; i < str.length(); i++) {
180             switch (str.charAt(i)) {
181             case 0:
182
183                 continue;
184
185             case '\b':
186                 retval.append("\\b");
187
188                 continue;
189
190             case '\t':
191                 retval.append("\\t");
192
193                 continue;
194
195             case '\n':
196                 retval.append("\\n");
197
198                 continue;
199
200             case '\f':
201                 retval.append("\\f");
202
203                 continue;
204
205             case '\r':
206                 retval.append("\\r");
207
208                 continue;
209
210             case '\"':
211                 retval.append("\\\"");
212
213                 continue;
214
215             case '\'':
216                 retval.append("\\\'");
217
218                 continue;
219
220             case '\\':
221                 retval.append("\\\\");
222
223                 continue;
224
225             default:
226
227                 if (((ch = str.charAt(i)) < 0x20) || (ch > 0x7e)) {
228                     String JavaDoc s = "0000" + Integer.toString(ch, 16);
229                     retval.append("\\u" + s.substring(s.length() - 4, s.length()));
230                 } else {
231                     retval.append(ch);
232                 }
233
234                 continue;
235             }
236         }
237
238         return retval.toString();
239     }
240 }
241
Popular Tags