KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
2 package org.apache.lucene.demo.html;
3
4 /**
5  * This exception is thrown when parse errors are encountered.
6  * You can explicitly create objects of this exception type by
7  * calling the method generateParseException in the generated
8  * parser.
9  *
10  * You can modify this class to customize your error reporting
11  * mechanisms so long as you retain the public fields.
12  */

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

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

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

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

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

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

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

99   public String JavaDoc getMessage() {
100     if (!specialConstructor) {
101       return super.getMessage();
102     }
103     StringBuffer JavaDoc expected = new StringBuffer JavaDoc();
104     int maxSize = 0;
105     for (int i = 0; i < expectedTokenSequences.length; i++) {
106       if (maxSize < expectedTokenSequences[i].length) {
107         maxSize = expectedTokenSequences[i].length;
108       }
109       for (int j = 0; j < expectedTokenSequences[i].length; j++) {
110         expected.append(tokenImage[expectedTokenSequences[i][j]]).append(" "); //$NON-NLS-1$
111
}
112       if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
113         expected.append("..."); //$NON-NLS-1$
114
}
115       expected.append(eol).append(" "); //$NON-NLS-1$
116
}
117     String JavaDoc retval = "Encountered \""; //$NON-NLS-1$
118
Token tok = currentToken.next;
119     for (int i = 0; i < maxSize; i++) {
120       if (i != 0) retval += " "; //$NON-NLS-1$
121
if (tok.kind == 0) {
122         retval += tokenImage[0];
123         break;
124       }
125       retval += add_escapes(tok.image);
126       tok = tok.next;
127     }
128     retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; //$NON-NLS-1$ //$NON-NLS-2$
129
retval += "." + eol; //$NON-NLS-1$
130
if (expectedTokenSequences.length == 1) {
131       retval += "Was expecting:" + eol + " "; //$NON-NLS-1$ //$NON-NLS-2$
132
} else {
133       retval += "Was expecting one of:" + eol + " "; //$NON-NLS-1$ //$NON-NLS-2$
134
}
135     retval += expected.toString();
136     return retval;
137   }
138
139   /**
140    * The end of line string for this machine.
141    */

142   protected String JavaDoc eol = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
143

144   /**
145    * Used to convert raw characters to their escaped version
146    * when these raw version cannot be used as part of an ASCII
147    * string literal.
148    */

149   protected String JavaDoc add_escapes(String JavaDoc str) {
150       StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
151       char ch;
152       for (int i = 0; i < str.length(); i++) {
153         switch (str.charAt(i))
154         {
155            case 0 :
156               continue;
157            case '\b':
158               retval.append("\\b"); //$NON-NLS-1$
159
continue;
160            case '\t':
161               retval.append("\\t"); //$NON-NLS-1$
162
continue;
163            case '\n':
164               retval.append("\\n"); //$NON-NLS-1$
165
continue;
166            case '\f':
167               retval.append("\\f"); //$NON-NLS-1$
168
continue;
169            case '\r':
170               retval.append("\\r"); //$NON-NLS-1$
171
continue;
172            case '\"':
173               retval.append("\\\""); //$NON-NLS-1$
174
continue;
175            case '\'':
176               retval.append("\\\'"); //$NON-NLS-1$
177
continue;
178            case '\\':
179               retval.append("\\\\"); //$NON-NLS-1$
180
continue;
181            default:
182               if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
183                  String JavaDoc s = "0000" + Integer.toString(ch, 16); //$NON-NLS-1$
184
retval.append("\\u" + s.substring(s.length() - 4, s.length())); //$NON-NLS-1$
185
} else {
186                  retval.append(ch);
187               }
188               continue;
189         }
190       }
191       return retval.toString();
192    }
193
194 }
195
Popular Tags