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