KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > util > cc > html > ParseException


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35
36 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
37 package com.micronova.util.cc.html;
38
39 /**
40  * This exception is thrown when parse errors are encountered.
41  * You can explicitly create objects of this exception type by
42  * calling the method generateParseException in the generated
43  * parser.
44  *
45  * You can modify this class to customize your error reporting
46  * mechanisms so long as you retain the public fields.
47  */

48 public class ParseException extends Exception JavaDoc {
49
50   /**
51    * This constructor is used by the method "generateParseException"
52    * in the generated parser. Calling this constructor generates
53    * a new object of this type with the fields "currentToken",
54    * "expectedTokenSequences", and "tokenImage" set. The boolean
55    * flag "specialConstructor" is also set to true to indicate that
56    * this constructor was used to create this object.
57    * This constructor calls its super class with the empty string
58    * to force the "toString" method of parent class "Throwable" to
59    * print the error message in the form:
60    * ParseException: <result of getMessage>
61    */

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

83
84   public ParseException() {
85     super();
86     specialConstructor = false;
87   }
88
89   public ParseException(String JavaDoc message) {
90     super(message);
91     specialConstructor = false;
92   }
93
94   /**
95    * This variable determines which constructor was used to create
96    * this object and thereby affects the semantics of the
97    * "getMessage" method (see below).
98    */

99   protected boolean specialConstructor;
100
101   /**
102    * This is the last token that has been consumed successfully. If
103    * this object has been created due to a parse error, the token
104    * followng this token will (therefore) be the first error token.
105    */

106   public Token currentToken;
107
108   /**
109    * Each entry in this array is an array of integers. Each array
110    * of integers represents a sequence of tokens (by their ordinal
111    * values) that is expected at this point of the parse.
112    */

113   public int[][] expectedTokenSequences;
114
115   /**
116    * This is a reference to the "tokenImage" array of the generated
117    * parser within which the parse error occurred. This array is
118    * defined in the generated ...Constants interface.
119    */

120   public String JavaDoc[] tokenImage;
121
122   /**
123    * This method has the standard behavior when this object has been
124    * created using the standard constructors. Otherwise, it uses
125    * "currentToken" and "expectedTokenSequences" to generate a parse
126    * error message and returns it. If this object has been created
127    * due to a parse error, and you do not catch it (it gets thrown
128    * from the parser), then this method is called during the printing
129    * of the final stack trace, and hence the correct error message
130    * gets displayed.
131    */

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

175   protected String JavaDoc eol = System.getProperty("line.separator", "\n");
176  
177   /**
178    * Used to convert raw characters to their escaped version
179    * when these raw version cannot be used as part of an ASCII
180    * string literal.
181    */

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