1 /* 2 * Copyright (c) 1999, 2018, 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 * @author unascribed 33 * @since 1.4 34 * @spec JSR-51 35 */ 36 37 public class PatternSyntaxException 38 extends IllegalArgumentException 39 { 40 private static final long serialVersionUID = -3864639126226059218L; 41 42 private final String desc; 43 private final String pattern; 44 private final int index; 45 46 /** 47 * Constructs a new instance of this class. 48 * 49 * @param desc 50 * A description of the error 51 * 52 * @param regex 53 * The erroneous pattern 54 * 55 * @param index 56 * The approximate index in the pattern of the error, 57 * or {@code -1} if the index is not known 58 */ PatternSyntaxException(String desc, String regex, int index)59 public PatternSyntaxException(String desc, String regex, int index) { 60 this.desc = desc; 61 this.pattern = regex; 62 this.index = index; 63 } 64 65 /** 66 * Retrieves the error index. 67 * 68 * @return The approximate index in the pattern of the error, 69 * or {@code -1} if the index is not known 70 */ getIndex()71 public int getIndex() { 72 return index; 73 } 74 75 /** 76 * Retrieves the description of the error. 77 * 78 * @return The description of the error 79 */ getDescription()80 public String getDescription() { 81 return desc; 82 } 83 84 /** 85 * Retrieves the erroneous regular-expression pattern. 86 * 87 * @return The erroneous pattern 88 */ getPattern()89 public String getPattern() { 90 return pattern; 91 } 92 93 /** 94 * Returns a multi-line string containing the description of the syntax 95 * error and its index, the erroneous regular-expression pattern, and a 96 * visual indication of the error index within the pattern. 97 * 98 * @return The full detail message 99 */ getMessage()100 public String getMessage() { 101 StringBuilder sb = new StringBuilder(); 102 sb.append(desc); 103 if (index >= 0) { 104 sb.append(" near index "); 105 sb.append(index); 106 } 107 sb.append(System.lineSeparator()); 108 sb.append(pattern); 109 if (index >= 0 && pattern != null && index < pattern.length()) { 110 sb.append(System.lineSeparator()); 111 for (int i = 0; i < index; i++) sb.append(' '); 112 sb.append('^'); 113 } 114 return sb.toString(); 115 } 116 117 } 118