001: /*
002: *******************************************************************************
003: * Copyright (C) 2003-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.text;
008:
009: import java.text.ParseException;
010:
011: /**
012: * Exception that signals an error has occurred while parsing the
013: * input to StringPrep or IDNA.
014: *
015: * @author Ram Viswanadha
016: * @stable ICU 2.8
017: */
018: public class StringPrepParseException extends ParseException {
019: // Generated by serialver from JDK 1.4.1_01
020: static final long serialVersionUID = 7160264827701651255L;
021:
022: /**
023: * @stable ICU 2.8
024: */
025: public static final int INVALID_CHAR_FOUND = 0;
026: /**
027: * @stable ICU 2.8
028: */
029: public static final int ILLEGAL_CHAR_FOUND = 1;
030: /**
031: * @stable ICU 2.8
032: */
033: public static final int PROHIBITED_ERROR = 2;
034: /**
035: * @stable ICU 2.8
036: */
037: public static final int UNASSIGNED_ERROR = 3;
038: /**
039: * @stable ICU 2.8
040: */
041: public static final int CHECK_BIDI_ERROR = 4;
042: /**
043: * @stable ICU 2.8
044: */
045: public static final int STD3_ASCII_RULES_ERROR = 5;
046: /**
047: * @stable ICU 2.8
048: */
049: public static final int ACE_PREFIX_ERROR = 6;
050: /**
051: * @stable ICU 2.8
052: */
053: public static final int VERIFICATION_ERROR = 7;
054: /**
055: * @stable ICU 2.8
056: */
057: public static final int LABEL_TOO_LONG_ERROR = 8;
058: /**
059: * @stable ICU 2.8
060: */
061: public static final int BUFFER_OVERFLOW_ERROR = 9;
062:
063: /**
064: * @stable ICU 2.2
065: */
066: public static final int ZERO_LENGTH_LABEL = 10;
067:
068: /**
069: * Construct a ParseException object with the given message
070: * and error code
071: *
072: * @param message A string describing the type of error that occurred
073: * @param error The error that has occurred
074: * @stable ICU 2.8
075: */
076: public StringPrepParseException(String message, int error) {
077: super (message, -1);
078: this .error = error;
079: this .line = 0;
080: }
081:
082: /**
083: * Construct a ParseException object with the given message and
084: * error code
085: *
086: * @param message A string describing the type of error that occurred
087: * @param error The error that has occurred
088: * @param rules The input rules string
089: * @param pos The position of error in the rules string
090: * @stable ICU 2.8
091: */
092: public StringPrepParseException(String message, int error,
093: String rules, int pos) {
094: super (message, -1);
095: this .error = error;
096: setContext(rules, pos);
097: this .line = 0;
098: }
099:
100: /**
101: * Construct a ParseException object with the given message and error code
102: *
103: * @param message A string describing the type of error that occurred
104: * @param error The error that has occurred
105: * @param rules The input rules string
106: * @param pos The position of error in the rules string
107: * @param lineNumber The line number at which the error has occurred.
108: * If the parse engine is not using this field, it should set it to zero. Otherwise
109: * it should be a positive integer. The default value of this field
110: * is -1. It will be set to 0 if the code populating this struct is not
111: * using line numbers.
112: * @stable ICU 2.8
113: */
114: public StringPrepParseException(String message, int error,
115: String rules, int pos, int lineNumber) {
116: super (message, -1);
117: this .error = error;
118: setContext(rules, pos);
119: this .line = lineNumber;
120: }
121:
122: /**
123: * Compare this ParseException to another and evaluate if they are equal.
124: * The comparison works only on the type of error and does not compare
125: * the rules strings, if any, for equality.
126: *
127: * @param other The exception that this object should be compared to
128: * @return true if the objects are equal, false if unequal
129: * @stable ICU 2.8
130: */
131: public boolean equals(Object other) {
132: if (!(other instanceof StringPrepParseException)) {
133: return false;
134: }
135: return ((StringPrepParseException) other).error == this .error;
136:
137: }
138:
139: /**
140: * Returns the position of error in the rules string
141: *
142: * @return String
143: * @stable ICU 2.8
144: */
145: public String toString() {
146: StringBuffer buf = new StringBuffer();
147: buf.append(super .getMessage());
148: buf.append(". preContext: ");
149: buf.append(preContext);
150: buf.append(". postContext: ");
151: buf.append(postContext);
152: buf.append("\n");
153: return buf.toString();
154: }
155:
156: private int error;
157:
158: /**
159: * The line on which the error occured. If the parse engine
160: * is not using this field, it should set it to zero. Otherwise
161: * it should be a positive integer. The default value of this field
162: * is -1. It will be set to 0 if the code populating this struct is not
163: * using line numbers.
164: * @stable ICU 2.8
165: */
166: private int line;
167:
168: /**
169: * Textual context before the error. Null-terminated.
170: * May be the empty string if not implemented by parser.
171: * @stable ICU 2.8
172: */
173: private StringBuffer preContext = new StringBuffer();
174:
175: /**
176: * Textual context after the error. Null-terminated.
177: * May be the empty string if not implemented by parser.
178: * @stable ICU 2.8
179: */
180: private StringBuffer postContext = new StringBuffer();
181:
182: private static final int PARSE_CONTEXT_LEN = 16;
183:
184: private void setPreContext(String str, int pos) {
185: setPreContext(str.toCharArray(), pos);
186: }
187:
188: private void setPreContext(char[] str, int pos) {
189: int start = (pos <= PARSE_CONTEXT_LEN) ? 0
190: : (pos - (PARSE_CONTEXT_LEN - 1));
191: int len = (start <= PARSE_CONTEXT_LEN) ? start
192: : PARSE_CONTEXT_LEN;
193: preContext.append(str, start, len);
194:
195: }
196:
197: private void setPostContext(String str, int pos) {
198: setPostContext(str.toCharArray(), pos);
199: }
200:
201: private void setPostContext(char[] str, int pos) {
202: int start = pos;
203: int len = str.length - start;
204: postContext.append(str, start, len);
205:
206: }
207:
208: private void setContext(String str, int pos) {
209: setPreContext(str, pos);
210: setPostContext(str, pos);
211: }
212: }
|