KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > parser > re > RegExpException


1 /*
2  * RegExpException.java
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2.1
7  * of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307, USA.
18  *
19  * Copyright (c) 2003-2005 Per Cederberg. All rights reserved.
20  */

21
22 package net.percederberg.grammatica.parser.re;
23
24 /**
25  * A regular expression exception. This exception is thrown if a
26  * regular expression couldn't be processed (or "compiled") properly.
27  *
28  * @author Per Cederberg, <per at percederberg dot net>
29  * @version 1.0
30  */

31 public class RegExpException extends Exception JavaDoc {
32
33     /**
34      * The unexpected character error constant. This error is used
35      * when a character was read that didn't match the allowed set of
36      * characters at the given position.
37      */

38     public static final int UNEXPECTED_CHARACTER = 1;
39
40     /**
41      * The unterminated pattern error constant. This error is used
42      * when more characters were expected in the pattern.
43      */

44     public static final int UNTERMINATED_PATTERN = 2;
45
46     /**
47      * The unsupported special character error constant. This error
48      * is used when special regular expression characters are used in
49      * the pattern, but not supported in this implementation.
50      */

51     public static final int UNSUPPORTED_SPECIAL_CHARACTER = 3;
52
53     /**
54      * The unsupported escape character error constant. This error is
55      * used when an escape character construct is used in the pattern,
56      * but not supported in this implementation.
57      */

58     public static final int UNSUPPORTED_ESCAPE_CHARACTER = 4;
59
60     /**
61      * The invalid repeat count error constant. This error is used
62      * when a repetition count of zero is specified, or when the
63      * minimum exceeds the maximum.
64      */

65     public static final int INVALID_REPEAT_COUNT = 5;
66
67     /**
68      * The error type constant.
69      */

70     private int type;
71
72     /**
73      * The error position.
74      */

75     private int position;
76
77     /**
78      * The regular expression pattern.
79      */

80     private String JavaDoc pattern;
81
82     /**
83      * Creates a new regular expression exception.
84      *
85      * @param type the error type constant
86      * @param pos the error position
87      * @param pattern the regular expression pattern
88      */

89     public RegExpException(int type, int pos, String JavaDoc pattern) {
90         this.type = type;
91         this.position = pos;
92         this.pattern = pattern;
93     }
94
95     /**
96      * Returns the exception error message.
97      *
98      * @return the exception error message
99      */

100     public String JavaDoc getMessage() {
101         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
102
103         // Append error type name
104
switch (type) {
105         case UNEXPECTED_CHARACTER:
106             buffer.append("unexpected character");
107             break;
108         case UNTERMINATED_PATTERN:
109             buffer.append("unterminated pattern");
110             break;
111         case UNSUPPORTED_SPECIAL_CHARACTER:
112             buffer.append("unsupported character");
113             break;
114         case UNSUPPORTED_ESCAPE_CHARACTER:
115             buffer.append("unsupported escape character");
116             break;
117         case INVALID_REPEAT_COUNT:
118             buffer.append("invalid repeat count");
119             break;
120         default:
121             buffer.append("internal error");
122             break;
123         }
124
125         // Append erroneous character
126
buffer.append(": ");
127         if (position < pattern.length()) {
128             buffer.append('\'');
129             buffer.append(pattern.substring(position));
130             buffer.append('\'');
131         } else {
132             buffer.append("<end of pattern>");
133         }
134
135         // Append position
136
buffer.append(" at position ");
137         buffer.append(position);
138
139         return buffer.toString();
140     }
141 }
142
Popular Tags