1/*
2 *******************************************************************************
3 * Copyright (C) 2003-2014, International Business Machines Corporation and    *
4 * others. All Rights Reserved.                                                *
5 *******************************************************************************
6 */
7package com.ibm.icu.text;
8
9import 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 */
18public 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    @Deprecated
147    public int hashCode() {
148        assert false : "hashCode not designed";
149        return 42;
150    }
151
152    /**
153     * Returns the position of error in the rules string
154     *
155     * @return String
156     * @stable ICU 2.8
157     */
158    public String toString(){
159        StringBuilder buf = new StringBuilder();
160        buf.append(super.getMessage());
161        buf.append(". line:  ");
162        buf.append(line);
163        buf.append(". preContext:  ");
164        buf.append(preContext);
165        buf.append(". postContext: ");
166        buf.append(postContext);
167        buf.append("\n");
168        return buf.toString();
169    }
170
171    private int error;
172
173    /**
174     * The line on which the error occurred.  If the parse engine
175     * is not using this field, it should set it to zero.  Otherwise
176     * it should be a positive integer. The default value of this field
177     * is -1. It will be set to 0 if the code populating this struct is not
178     * using line numbers.
179     */
180    private int line;
181
182
183    /**
184     * Textual context before the error.  Null-terminated.
185     * May be the empty string if not implemented by parser.
186     */
187    private StringBuffer preContext = new StringBuffer();
188
189    /**
190     * Textual context after the error.  Null-terminated.
191     * May be the empty string if not implemented by parser.
192     */
193    private StringBuffer postContext =  new StringBuffer();
194
195    private static final int PARSE_CONTEXT_LEN = 16;
196
197    private void setPreContext(String str, int pos){
198        setPreContext(str.toCharArray(),pos);
199    }
200
201    private void setPreContext(char[] str, int pos){
202        int start = (pos <= PARSE_CONTEXT_LEN)? 0 : (pos - (PARSE_CONTEXT_LEN-1));
203        int len = (start <= PARSE_CONTEXT_LEN)? start : PARSE_CONTEXT_LEN;
204        preContext.append(str,start,len);
205
206    }
207
208    private void setPostContext(String str, int pos){
209        setPostContext(str.toCharArray(),pos);
210    }
211
212    private void setPostContext(char[] str, int pos){
213        int start = pos;
214        int len  = str.length - start;
215        postContext.append(str,start,len);
216
217    }
218
219    private void setContext(String str,int pos){
220        setPreContext(str,pos);
221        setPostContext(str,pos);
222    }
223
224    /**
225     * Returns the error code of this exception.
226     * This method is only used for testing to verify the error.
227     * @return The error code
228     * @stable ICU 3.8
229     */
230    public int getError(){
231        return error;
232    }
233}
234