001/*
002 *  gnu/regexp/REException.java
003 *  Copyright (C) 1998-2001 Wes Biggs
004 *
005 *  This library is free software; you can redistribute it and/or modify
006 *  it under the terms of the GNU Lesser General Public License as published
007 *  by the Free Software Foundation; either version 2.1 of the License, or
008 *  (at your option) any later version.
009 *
010 *  This library is distributed in the hope that it will be useful,
011 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
012 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 *  GNU Lesser General Public License for more details.
014 *
015 *  You should have received a copy of the GNU Lesser General Public License
016 *  along with this program; if not, write to the Free Software
017 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018 */
019
020package gnu.regexp;
021
022import java.text.MessageFormat;
023
024/**
025 * This is the regular expression exception class.  An exception of this type
026 * defines the three attributes:
027 * <OL>
028 * <LI> A descriptive message of the error.
029 * <LI> An integral type code equivalent to one of the statically
030 *      defined symbols listed below.
031 * <LI> The approximate position in the input string where the error
032 *      occurred.
033 * </OL>
034 *
035 * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
036 */
037
038public class REException extends Exception {
039  private int type;
040  private int pos;
041
042  // Error conditions from GNU regcomp(3) manual
043
044  /**
045   * Error flag.
046   * Invalid use of repetition operators such  as  using
047   * `*' as the first character.
048   */
049  public static final int REG_BADRPT  =  1;
050
051  /**
052   * Error flag.
053   * Invalid use of back reference operator.
054   */
055  public static final int REG_BADBR   =  2;
056
057  /**
058   * Error flag.
059   * Un-matched brace interval operators.
060   */
061  public static final int REG_EBRACE  =  3;
062
063  /**
064   * Error flag.
065   * Un-matched bracket list operators.
066   */
067  public static final int REG_EBRACK  =  4;
068
069  /**
070   * Error flag.
071   * Invalid  use  of the range operator, eg. the ending
072   * point of the range occurs  prior  to  the  starting
073   * point.
074   */
075  public static final int REG_ERANGE  =  5;
076
077  /**
078   * Error flag.
079   * Unknown character class name. <B>Not implemented</B>.
080   */
081  public static final int REG_ECTYPE  =  6;
082
083  /**
084   * Error flag.
085   * Un-matched parenthesis group operators.
086   */
087  public static final int REG_EPAREN  =  7;
088
089  /**
090   * Error flag.
091   * Invalid back reference to a subexpression.
092   */
093  public static final int REG_ESUBREG =  8;
094
095  /**
096   * Error flag.
097   * Non specific error. <B>Not implemented</B>.
098   */
099  public static final int REG_EEND    =  9;
100
101  /**
102   * Error flag.
103   * Invalid escape sequence. <B>Not implemented</B>.
104   */
105  public static final int REG_ESCAPE  = 10;
106
107  /**
108   * Error flag.
109   * Invalid  use  of pattern operators such as group or list.
110   */
111  public static final int REG_BADPAT  = 11;
112
113  /**
114   * Error flag.
115   * Compiled  regular  expression  requires  a  pattern
116   * buffer larger than 64Kb. <B>Not implemented</B>.
117   */
118  public static final int REG_ESIZE   = 12;
119
120  /**
121   * Error flag.
122   * The regex routines ran out of memory. <B>Not implemented</B>.
123   */
124  public static final int REG_ESPACE  = 13;
125
126  REException(String msg, int type, int position) { 
127    super(msg); 
128    this.type = type;
129    this.pos = position;
130  }
131
132  /**
133   * Returns the type of the exception, one of the constants listed above.
134   */
135
136  public int getType() {
137    return type;
138  }
139
140  /**
141   * Returns the position, relative to the string or character array being
142   * compiled, where the error occurred.  This position is generally the point
143   * where the error was detected, not necessarily the starting index of
144   * a bad subexpression.
145   */
146  public int getPosition() {
147    return pos;
148  }
149
150  /**
151   * Reports the descriptive message associated with this exception
152   * as well as its index position in the string or character array
153   * being compiled.
154   */
155  public String getMessage() {
156    Object[] args = {new Integer(pos)};
157    StringBuffer sb = new StringBuffer();
158    String prefix = RE.getLocalizedMessage("error.prefix");
159    sb.append(MessageFormat.format(prefix, args));
160    sb.append('\n');
161    sb.append(super.getMessage());
162    return sb.toString();
163  }
164}