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 /** 041 * Indicates illegal pattern for regular expression. 042 * Includes state to inspect the pattern and what and where the expression 043 * was not valid regular expression. 044 * @since 1.4 045 */ 046 public class PatternSyntaxException extends IllegalArgumentException 047 { 048 private static final long serialVersionUID = -3864639126226059218L; 049 050 /** 051 * Human readable escription of the syntax error. 052 */ 053 private final String desc; 054 055 /** 056 * The original pattern that contained the syntax error. 057 */ 058 private final String pattern; 059 060 /** 061 * Index of the first character in the String that was probably invalid, 062 * or -1 when unknown. 063 */ 064 private final int index; 065 066 /** 067 * Creates a new PatternSyntaxException. 068 * 069 * @param description Human readable escription of the syntax error. 070 * @param pattern The original pattern that contained the syntax error. 071 * @param index Index of the first character in the String that was 072 * probably invalid, or -1 when unknown. 073 */ 074 public PatternSyntaxException(String description, 075 String pattern, 076 int index) 077 { 078 super(description); 079 this.desc = description; 080 this.pattern = pattern; 081 this.index = index; 082 } 083 084 /** 085 * Returns a human readable escription of the syntax error. 086 */ 087 public String getDescription() 088 { 089 return desc; 090 } 091 092 /** 093 * Returns the original pattern that contained the syntax error. 094 */ 095 public String getPattern() 096 { 097 return pattern; 098 } 099 100 /** 101 * Returns the index of the first character in the String that was probably 102 * invalid, or -1 when unknown. 103 */ 104 public int getIndex() 105 { 106 return index; 107 } 108 109 /** 110 * Returns a string containing a line with the description, a line with 111 * the original pattern and a line indicating with a ^ which character is 112 * probably the first invalid character in the pattern if the index is not 113 * negative. 114 */ 115 public String getMessage() 116 { 117 String lineSep = System.getProperty("line.separator"); 118 StringBuffer sb = new StringBuffer(desc); 119 sb.append(lineSep); 120 sb.append('\t'); 121 sb.append(pattern); 122 if (index != -1) 123 { 124 sb.append(lineSep); 125 sb.append('\t'); 126 for (int i=0; i<index; i++) 127 sb.append(' '); 128 sb.append('^'); 129 } 130 return sb.toString(); 131 } 132 133 }