KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > spi > conf > util > ParseException


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  */

22
23 package org.enhydra.spi.conf.util;
24
25 /**
26  * This exception is thrown when parse errors are encountered.
27  * You can explicitly create objects of this exception type by
28  * calling the method generateParseException in the generated
29  * parser.
30  *
31  * You can modify this class to customize your error reporting
32  * mechanisms so long as you retain the public fields.
33  */

34 public class ParseException extends Exception JavaDoc {
35
36   /**
37    * This constructor is used by the method "generateParseException"
38    * in the generated parser. Calling this constructor generates
39    * a new object of this type with the fields "currentToken",
40    * "expectedTokenSequences", and "tokenImage" set. The boolean
41    * flag "specialConstructor" is also set to true to indicate that
42    * this constructor was used to create this object.
43    * This constructor calls its super class with the empty string
44    * to force the "toString" method of parent class "Throwable" to
45    * print the error message in the form:
46    * ParseException: <result of getMessage>
47    *
48    * @param currentTokenVal Current token value.
49    * @param expectedTokenSequencesVal Expected token sequence of values.
50    * @param tokenImageVal Token image value.
51    */

52   public ParseException(Token currentTokenVal,
53                         int[][] expectedTokenSequencesVal,
54                         String JavaDoc[] tokenImageVal
55                        )
56   {
57     super("");
58     specialConstructor = true;
59     currentToken = currentTokenVal;
60     expectedTokenSequences = expectedTokenSequencesVal;
61     tokenImage = tokenImageVal;
62   }
63
64   /**
65    * The following constructors are for use by you for whatever
66    * purpose you can think of. Constructing the exception in this
67    * manner makes the exception behave in the normal way - i.e., as
68    * documented in the class "Throwable". The fields "errorToken",
69    * "expectedTokenSequences", and "tokenImage" do not contain
70    * relevant information. The JavaCC generated code does not use
71    * these constructors.
72    */

73
74   public ParseException() {
75     super();
76     specialConstructor = false;
77   }
78
79   public ParseException(String JavaDoc message) {
80     super(message);
81     specialConstructor = false;
82   }
83
84   /**
85    * This variable determines which constructor was used to create
86    * this object and thereby affects the semantics of the
87    * "getMessage" method (see below).
88    */

89   protected boolean specialConstructor;
90
91   /**
92    * This is the last token that has been consumed successfully. If
93    * this object has been created due to a parse error, the token
94    * followng this token will (therefore) be the first error token.
95    */

96   public Token currentToken;
97
98   /**
99    * Each entry in this array is an array of integers. Each array
100    * of integers represents a sequence of tokens (by their ordinal
101    * values) that is expected at this point of the parse.
102    */

103   public int[][] expectedTokenSequences;
104
105   /**
106    * This is a reference to the "tokenImage" array of the generated
107    * parser within which the parse error occurred. This array is
108    * defined in the generated ...Constants interface.
109    */

110   public String JavaDoc[] tokenImage;
111
112   /**
113    * This method has the standard behavior when this object has been
114    * created using the standard constructors. Otherwise, it uses
115    * "currentToken" and "expectedTokenSequences" to generate a parse
116    * error message and returns it. If this object has been created
117    * due to a parse error, and you do not catch it (it gets thrown
118    * from the parser), then this method is called during the printing
119    * of the final stack trace, and hence the correct error message
120    * gets displayed.#
121    *
122    * @return The message.
123    */

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

167   protected String JavaDoc eol = System.getProperty("line.separator", "\n");
168
169   /**
170    * Used to convert raw characters to their escaped version
171    * when these raw version cannot be used as part of an ASCII
172    * string literal.
173    *
174    * @param str String to be transformed.
175    * @return String in which are raw characters converted to their escaped version;
176    */

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