PatternSyntaxException.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package java.util.regex;
18
19import java.util.Arrays;
20
21/**
22 * Encapsulates a syntax error that occurred during the compilation of a
23 * {@link Pattern}. Might include a detailed description, the original regular
24 * expression, and the index at which the error occurred.
25 *
26 * @see Pattern#compile(String)
27 * @see Pattern#compile(java.lang.String,int)
28 *
29 * @since Android 1.0
30 */
31public class PatternSyntaxException extends IllegalArgumentException {
32
33    private static final long serialVersionUID = -3864639126226059218L;
34
35    /**
36     * Holds the syntactically incorrect regular expression, or null if the
37     * regular expression is not known.
38     */
39    private String pattern;
40
41    /**
42     * Holds the description of the syntax error, or null if the description is
43     * not known.
44     */
45    private String description;
46
47    /**
48     * Holds the index around which the error occured, or -1, in case it is
49     * unknown.
50     */
51    private int index = -1;
52
53    /**
54     * Creates a new PatternSyntaxException for a given message, pattern, and
55     * error index.
56     *
57     * @param description
58     *            the description of the syntax error, or {@code null} if the
59     *            description is not known.
60     * @param pattern
61     *            the syntactically incorrect regular expression, or
62     *            {@code null} if the regular expression is not known.
63     * @param index
64     *            the character index around which the error occurred, or -1 if
65     *            the index is not known.
66     * @since Android 1.0
67     */
68    public PatternSyntaxException(String description, String pattern, int index) {
69        this.pattern = pattern;
70        this.description = description;
71        this.index = index;
72    }
73
74    /**
75     * Returns the syntactically incorrect regular expression.
76     *
77     * @return the regular expression.
78     *
79     * @since Android 1.0
80     */
81    public String getPattern() {
82        return pattern;
83    }
84
85    /**
86     * Returns a detailed error message for the exception. The message is
87     * potentially multi-line, and it might include a detailed description, the
88     * original regular expression, and the index at which the error occured.
89     *
90     * @return the error message.
91     *
92     * @since Android 1.0
93     */
94    @Override
95    public String getMessage() {
96        StringBuilder builder = new StringBuilder("Syntax error");
97
98        if (description != null) {
99            builder.append(' ');
100            builder.append(description);
101        }
102
103        if (index >= 0) {
104            builder.append(" near index " + index + ":");
105        }
106
107        if (pattern != null) {
108            builder.append('\n');
109            builder.append(pattern);
110
111            if (index >= 0) {
112                char[] spaces = new char[index];
113                Arrays.fill(spaces, ' ');
114                builder.append('\n');
115                builder.append(spaces);
116                builder.append('^');
117            }
118        }
119
120        return builder.toString();
121    }
122
123    /**
124     * Returns the description of the syntax error, or {@code null} if the
125     * description is not known.
126     *
127     * @return the description.
128     * @since Android 1.0
129     */
130    public String getDescription() {
131        return description;
132    }
133
134    /**
135     * Returns the character index around which the error occurred, or -1 if the
136     * index is not known.
137     *
138     * @return the index.
139     *
140     * @since Android 1.0
141     */
142    public int getIndex() {
143        return index;
144    }
145
146}
147