1// © 2016 and later: Unicode, Inc. and others. 2// License & terms of use: http://www.unicode.org/copyright.html#License 3/* 4 ******************************************************************************* 5 * Copyright (C) 2003-2014, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9package com.ibm.icu.text; 10 11import java.text.ParseException; 12 13/** 14 * Exception that signals an error has occurred while parsing the 15 * input to StringPrep or IDNA. 16 * 17 * @author Ram Viswanadha 18 * @stable ICU 2.8 19 */ 20public class StringPrepParseException extends ParseException { 21 // Generated by serialver from JDK 1.4.1_01 22 static final long serialVersionUID = 7160264827701651255L; 23 24 /** 25 * @stable ICU 2.8 26 */ 27 public static final int INVALID_CHAR_FOUND = 0; 28 /** 29 * @stable ICU 2.8 30 */ 31 public static final int ILLEGAL_CHAR_FOUND = 1; 32 /** 33 * @stable ICU 2.8 34 */ 35 public static final int PROHIBITED_ERROR = 2; 36 /** 37 * @stable ICU 2.8 38 */ 39 public static final int UNASSIGNED_ERROR = 3; 40 /** 41 * @stable ICU 2.8 42 */ 43 public static final int CHECK_BIDI_ERROR = 4; 44 /** 45 * @stable ICU 2.8 46 */ 47 public static final int STD3_ASCII_RULES_ERROR = 5; 48 /** 49 * @stable ICU 2.8 50 */ 51 public static final int ACE_PREFIX_ERROR = 6; 52 /** 53 * @stable ICU 2.8 54 */ 55 public static final int VERIFICATION_ERROR = 7; 56 /** 57 * @stable ICU 2.8 58 */ 59 public static final int LABEL_TOO_LONG_ERROR = 8; 60 /** 61 * @stable ICU 2.8 62 */ 63 public static final int BUFFER_OVERFLOW_ERROR = 9; 64 65 /** 66 * @stable ICU 2.8 67 */ 68 public static final int ZERO_LENGTH_LABEL = 10; 69 70 /** 71 * @stable ICU 3.8 72 */ 73 public static final int DOMAIN_NAME_TOO_LONG_ERROR = 11; 74 75 /** 76 * Construct a ParseException object with the given message 77 * and error code 78 * 79 * @param message A string describing the type of error that occurred 80 * @param error The error that has occurred 81 * @stable ICU 2.8 82 */ 83 public StringPrepParseException(String message,int error){ 84 super(message, -1); 85 this.error = error; 86 this.line = 0; 87 } 88 89 /** 90 * Construct a ParseException object with the given message and 91 * error code 92 * 93 * @param message A string describing the type of error that occurred 94 * @param error The error that has occurred 95 * @param rules The input rules string 96 * @param pos The position of error in the rules string 97 * @stable ICU 2.8 98 */ 99 public StringPrepParseException(String message,int error, String rules, int pos){ 100 super(message, -1); 101 this.error = error; 102 setContext(rules,pos); 103 this.line = 0; 104 } 105 /** 106 * Construct a ParseException object with the given message and error code 107 * 108 * @param message A string describing the type of error that occurred 109 * @param error The error that has occurred 110 * @param rules The input rules string 111 * @param pos The position of error in the rules string 112 * @param lineNumber The line number at which the error has occurred. 113 * If the parse engine is not using this field, it should set it to zero. Otherwise 114 * it should be a positive integer. The default value of this field 115 * is -1. It will be set to 0 if the code populating this struct is not 116 * using line numbers. 117 * @stable ICU 2.8 118 */ 119 public StringPrepParseException(String message, int error, String rules, int pos, int lineNumber){ 120 super(message, -1); 121 this.error = error; 122 setContext(rules,pos); 123 this.line = lineNumber; 124 } 125 /** 126 * Compare this ParseException to another and evaluate if they are equal. 127 * The comparison works only on the type of error and does not compare 128 * the rules strings, if any, for equality. 129 * 130 * @param other The exception that this object should be compared to 131 * @return true if the objects are equal, false if unequal 132 * @stable ICU 2.8 133 */ 134 @Override 135 public boolean equals(Object other){ 136 if(!(other instanceof StringPrepParseException)){ 137 return false; 138 } 139 return ((StringPrepParseException)other).error == this.error; 140 141 } 142 143 /** 144 * Mock implementation of hashCode(). This implementation always returns a constant 145 * value. When Java assertion is enabled, this method triggers an assertion failure. 146 * @internal 147 * @deprecated This API is ICU internal only. 148 */ 149 @Override 150 @Deprecated 151 public int hashCode() { 152 assert false : "hashCode not designed"; 153 return 42; 154 } 155 156 /** 157 * Returns the position of error in the rules string 158 * 159 * @return String 160 * @stable ICU 2.8 161 */ 162 @Override 163 public String toString(){ 164 StringBuilder buf = new StringBuilder(); 165 buf.append(super.getMessage()); 166 buf.append(". line: "); 167 buf.append(line); 168 buf.append(". preContext: "); 169 buf.append(preContext); 170 buf.append(". postContext: "); 171 buf.append(postContext); 172 buf.append("\n"); 173 return buf.toString(); 174 } 175 176 private int error; 177 178 /** 179 * The line on which the error occurred. If the parse engine 180 * is not using this field, it should set it to zero. Otherwise 181 * it should be a positive integer. The default value of this field 182 * is -1. It will be set to 0 if the code populating this struct is not 183 * using line numbers. 184 */ 185 private int line; 186 187 188 /** 189 * Textual context before the error. Null-terminated. 190 * May be the empty string if not implemented by parser. 191 */ 192 private StringBuffer preContext = new StringBuffer(); 193 194 /** 195 * Textual context after the error. Null-terminated. 196 * May be the empty string if not implemented by parser. 197 */ 198 private StringBuffer postContext = new StringBuffer(); 199 200 private static final int PARSE_CONTEXT_LEN = 16; 201 202 private void setPreContext(String str, int pos){ 203 setPreContext(str.toCharArray(),pos); 204 } 205 206 private void setPreContext(char[] str, int pos){ 207 int start = (pos <= PARSE_CONTEXT_LEN)? 0 : (pos - (PARSE_CONTEXT_LEN-1)); 208 int len = (start <= PARSE_CONTEXT_LEN)? start : PARSE_CONTEXT_LEN; 209 preContext.append(str,start,len); 210 211 } 212 213 private void setPostContext(String str, int pos){ 214 setPostContext(str.toCharArray(),pos); 215 } 216 217 private void setPostContext(char[] str, int pos){ 218 int start = pos; 219 int len = str.length - start; 220 postContext.append(str,start,len); 221 222 } 223 224 private void setContext(String str,int pos){ 225 setPreContext(str,pos); 226 setPostContext(str,pos); 227 } 228 229 /** 230 * Returns the error code of this exception. 231 * This method is only used for testing to verify the error. 232 * @return The error code 233 * @stable ICU 3.8 234 */ 235 public int getError(){ 236 return error; 237 } 238} 239