001 /* PatternSyntaxException - Indicates illegal pattern for regular expression.
002 Copyright (C) 2002 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038 package java.util.regex;
039
040 import gnu.java.lang.CPStringBuilder;
041
042 /**
043 * Indicates illegal pattern for regular expression.
044 * Includes state to inspect the pattern and what and where the expression
045 * was not valid regular expression.
046 * @since 1.4
047 */
048 public class PatternSyntaxException extends IllegalArgumentException
049 {
050 private static final long serialVersionUID = -3864639126226059218L;
051
052 /**
053 * Human readable escription of the syntax error.
054 */
055 private final String desc;
056
057 /**
058 * The original pattern that contained the syntax error.
059 */
060 private final String pattern;
061
062 /**
063 * Index of the first character in the String that was probably invalid,
064 * or -1 when unknown.
065 */
066 private final int index;
067
068 /**
069 * Creates a new PatternSyntaxException.
070 *
071 * @param description Human readable escription of the syntax error.
072 * @param pattern The original pattern that contained the syntax error.
073 * @param index Index of the first character in the String that was
074 * probably invalid, or -1 when unknown.
075 */
076 public PatternSyntaxException(String description,
077 String pattern,
078 int index)
079 {
080 super(description);
081 this.desc = description;
082 this.pattern = pattern;
083 this.index = index;
084 }
085
086 /**
087 * Returns a human readable escription of the syntax error.
088 */
089 public String getDescription()
090 {
091 return desc;
092 }
093
094 /**
095 * Returns the original pattern that contained the syntax error.
096 */
097 public String getPattern()
098 {
099 return pattern;
100 }
101
102 /**
103 * Returns the index of the first character in the String that was probably
104 * invalid, or -1 when unknown.
105 */
106 public int getIndex()
107 {
108 return index;
109 }
110
111 /**
112 * Returns a string containing a line with the description, a line with
113 * the original pattern and a line indicating with a ^ which character is
114 * probably the first invalid character in the pattern if the index is not
115 * negative.
116 */
117 public String getMessage()
118 {
119 String lineSep = System.getProperty("line.separator");
120 CPStringBuilder sb = new CPStringBuilder(desc);
121 sb.append(lineSep);
122 sb.append('\t');
123 sb.append(pattern);
124 if (index != -1)
125 {
126 sb.append(lineSep);
127 sb.append('\t');
128 for (int i=0; i<index; i++)
129 sb.append(' ');
130 sb.append('^');
131 }
132 return sb.toString();
133 }
134
135 }