1 /*
2  * Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package java.util.regex;
27 
28 /**
29  * Unchecked exception thrown to indicate a syntax error in a
30  * regular-expression pattern.
31  *
32  * @since 1.4
33  */
34 
35 public class PatternSyntaxException
36     extends IllegalArgumentException
37 {
38     @java.io.Serial
39     private static final long serialVersionUID = -3864639126226059218L;
40 
41     private final String desc;
42     private final String pattern;
43     private final int index;
44 
45     /**
46      * Constructs a new instance of this class.
47      *
48      * @param  desc
49      *         A description of the error
50      *
51      * @param  regex
52      *         The erroneous pattern
53      *
54      * @param  index
55      *         The approximate index in the pattern of the error,
56      *         or {@code -1} if the index is not known
57      */
PatternSyntaxException(String desc, String regex, int index)58     public PatternSyntaxException(String desc, String regex, int index) {
59         this.desc = desc;
60         this.pattern = regex;
61         this.index = index;
62     }
63 
64     /**
65      * Retrieves the error index.
66      *
67      * @return  The approximate index in the pattern of the error,
68      *         or {@code -1} if the index is not known
69      */
getIndex()70     public int getIndex() {
71         return index;
72     }
73 
74     /**
75      * Retrieves the description of the error.
76      *
77      * @return  The description of the error
78      */
getDescription()79     public String getDescription() {
80         return desc;
81     }
82 
83     /**
84      * Retrieves the erroneous regular-expression pattern.
85      *
86      * @return  The erroneous pattern
87      */
getPattern()88     public String getPattern() {
89         return pattern;
90     }
91 
92     /**
93      * Returns a multi-line string containing the description of the syntax
94      * error and its index, the erroneous regular-expression pattern, and a
95      * visual indication of the error index within the pattern.
96      *
97      * @return  The full detail message
98      */
getMessage()99     public String getMessage() {
100         StringBuilder sb = new StringBuilder();
101         sb.append(desc);
102         if (index >= 0) {
103             sb.append(" near index ");
104             sb.append(index);
105         }
106         sb.append(System.lineSeparator());
107         sb.append(pattern);
108         if (index >= 0 && pattern != null && index < pattern.length()) {
109             sb.append(System.lineSeparator());
110             for (int i = 0; i < index; i++) {
111                 sb.append((pattern.charAt(i) == '\t') ? '\t' : ' ');
112             }
113             sb.append('^');
114         }
115         return sb.toString();
116     }
117 
118 }
119