2 *******************************************************************************
\r
3 * Copyright (C) 2003-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.text;
\r
9 import java.text.ParseException;
\r
12 * Exception that signals an error has occurred while parsing the
\r
13 * input to StringPrep or IDNA.
\r
15 * @author Ram Viswanadha
\r
18 public class StringPrepParseException extends ParseException {
\r
19 // Generated by serialver from JDK 1.4.1_01
\r
20 static final long serialVersionUID = 7160264827701651255L;
\r
25 public static final int INVALID_CHAR_FOUND = 0;
\r
29 public static final int ILLEGAL_CHAR_FOUND = 1;
\r
33 public static final int PROHIBITED_ERROR = 2;
\r
37 public static final int UNASSIGNED_ERROR = 3;
\r
41 public static final int CHECK_BIDI_ERROR = 4;
\r
45 public static final int STD3_ASCII_RULES_ERROR = 5;
\r
49 public static final int ACE_PREFIX_ERROR = 6;
\r
53 public static final int VERIFICATION_ERROR = 7;
\r
57 public static final int LABEL_TOO_LONG_ERROR = 8;
\r
61 public static final int BUFFER_OVERFLOW_ERROR = 9;
\r
66 public static final int ZERO_LENGTH_LABEL = 10;
\r
71 public static final int DOMAIN_NAME_TOO_LONG_ERROR = 11;
\r
74 * Construct a ParseException object with the given message
\r
77 * @param message A string describing the type of error that occurred
\r
78 * @param error The error that has occurred
\r
81 public StringPrepParseException(String message,int error){
\r
88 * Construct a ParseException object with the given message and
\r
91 * @param message A string describing the type of error that occurred
\r
92 * @param error The error that has occurred
\r
93 * @param rules The input rules string
\r
94 * @param pos The position of error in the rules string
\r
97 public StringPrepParseException(String message,int error, String rules, int pos){
\r
100 setContext(rules,pos);
\r
104 * Construct a ParseException object with the given message and error code
\r
106 * @param message A string describing the type of error that occurred
\r
107 * @param error The error that has occurred
\r
108 * @param rules The input rules string
\r
109 * @param pos The position of error in the rules string
\r
110 * @param lineNumber The line number at which the error has occurred.
\r
111 * If the parse engine is not using this field, it should set it to zero. Otherwise
\r
112 * it should be a positive integer. The default value of this field
\r
113 * is -1. It will be set to 0 if the code populating this struct is not
\r
114 * using line numbers.
\r
117 public StringPrepParseException(String message, int error, String rules, int pos, int lineNumber){
\r
118 super(message, -1);
\r
119 this.error = error;
\r
120 setContext(rules,pos);
\r
121 this.line = lineNumber;
\r
124 * Compare this ParseException to another and evaluate if they are equal.
\r
125 * The comparison works only on the type of error and does not compare
\r
126 * the rules strings, if any, for equality.
\r
128 * @param other The exception that this object should be compared to
\r
129 * @return true if the objects are equal, false if unequal
\r
132 public boolean equals(Object other){
\r
133 if(!(other instanceof StringPrepParseException)){
\r
136 return ((StringPrepParseException)other).error == this.error;
\r
140 * Returns the position of error in the rules string
\r
145 public String toString(){
\r
146 StringBuilder buf = new StringBuilder();
\r
147 buf.append(super.getMessage());
\r
148 buf.append(". line: ");
\r
150 buf.append(". preContext: ");
\r
151 buf.append(preContext);
\r
152 buf.append(". postContext: ");
\r
153 buf.append(postContext);
\r
155 return buf.toString();
\r
161 * The line on which the error occurred. If the parse engine
\r
162 * is not using this field, it should set it to zero. Otherwise
\r
163 * it should be a positive integer. The default value of this field
\r
164 * is -1. It will be set to 0 if the code populating this struct is not
\r
165 * using line numbers.
\r
171 * Textual context before the error. Null-terminated.
\r
172 * May be the empty string if not implemented by parser.
\r
174 private StringBuffer preContext = new StringBuffer();
\r
177 * Textual context after the error. Null-terminated.
\r
178 * May be the empty string if not implemented by parser.
\r
180 private StringBuffer postContext = new StringBuffer();
\r
182 private static final int PARSE_CONTEXT_LEN = 16;
\r
184 private void setPreContext(String str, int pos){
\r
185 setPreContext(str.toCharArray(),pos);
\r
188 private void setPreContext(char[] str, int pos){
\r
189 int start = (pos <= PARSE_CONTEXT_LEN)? 0 : (pos - (PARSE_CONTEXT_LEN-1));
\r
190 int len = (start <= PARSE_CONTEXT_LEN)? start : PARSE_CONTEXT_LEN;
\r
191 preContext.append(str,start,len);
\r
195 private void setPostContext(String str, int pos){
\r
196 setPostContext(str.toCharArray(),pos);
\r
199 private void setPostContext(char[] str, int pos){
\r
201 int len = str.length - start;
\r
202 postContext.append(str,start,len);
\r
206 private void setContext(String str,int pos){
\r
207 setPreContext(str,pos);
\r
208 setPostContext(str,pos);
\r
212 * Returns the error code of this exception.
\r
213 * This method is only used for testing to verify the error.
\r
214 * @return The error code
\r
217 public int getError(){
\r