/* * @(#) ParserException.java - Class for custom parser exceptions. * (c) 2000 Ivan Maidanski http://ivmai.chat.ru * Freeware class library sources. All rights reserved. ** * Language: Java [pure] * Tested with: JDK v1.1.6 * Last modified: 2000-12-24 12:20:00 GMT+03:00 */ /* * This software is the proprietary information of the author. ** * Permission to use, copy, and distribute this software and its * documentation for non-commercial purposes and without fee is * hereby granted provided that this copyright notice appears in all * copies. ** * This software should not be modified in any way; any found bug * should be reported to the author. ** * The author disclaims all warranties with regard to this software, * including all implied warranties of merchantability and fitness. * In no event shall the author be liable for any special, indirect * or consequential damages or any damages whatsoever resulting from * loss of use, data or profits, whether in an action of contract, * negligence or other tortuous action, arising out of or in * connection with the use or performance of this software. */ package ivmai.util; /** * Class for custom parser exceptions. ** * This class extends the semantics of * NumberFormatException class and is used to signal * that a particular error has been reached at a particular index * unexpectedly while parsing a string or its region. Important * notes: this exception class may be overridden (if needed). ** * @see UnsignedInt * @see UnsignedLong * @see ByteVector ** * @version 2.0 * @author Ivan Maidanski ** * @since 1.8 */ public class ParserException extends NumberFormatException { /** * The class version unique identifier for serialization * interoperability. ** * @since 1.8 */ private static final long serialVersionUID = 2211498944992357376L; /** * The string containing the region being parsed. ** * Important notes: this value may be null. ** * @serial ** * @see ParserException#ParserException() * @see ParserException#ParserException(java.lang.String, int, int) * @see #getStr() * @see #index * @see #error */ protected String str; /** * The parsed string index at which this exception * occurs during the string region being parsed. ** * Important notes: index may be of any integer value; if * its value is negative then it should be treated as zero; if its * value is too big then it should be treated as * length() of str (only if str * is not null else it should be treated as zero). ** * @serial ** * @see ParserException#ParserException() * @see ParserException#ParserException(java.lang.String, int, int) * @see #getIndex() * @see #str * @see #error */ protected int index; /** * The error reason code explaining why this exception * is thrown during the string region being parsed. ** * Important notes: error may be of any integer value; if * its value is not positive then it should be treated as zero * (indicating an unknown error). ** * @serial ** * @see ParserException#ParserException() * @see ParserException#ParserException(java.lang.String, int, int) * @see #getError() * @see #str * @see #index */ protected int error; /** * The default no-argument exception constructor. ** * str is set to null, index and * error are set to zero. ** * @see ParserException#ParserException(java.lang.String, int, int) * @see #getMessage() */ public ParserException() {} /** * The standard parser exception constructor. ** * The meaning of a particular error code is custom-defined (zero * code indicates an unknown/unset error). ** * @param str * the string containing the region being parsed (may be * null, in fact). * @param index * the string index (may be of any integer value) at which parsing * has failed. * @param error * the reason code (may be of any integer value) explaining why * parsing has failed. ** * @see ParserException#ParserException() * @see #getMessage() * @see #getStr() * @see #getIndex() * @see #getError() */ public ParserException(String str, int index, int error) { super(str); this.str = str; this.index = index; this.error = error; } /** * Returns the string containing the region being parsed. ** * @return * the string (may be null) with the region being * parsed. ** * @see ParserException#ParserException(java.lang.String, int, int) * @see #getIndex() * @see #getError() */ public final String getStr() { return this.str; } /** * Returns the string index at which parsing has failed. ** * Important notes: result may be of any integer value; if its value * is negative then it should be treated as zero; if its value is * too big then it should be treated as length() of * getStr() (only if getStr() is not * null else it should be treated as zero). ** * @return * the string index (may be of any integer value) at which parsing * has failed. ** * @see ParserException#ParserException(java.lang.String, int, int) * @see #getStr() * @see #getError() */ public final int getIndex() { return this.index; } /** * Returns the error reason code explaining why parsing has failed. ** * The meaning of a particular error code is custom-defined. ** * @return * the reason code (may be of any integer value) explaining why * parsing has failed. ** * @see ParserException#ParserException(java.lang.String, int, int) * @see #getStr() * @see #getIndex() */ public final int getError() { return this.error; } /** * Returns the detailed message of the exception. ** * This method overrides the default getMessage() one. * The resulting string is a concatenation of the following: * "Error", error represented as a signed decimal * integer, ":", index represented as a signed decimal * integer too, ":" and str (if not null). ** * @return * the message string (may be null) describing * this parser exception. * @exception OutOfMemoryError * if there is not enough memory. ** * @see ParserException#ParserException(java.lang.String, int, int) * @see #getStr() * @see #getIndex() * @see #getError() */ public String getMessage() { int value = 15, digit = 0, shift = 0; String str; if ((str = this.str) != null && (value += str.length()) < 0) value = -1 >>> 1; StringBuffer sBuf = new StringBuffer(value); sBuf.append("Error"); if ((value = this.error) < 0) sBuf.append('-'); else value = -value; shift = 1; for (digit = value; (digit /= '9' - '0' + 1) < 0; shift *= '9' - '0' + 1); do { sBuf.append((char)('0' - (digit = value / shift))); if (shift <= 1) break; value -= digit * shift; shift /= '9' - '0' + 1; } while (true); sBuf.append(':'); if ((value = this.index) < 0) sBuf.append('-'); else value = -value; shift = 1; for (digit = value; (digit /= '9' - '0' + 1) < 0; shift *= '9' - '0' + 1); do { sBuf.append((char)('0' - (digit = value / shift))); if (shift <= 1) break; value -= digit * shift; shift /= '9' - '0' + 1; } while (true); sBuf.append(':'); if (str != null) sBuf.append(str); return new String(sBuf); } }