2 *******************************************************************************
3 * Copyright (C) 2003-2011, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.text;
9 import java.text.ParseException;
12 * Exception that signals an error has occurred while parsing the
13 * input to StringPrep or IDNA.
15 * @author Ram Viswanadha
18 public class StringPrepParseException extends ParseException {
19 // Generated by serialver from JDK 1.4.1_01
20 static final long serialVersionUID = 7160264827701651255L;
25 public static final int INVALID_CHAR_FOUND = 0;
29 public static final int ILLEGAL_CHAR_FOUND = 1;
33 public static final int PROHIBITED_ERROR = 2;
37 public static final int UNASSIGNED_ERROR = 3;
41 public static final int CHECK_BIDI_ERROR = 4;
45 public static final int STD3_ASCII_RULES_ERROR = 5;
49 public static final int ACE_PREFIX_ERROR = 6;
53 public static final int VERIFICATION_ERROR = 7;
57 public static final int LABEL_TOO_LONG_ERROR = 8;
61 public static final int BUFFER_OVERFLOW_ERROR = 9;
66 public static final int ZERO_LENGTH_LABEL = 10;
71 public static final int DOMAIN_NAME_TOO_LONG_ERROR = 11;
74 * Construct a ParseException object with the given message
77 * @param message A string describing the type of error that occurred
78 * @param error The error that has occurred
81 public StringPrepParseException(String message,int error){
88 * Construct a ParseException object with the given message and
91 * @param message A string describing the type of error that occurred
92 * @param error The error that has occurred
93 * @param rules The input rules string
94 * @param pos The position of error in the rules string
97 public StringPrepParseException(String message,int error, String rules, int pos){
100 setContext(rules,pos);
104 * Construct a ParseException object with the given message and error code
106 * @param message A string describing the type of error that occurred
107 * @param error The error that has occurred
108 * @param rules The input rules string
109 * @param pos The position of error in the rules string
110 * @param lineNumber The line number at which the error has occurred.
111 * If the parse engine is not using this field, it should set it to zero. Otherwise
112 * it should be a positive integer. The default value of this field
113 * is -1. It will be set to 0 if the code populating this struct is not
114 * using line numbers.
117 public StringPrepParseException(String message, int error, String rules, int pos, int lineNumber){
120 setContext(rules,pos);
121 this.line = lineNumber;
124 * Compare this ParseException to another and evaluate if they are equal.
125 * The comparison works only on the type of error and does not compare
126 * the rules strings, if any, for equality.
128 * @param other The exception that this object should be compared to
129 * @return true if the objects are equal, false if unequal
132 public boolean equals(Object other){
133 if(!(other instanceof StringPrepParseException)){
136 return ((StringPrepParseException)other).error == this.error;
141 * Mock implementation of hashCode(). This implementation always returns a constant
142 * value. When Java assertion is enabled, this method triggers an assertion failure.
144 * @deprecated This API is ICU internal only.
146 public int hashCode() {
147 assert false : "hashCode not designed";
152 * Returns the position of error in the rules string
157 public String toString(){
158 StringBuilder buf = new StringBuilder();
159 buf.append(super.getMessage());
160 buf.append(". line: ");
162 buf.append(". preContext: ");
163 buf.append(preContext);
164 buf.append(". postContext: ");
165 buf.append(postContext);
167 return buf.toString();
173 * The line on which the error occurred. If the parse engine
174 * is not using this field, it should set it to zero. Otherwise
175 * it should be a positive integer. The default value of this field
176 * is -1. It will be set to 0 if the code populating this struct is not
177 * using line numbers.
183 * Textual context before the error. Null-terminated.
184 * May be the empty string if not implemented by parser.
186 private StringBuffer preContext = new StringBuffer();
189 * Textual context after the error. Null-terminated.
190 * May be the empty string if not implemented by parser.
192 private StringBuffer postContext = new StringBuffer();
194 private static final int PARSE_CONTEXT_LEN = 16;
196 private void setPreContext(String str, int pos){
197 setPreContext(str.toCharArray(),pos);
200 private void setPreContext(char[] str, int pos){
201 int start = (pos <= PARSE_CONTEXT_LEN)? 0 : (pos - (PARSE_CONTEXT_LEN-1));
202 int len = (start <= PARSE_CONTEXT_LEN)? start : PARSE_CONTEXT_LEN;
203 preContext.append(str,start,len);
207 private void setPostContext(String str, int pos){
208 setPostContext(str.toCharArray(),pos);
211 private void setPostContext(char[] str, int pos){
213 int len = str.length - start;
214 postContext.append(str,start,len);
218 private void setContext(String str,int pos){
219 setPreContext(str,pos);
220 setPostContext(str,pos);
224 * Returns the error code of this exception.
225 * This method is only used for testing to verify the error.
226 * @return The error code
229 public int getError(){