]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/classes/core/src/com/ibm/icu/text/StringPrepParseException.java
Added flags.
[Dictionary.git] / jars / icu4j-52_1 / main / classes / core / src / com / ibm / icu / text / StringPrepParseException.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2003-2011, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.text;
8
9 import java.text.ParseException;
10
11 /**
12  * Exception that signals an error has occurred while parsing the 
13  * input to StringPrep or IDNA. 
14  *
15  * @author Ram Viswanadha
16  * @stable ICU 2.8
17  */
18 public class StringPrepParseException extends ParseException {
19     // Generated by serialver from JDK 1.4.1_01
20     static final long serialVersionUID = 7160264827701651255L;
21     
22     /**
23      * @stable ICU 2.8
24      */
25     public static final int INVALID_CHAR_FOUND      = 0;
26     /**
27      * @stable ICU 2.8
28      */
29     public static final int ILLEGAL_CHAR_FOUND      = 1;
30     /**
31      * @stable ICU 2.8
32      */
33     public static final int PROHIBITED_ERROR        = 2;
34     /**
35      * @stable ICU 2.8
36      */
37     public static final int UNASSIGNED_ERROR        = 3;
38     /**
39      * @stable ICU 2.8
40      */
41     public static final int CHECK_BIDI_ERROR        = 4;
42     /**
43      * @stable ICU 2.8
44      */
45     public static final int STD3_ASCII_RULES_ERROR  = 5;
46     /**
47      * @stable ICU 2.8
48      */
49     public static final int ACE_PREFIX_ERROR        = 6;
50     /**
51      * @stable ICU 2.8
52      */
53     public static final int VERIFICATION_ERROR      = 7;
54     /**
55      * @stable ICU 2.8
56      */
57     public static final int LABEL_TOO_LONG_ERROR    = 8;
58     /**
59      * @stable ICU 2.8
60      */
61     public static final int BUFFER_OVERFLOW_ERROR   = 9;
62     
63     /**
64      * @stable ICU 2.8
65      */
66     public static final int ZERO_LENGTH_LABEL   = 10;
67     
68     /**
69      * @stable ICU 3.8
70      */
71     public static final int DOMAIN_NAME_TOO_LONG_ERROR   = 11;
72     
73     /**
74      * Construct a ParseException object with the given message
75      * and error code
76      * 
77      * @param message A string describing the type of error that occurred
78      * @param error   The error that has occurred
79      * @stable ICU 2.8
80      */
81     public StringPrepParseException(String message,int error){
82         super(message, -1);
83         this.error = error;
84         this.line = 0;
85     }
86     
87     /**
88      * Construct a ParseException object with the given message and
89      * error code
90      * 
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
95      * @stable ICU 2.8
96      */
97     public StringPrepParseException(String message,int error, String rules, int pos){
98         super(message, -1);
99         this.error = error;
100         setContext(rules,pos);  
101         this.line = 0;
102     }
103     /**
104      * Construct  a ParseException object with the given message and error code
105      * 
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.
115      * @stable ICU 2.8
116      */
117     public StringPrepParseException(String message, int error, String rules, int pos, int lineNumber){
118         super(message, -1);
119         this.error = error;
120         setContext(rules,pos);   
121         this.line = lineNumber;
122     }
123     /**
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.
127      * 
128      * @param other The exception that this object should be compared to
129      * @return true if the objects are equal, false if unequal
130      * @stable ICU 2.8
131      */
132     public boolean equals(Object other){
133         if(!(other instanceof StringPrepParseException)){
134             return false;
135         }
136         return ((StringPrepParseException)other).error == this.error;
137         
138     }
139     
140     /**
141      * Mock implementation of hashCode(). This implementation always returns a constant
142      * value. When Java assertion is enabled, this method triggers an assertion failure.
143      * @internal
144      * @deprecated This API is ICU internal only.
145      */
146     public int hashCode() {
147         assert false : "hashCode not designed";
148         return 42;
149     }
150     
151     /**
152      * Returns the position of error in the rules string
153      * 
154      * @return String
155      * @stable ICU 2.8
156      */
157     public String toString(){
158         StringBuilder buf = new StringBuilder();
159         buf.append(super.getMessage());
160         buf.append(". line:  ");
161         buf.append(line);
162         buf.append(". preContext:  ");
163         buf.append(preContext);
164         buf.append(". postContext: ");
165         buf.append(postContext);
166         buf.append("\n");
167         return buf.toString();
168     }
169
170     private int error;
171     
172     /**
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.
178      */
179     private int line;
180
181
182     /**
183      * Textual context before the error.  Null-terminated.
184      * May be the empty string if not implemented by parser.
185      */
186     private StringBuffer preContext = new StringBuffer();
187
188     /**
189      * Textual context after the error.  Null-terminated.
190      * May be the empty string if not implemented by parser.
191      */
192     private StringBuffer postContext =  new StringBuffer();
193     
194     private static final int PARSE_CONTEXT_LEN = 16;
195     
196     private void setPreContext(String str, int pos){
197         setPreContext(str.toCharArray(),pos);
198     }
199     
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);
204  
205     }
206     
207     private void setPostContext(String str, int pos){
208         setPostContext(str.toCharArray(),pos);
209     }
210     
211     private void setPostContext(char[] str, int pos){
212         int start = pos;
213         int len  = str.length - start; 
214         postContext.append(str,start,len);
215
216     }
217     
218     private void setContext(String str,int pos){
219         setPreContext(str,pos);
220         setPostContext(str,pos);
221     }
222     
223     /**
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
227      * @stable ICU 3.8
228      */
229     public int getError(){
230         return error;
231     }
232 }