]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/classes/core/src/com/ibm/icu/text/StringPrepParseException.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / classes / core / src / com / ibm / icu / text / StringPrepParseException.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2003-2010, 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      * Returns the position of error in the rules string
141      * 
142      * @return String
143      * @stable ICU 2.8
144      */
145     public String toString(){
146         StringBuilder buf = new StringBuilder();
147         buf.append(super.getMessage());
148         buf.append(". line:  ");
149         buf.append(line);
150         buf.append(". preContext:  ");
151         buf.append(preContext);
152         buf.append(". postContext: ");
153         buf.append(postContext);
154         buf.append("\n");
155         return buf.toString();
156     }
157
158     private int error;
159     
160     /**
161      * The line on which the error occurred.  If the parse engine
162      * is not using this field, it should set it to zero.  Otherwise
163      * it should be a positive integer. The default value of this field
164      * is -1. It will be set to 0 if the code populating this struct is not
165      * using line numbers.
166      */
167     private int line;
168
169
170     /**
171      * Textual context before the error.  Null-terminated.
172      * May be the empty string if not implemented by parser.
173      */
174     private StringBuffer preContext = new StringBuffer();
175
176     /**
177      * Textual context after the error.  Null-terminated.
178      * May be the empty string if not implemented by parser.
179      */
180     private StringBuffer postContext =  new StringBuffer();
181     
182     private static final int PARSE_CONTEXT_LEN = 16;
183     
184     private void setPreContext(String str, int pos){
185         setPreContext(str.toCharArray(),pos);
186     }
187     
188     private void setPreContext(char[] str, int pos){
189         int start = (pos <= PARSE_CONTEXT_LEN)? 0 : (pos - (PARSE_CONTEXT_LEN-1));
190         int len = (start <= PARSE_CONTEXT_LEN)? start : PARSE_CONTEXT_LEN;
191         preContext.append(str,start,len);
192  
193     }
194     
195     private void setPostContext(String str, int pos){
196         setPostContext(str.toCharArray(),pos);
197     }
198     
199     private void setPostContext(char[] str, int pos){
200         int start = pos;
201         int len  = str.length - start; 
202         postContext.append(str,start,len);
203
204     }
205     
206     private void setContext(String str,int pos){
207         setPreContext(str,pos);
208         setPostContext(str,pos);
209     }
210     
211     /**
212      * Returns the error code of this exception. 
213      * This method is only used for testing to verify the error.
214      * @return The error code
215      * @stable ICU 3.8
216      */
217     public int getError(){
218         return error;
219     }
220 }