KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > deployment > ejbql > ParseException


1 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
2 package org.objectweb.jonas_ejb.deployment.ejbql;
3
4 /**
5  * Initialy generated by JJTree, this class was modified to get specific error reporting
6  * @author Christophe Ney, Consultant cney@batisseurs.com
7  *
8  * This exception is thrown when parse errors are encountered.
9  * You can explicitly create objects of this exception type by
10  * calling the method generateParseException in the generated
11  * parser.
12  *
13  * You can modify this class to customize your error reporting
14  * mechanisms so long as you retain the public fields.
15  */

16 public class ParseException extends Exception JavaDoc {
17
18   /**
19    * This constructor is used by the method "generateParseException"
20    * in the generated parser. Calling this constructor generates
21    * a new object of this type with the fields "currentToken",
22    * "expectedTokenSequences", and "tokenImage" set. The boolean
23    * flag "specialConstructor" is also set to true to indicate that
24    * this constructor was used to create this object.
25    * This constructor calls its super class with the empty string
26    * to force the "toString" method of parent class "Throwable" to
27    * print the error message in the form:
28    * ParseException: <result of getMessage>
29    */

30   public ParseException(Token currentTokenVal,
31                         int[][] expectedTokenSequencesVal,
32                         String JavaDoc[] tokenImageVal
33                        )
34   {
35     super("");
36     specialConstructor = true;
37     currentToken = currentTokenVal;
38     expectedTokenSequences = expectedTokenSequencesVal;
39     tokenImage = tokenImageVal;
40   }
41
42   /**
43    * The following constructors are for use by you for whatever
44    * purpose you can think of. Constructing the exception in this
45    * manner makes the exception behave in the normal way - i.e., as
46    * documented in the class "Throwable". The fields "errorToken",
47    * "expectedTokenSequences", and "tokenImage" do not contain
48    * relevant information. The JavaCC generated code does not use
49    * these constructors.
50    */

51
52   public ParseException() {
53     super();
54     specialConstructor = false;
55   }
56
57   public ParseException(String JavaDoc message) {
58     super(message);
59     specialConstructor = false;
60   }
61
62   /**
63    * This variable determines which constructor was used to create
64    * this object and thereby affects the semantics of the
65    * "getMessage" method (see below).
66    */

67   protected boolean specialConstructor;
68
69   /**
70    * This is the last token that has been consumed successfully. If
71    * this object has been created due to a parse error, the token
72    * followng this token will (therefore) be the first error token.
73    */

74   public Token currentToken;
75
76   /**
77    * Each entry in this array is an array of integers. Each array
78    * of integers represents a sequence of tokens (by their ordinal
79    * values) that is expected at this point of the parse.
80    */

81   public int[][] expectedTokenSequences;
82
83   /**
84    * This is a reference to the "tokenImage" array of the generated
85    * parser within which the parse error occurred. This array is
86    * defined in the generated ...Constants interface.
87    */

88   public String JavaDoc[] tokenImage;
89
90   /**
91    * This method has the standard behavior when this object has been
92    * created using the standard constructors. Otherwise, it uses
93    * "currentToken" and "expectedTokenSequences" to generate a parse
94    * error message and returns it. If this object has been created
95    * due to a parse error, and you do not catch it (it gets thrown
96    * from the parser), then this method is called during the printing
97    * of the final stack trace, and hence the correct error message
98    * gets displayed.
99    */

100   public String JavaDoc getMessage(String JavaDoc query) {
101     if (!specialConstructor) {
102       return super.getMessage();
103     }
104     String JavaDoc expected = "";
105     int maxSize = 0;
106     for (int i = 0; i < expectedTokenSequences.length; i++) {
107       if (maxSize < expectedTokenSequences[i].length) {
108         maxSize = expectedTokenSequences[i].length;
109       }
110       for (int j = 0; j < expectedTokenSequences[i].length; j++) {
111         expected += tokenImage[expectedTokenSequences[i][j]] + " ";
112       }
113       if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
114         expected += "...";
115       }
116       expected += eol + " ";
117     }
118     String JavaDoc retval = "Encountered \"";
119     Token tok = currentToken.next;
120     for (int i = 0; i < maxSize; i++) {
121       if (i != 0) retval += " ";
122       if (tok.kind == 0) {
123         retval += tokenImage[0];
124         break;
125       }
126       retval += add_escapes(tok.image);
127       tok = tok.next;
128     }
129     // begin of ObjectWeb changes
130
// retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
131
if ((query==null) || ("".equals(query))) {
132         retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
133     } else {
134         // display the part of the query that was parsed (assuming one line)
135
retval += "\" after \""+query.substring(0,currentToken.next.beginColumn-1)+"\""+ eol;
136         retval += "in \""+query+"\"";
137     }
138     // end of ObjectWeb changes
139
retval += "." + eol;
140     if (expectedTokenSequences.length == 1) {
141       retval += "Was expecting:" + eol + " ";
142     } else {
143       retval += "Was expecting one of:" + eol + " ";
144     }
145     retval += expected;
146     return retval;
147   }
148
149   /**
150    * The end of line string for this machine.
151    */

152   protected String JavaDoc eol = System.getProperty("line.separator", "\n");
153  
154   /**
155    * Used to convert raw characters to their escaped version
156    * when these raw version cannot be used as part of an ASCII
157    * string literal.
158    */

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