KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > xpointer > parser > 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 package org.apache.cocoon.components.xpointer.parser;
17
18 /**
19  * This exception is thrown when parse errors are encountered.
20  * You can explicitly create objects of this exception type by
21  * calling the method generateParseException in the generated
22  * parser.
23  *
24  * You can modify this class to customize your error reporting
25  * mechanisms so long as you retain the public fields.
26  */

27 public class ParseException extends Exception JavaDoc {
28
29     /**
30      * This constructor is used by the method "generateParseException"
31      * in the generated parser. Calling this constructor generates
32      * a new object of this type with the fields "currentToken",
33      * "expectedTokenSequences", and "tokenImage" set. The boolean
34      * flag "specialConstructor" is also set to true to indicate that
35      * this constructor was used to create this object.
36      * This constructor calls its super class with the empty string
37      * to force the "toString" method of parent class "Throwable" to
38      * print the error message in the form:
39      * ParseException: <result of getMessage>
40      */

41     public ParseException(
42         Token currentTokenVal,
43         int[][] expectedTokenSequencesVal,
44         String JavaDoc[] tokenImageVal) {
45         super("");
46         specialConstructor = true;
47         currentToken = currentTokenVal;
48         expectedTokenSequences = expectedTokenSequencesVal;
49         tokenImage = tokenImageVal;
50     }
51
52     /**
53      * The following constructors are for use by you for whatever
54      * purpose you can think of. Constructing the exception in this
55      * manner makes the exception behave in the normal way - i.e., as
56      * documented in the class "Throwable". The fields "errorToken",
57      * "expectedTokenSequences", and "tokenImage" do not contain
58      * relevant information. The JavaCC generated code does not use
59      * these constructors.
60      */

61
62     public ParseException() {
63         super();
64         specialConstructor = false;
65     }
66
67     public ParseException(String JavaDoc message) {
68         super(message);
69         specialConstructor = false;
70     }
71
72     /**
73      * This variable determines which constructor was used to create
74      * this object and thereby affects the semantics of the
75      * "getMessage" method (see below).
76      */

77     protected boolean specialConstructor;
78
79     /**
80      * This is the last token that has been consumed successfully. If
81      * this object has been created due to a parse error, the token
82      * followng this token will (therefore) be the first error token.
83      */

84     public Token currentToken;
85
86     /**
87      * Each entry in this array is an array of integers. Each array
88      * of integers represents a sequence of tokens (by their ordinal
89      * values) that is expected at this point of the parse.
90      */

91     public int[][] expectedTokenSequences;
92
93     /**
94      * This is a reference to the "tokenImage" array of the generated
95      * parser within which the parse error occurred. This array is
96      * defined in the generated ...Constants interface.
97      */

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

110     public String JavaDoc getMessage() {
111         if (!specialConstructor) {
112             return super.getMessage();
113         }
114         StringBuffer JavaDoc expected = new StringBuffer JavaDoc();
115         int maxSize = 0;
116         for (int i = 0; i < expectedTokenSequences.length; i++) {
117             if (maxSize < expectedTokenSequences[i].length) {
118                 maxSize = expectedTokenSequences[i].length;
119             }
120             for (int j = 0; j < expectedTokenSequences[i].length; j++) {
121                 expected.append(tokenImage[expectedTokenSequences[i][j]]);
122                 expected.append(" ");
123             }
124             if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1]
125                 != 0) {
126                 expected.append("...");
127             }
128             expected.append(eol);
129             expected.append(" ");
130         }
131         StringBuffer JavaDoc retval = new StringBuffer JavaDoc("Encountered \"");
132         Token tok = currentToken.next;
133         for (int i = 0; i < maxSize; i++) {
134             if (i != 0)
135                 retval.append(" ");
136             if (tok.kind == 0) {
137                 retval.append(tokenImage[0]);
138                 break;
139             }
140             retval.append(add_escapes(tok.image));
141             tok = tok.next;
142         }
143         retval.append("\" at line ");
144         retval.append(currentToken.next.beginLine);
145         retval.append(", column ");
146         retval.append(currentToken.next.beginColumn);
147         retval.append(".");
148         retval.append(eol);
149         retval.append("Was expecting");
150         if (expectedTokenSequences.length != 1) {
151             retval.append(" one of");
152         }
153         retval.append(":");
154         retval.append(eol);
155         retval.append(" ");
156         retval.append(expected);
157         return retval.toString();
158     }
159
160     /**
161      * The end of line string for this machine.
162      */

163     protected String JavaDoc eol = System.getProperty("line.separator", "\n");
164
165     /**
166      * Used to convert raw characters to their escaped version
167      * when these raw version cannot be used as part of an ASCII
168      * string literal.
169      */

170     protected String JavaDoc add_escapes(String JavaDoc str) {
171         StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
172         char ch;
173         for (int i = 0; i < str.length(); i++) {
174             switch (str.charAt(i)) {
175                 case 0 :
176                     continue;
177                 case '\b' :
178                     retval.append("\\b");
179                     continue;
180                 case '\t' :
181                     retval.append("\\t");
182                     continue;
183                 case '\n' :
184                     retval.append("\\n");
185                     continue;
186                 case '\f' :
187                     retval.append("\\f");
188                     continue;
189                 case '\r' :
190                     retval.append("\\r");
191                     continue;
192                 case '\"' :
193                     retval.append("\\\"");
194                     continue;
195                 case '\'' :
196                     retval.append("\\\'");
197                     continue;
198                 case '\\' :
199                     retval.append("\\\\");
200                     continue;
201                 default :
202                     if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
203                         String JavaDoc s = "0000" + Integer.toString(ch, 16);
204                         retval.append(
205                             "\\u" + s.substring(s.length() - 4, s.length()));
206                     } else {
207                         retval.append(ch);
208                     }
209                     continue;
210             }
211         }
212         return retval.toString();
213     }
214
215 }
216
Popular Tags