View Javadoc
1   /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 0.7pre6 */
2   
3   /**
4    * Redistribution and use of this software and associated documentation
5    * ("Software"), with or without modification, are permitted provided
6    * that the following conditions are met:
7    *
8    * 1. Redistributions of source code must retain copyright
9    *    statements and notices.  Redistributions must also contain a
10   *    copy of this document.
11   *
12   * 2. Redistributions in binary form must reproduce the
13   *    above copyright notice, this list of conditions and the
14   *    following disclaimer in the documentation and/or other
15   *    materials provided with the distribution.
16   *
17   * 3. The name "Exolab" must not be used to endorse or promote
18   *    products derived from this Software without prior written
19   *    permission of Intalio, Inc.  For written permission,
20   *    please contact info@exolab.org.
21   *
22   * 4. Products derived from this Software may not be called "Exolab"
23   *    nor may "Exolab" appear in their names without prior written
24   *    permission of Intalio, Inc. Exolab is a registered
25   *    trademark of Intalio, Inc.
26   *
27   * 5. Due credit should be given to the Exolab Project
28   *    (http://www.exolab.org/).
29   *
30   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
31   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
32   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
33   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
34   * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
35   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
41   * OF THE POSSIBILITY OF SUCH DAMAGE.
42   *
43   * Copyright 2000 (C) Intalio Inc. All Rights Reserved.
44   *
45   * $Id$
46   */
47  
48  package org.exolab.castor.xml.dtd.parser;
49  
50  /**
51   * This exception is thrown when parse errors are encountered.
52   * You can explicitly create objects of this exception type by
53   * calling the method generateParseException in the generated
54   * parser.
55   * <p>
56   * You can modify this class to customize your error reporting
57   * mechanisms so long as you retain the public fields.
58   * @author Generated automatically by <b>JavaCC</b>
59   * @version Version 0.7pre6
60   */
61  public class ParseException extends Exception {
62      /** SerialVersionUID */
63      private static final long serialVersionUID = 5798523224831508600L;
64  
65    /**
66     * This constructor is used by the method "generateParseException"
67     * in the generated parser.  Calling this constructor generates
68     * a new object of this type with the fields "currentToken",
69     * "expectedTokenSequences", and "tokenImage" set.  The boolean
70     * flag "specialConstructor" is also set to true to indicate that
71     * this constructor was used to create this object.
72     * This constructor calls its super class with the empty string
73     * to force the "toString" method of parent class "Throwable" to
74     * print the error message in the form:
75     *     ParseException: <result of getMessage>
76     */
77    public ParseException(Token currentTokenVal,
78                          int[][] expectedTokenSequencesVal,
79                          String[] tokenImageVal
80                         )
81    {
82      super("");
83      specialConstructor = true;
84      currentToken = currentTokenVal;
85      expectedTokenSequences = expectedTokenSequencesVal;
86      tokenImage = tokenImageVal;
87    }
88  
89    /**
90     * The following constructors are for use by you for whatever
91     * purpose you can think of.  Constructing the exception in this
92     * manner makes the exception behave in the normal way - i.e., as
93     * documented in the class "Throwable".  The fields "errorToken",
94     * "expectedTokenSequences", and "tokenImage" do not contain
95     * relevant information.  The JavaCC generated code does not use
96     * these constructors.
97     */
98  
99    public ParseException() {
100     super();
101     specialConstructor = false;
102   }
103 
104   public ParseException(String message) {
105     super(message);
106     specialConstructor = false;
107   }
108 
109   /**
110    * This variable determines which constructor was used to create
111    * this object and thereby affects the semantics of the
112    * "getMessage" method (see below).
113    */
114   protected boolean specialConstructor;
115 
116   /**
117    * This is the last token that has been consumed successfully.  If
118    * this object has been created due to a parse error, the token
119    * followng this token will (therefore) be the first error token.
120    */
121   public Token currentToken;
122 
123   /**
124    * Each entry in this array is an array of integers.  Each array
125    * of integers represents a sequence of tokens (by their ordinal
126    * values) that is expected at this point of the parse.
127    */
128   public int[][] expectedTokenSequences;
129 
130   /**
131    * This is a reference to the "tokenImage" array of the generated
132    * parser within which the parse error occurred.  This array is
133    * defined in the generated ...Constants interface.
134    */
135   public String[] tokenImage;
136 
137   /**
138    * This method has the standard behavior when this object has been
139    * created using the standard constructors.  Otherwise, it uses
140    * "currentToken" and "expectedTokenSequences" to generate a parse
141    * error message and returns it.  If this object has been created
142    * due to a parse error, and you do not catch it (it gets thrown
143    * from the parser), then this method is called during the printing
144    * of the final stack trace, and hence the correct error message
145    * gets displayed.
146    */
147   public String getMessage() {
148     if (!specialConstructor) {
149       return super.getMessage();
150     }
151     String expected = "";
152     int maxSize = 0;
153     for (int i = 0; i < expectedTokenSequences.length; i++) {
154       if (maxSize < expectedTokenSequences[i].length) {
155         maxSize = expectedTokenSequences[i].length;
156       }
157       for (int j = 0; j < expectedTokenSequences[i].length; j++) {
158         expected += tokenImage[expectedTokenSequences[i][j]] + " ";
159       }
160       if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
161         expected += "...";
162       }
163       expected += eol + "    ";
164     }
165     String retval = "Encountered \"";
166     Token tok = currentToken.next;
167     for (int i = 0; i < maxSize; i++) {
168       if (i != 0) retval += " ";
169       if (tok.kind == 0) {
170         retval += tokenImage[0];
171         break;
172       }
173       retval += add_escapes(tok.image);
174       tok = tok.next;
175     }
176     retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn + "." + eol;
177     if (expectedTokenSequences.length == 1) {
178       retval += "Was expecting:" + eol + "    ";
179     } else {
180       retval += "Was expecting one of:" + eol + "    ";
181     }
182     retval += expected;
183     return retval;
184   }
185 
186   /**
187    * The end of line string for this machine.
188    */
189   protected String eol = System.getProperty("line.separator", "\n");
190 
191   /**
192    * Used to convert raw characters to their escaped version
193    * when these raw version cannot be used as part of an ASCII
194    * string literal.
195    */
196   protected String add_escapes(String str) {
197       StringBuffer retval = new StringBuffer();
198       char ch;
199       for (int i = 0; i < str.length(); i++) {
200         switch (str.charAt(i))
201         {
202            case 0 :
203               continue;
204            case '\b':
205               retval.append("\\b");
206               continue;
207            case '\t':
208               retval.append("\\t");
209               continue;
210            case '\n':
211               retval.append("\\n");
212               continue;
213            case '\f':
214               retval.append("\\f");
215               continue;
216            case '\r':
217               retval.append("\\r");
218               continue;
219            case '\"':
220               retval.append("\\\"");
221               continue;
222            case '\'':
223               retval.append("\\\'");
224               continue;
225            case '\\':
226               retval.append("\\\\");
227               continue;
228            default:
229               if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
230                  String s = "0000" + Integer.toString(ch, 16);
231                  retval.append("\\u" + s.substring(s.length() - 4, s.length()));
232               } else {
233                  retval.append(ch);
234               }
235               continue;
236         }
237       }
238       return retval.toString();
239    }
240 
241 }