]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/text/StringPrepParseException.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / text / StringPrepParseException.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2003-2010, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.text;\r
8 \r
9 import java.text.ParseException;\r
10 \r
11 /**\r
12  * Exception that signals an error has occurred while parsing the \r
13  * input to StringPrep or IDNA. \r
14  *\r
15  * @author Ram Viswanadha\r
16  * @stable ICU 2.8\r
17  */\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
21     \r
22     /**\r
23      * @stable ICU 2.8\r
24      */\r
25     public static final int INVALID_CHAR_FOUND      = 0;\r
26     /**\r
27      * @stable ICU 2.8\r
28      */\r
29     public static final int ILLEGAL_CHAR_FOUND      = 1;\r
30     /**\r
31      * @stable ICU 2.8\r
32      */\r
33     public static final int PROHIBITED_ERROR        = 2;\r
34     /**\r
35      * @stable ICU 2.8\r
36      */\r
37     public static final int UNASSIGNED_ERROR        = 3;\r
38     /**\r
39      * @stable ICU 2.8\r
40      */\r
41     public static final int CHECK_BIDI_ERROR        = 4;\r
42     /**\r
43      * @stable ICU 2.8\r
44      */\r
45     public static final int STD3_ASCII_RULES_ERROR  = 5;\r
46     /**\r
47      * @stable ICU 2.8\r
48      */\r
49     public static final int ACE_PREFIX_ERROR        = 6;\r
50     /**\r
51      * @stable ICU 2.8\r
52      */\r
53     public static final int VERIFICATION_ERROR      = 7;\r
54     /**\r
55      * @stable ICU 2.8\r
56      */\r
57     public static final int LABEL_TOO_LONG_ERROR    = 8;\r
58     /**\r
59      * @stable ICU 2.8\r
60      */\r
61     public static final int BUFFER_OVERFLOW_ERROR   = 9;\r
62     \r
63     /**\r
64      * @stable ICU 2.8\r
65      */\r
66     public static final int ZERO_LENGTH_LABEL   = 10;\r
67     \r
68     /**\r
69      * @stable ICU 3.8\r
70      */\r
71     public static final int DOMAIN_NAME_TOO_LONG_ERROR   = 11;\r
72     \r
73     /**\r
74      * Construct a ParseException object with the given message\r
75      * and error code\r
76      * \r
77      * @param message A string describing the type of error that occurred\r
78      * @param error   The error that has occurred\r
79      * @stable ICU 2.8\r
80      */\r
81     public StringPrepParseException(String message,int error){\r
82         super(message, -1);\r
83         this.error = error;\r
84         this.line = 0;\r
85     }\r
86     \r
87     /**\r
88      * Construct a ParseException object with the given message and\r
89      * error code\r
90      * \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
95      * @stable ICU 2.8\r
96      */\r
97     public StringPrepParseException(String message,int error, String rules, int pos){\r
98         super(message, -1);\r
99         this.error = error;\r
100         setContext(rules,pos);  \r
101         this.line = 0;\r
102     }\r
103     /**\r
104      * Construct  a ParseException object with the given message and error code\r
105      * \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
115      * @stable ICU 2.8\r
116      */\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
122     }\r
123     /**\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
127      * \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
130      * @stable ICU 2.8\r
131      */\r
132     public boolean equals(Object other){\r
133         if(!(other instanceof StringPrepParseException)){\r
134             return false;\r
135         }\r
136         return ((StringPrepParseException)other).error == this.error;\r
137         \r
138     }\r
139     /**\r
140      * Returns the position of error in the rules string\r
141      * \r
142      * @return String\r
143      * @stable ICU 2.8\r
144      */\r
145     public String toString(){\r
146         StringBuilder buf = new StringBuilder();\r
147         buf.append(super.getMessage());\r
148         buf.append(". line:  ");\r
149         buf.append(line);\r
150         buf.append(". preContext:  ");\r
151         buf.append(preContext);\r
152         buf.append(". postContext: ");\r
153         buf.append(postContext);\r
154         buf.append("\n");\r
155         return buf.toString();\r
156     }\r
157 \r
158     private int error;\r
159     \r
160     /**\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
166      */\r
167     private int line;\r
168 \r
169 \r
170     /**\r
171      * Textual context before the error.  Null-terminated.\r
172      * May be the empty string if not implemented by parser.\r
173      */\r
174     private StringBuffer preContext = new StringBuffer();\r
175 \r
176     /**\r
177      * Textual context after the error.  Null-terminated.\r
178      * May be the empty string if not implemented by parser.\r
179      */\r
180     private StringBuffer postContext =  new StringBuffer();\r
181     \r
182     private static final int PARSE_CONTEXT_LEN = 16;\r
183     \r
184     private void setPreContext(String str, int pos){\r
185         setPreContext(str.toCharArray(),pos);\r
186     }\r
187     \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
192  \r
193     }\r
194     \r
195     private void setPostContext(String str, int pos){\r
196         setPostContext(str.toCharArray(),pos);\r
197     }\r
198     \r
199     private void setPostContext(char[] str, int pos){\r
200         int start = pos;\r
201         int len  = str.length - start; \r
202         postContext.append(str,start,len);\r
203 \r
204     }\r
205     \r
206     private void setContext(String str,int pos){\r
207         setPreContext(str,pos);\r
208         setPostContext(str,pos);\r
209     }\r
210     \r
211     /**\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
215      * @stable ICU 3.8\r
216      */\r
217     public int getError(){\r
218         return error;\r
219     }\r
220 }\r