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