KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > regex > PatternSyntaxException


1 /*
2  * @(#)PatternSyntaxException.java 1.13 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.util.regex;
9
10 import sun.security.action.GetPropertyAction;
11
12
13 /**
14  * Unchecked exception thrown to indicate a syntax error in a
15  * regular-expression pattern.
16  *
17  * @author unascribed
18  * @version 1.13, 03/12/19
19  * @since 1.4
20  * @spec JSR-51
21  */

22
23 public class PatternSyntaxException
24     extends IllegalArgumentException JavaDoc
25 {
26
27     private final String JavaDoc desc;
28     private final String JavaDoc pattern;
29     private final int index;
30
31     /**
32      * Constructs a new instance of this class.
33      *
34      * @param desc
35      * A description of the error
36      *
37      * @param regex
38      * The erroneous pattern
39      *
40      * @param index
41      * The approximate index in the pattern of the error,
42      * or <tt>-1</tt> if the index is not known
43      */

44     public PatternSyntaxException(String JavaDoc desc, String JavaDoc regex, int index) {
45     this.desc = desc;
46     this.pattern = regex;
47     this.index = index;
48     }
49
50     /**
51      * Retrieves the error index.
52      *
53      * @return The approximate index in the pattern of the error,
54      * or <tt>-1</tt> if the index is not known
55      */

56     public int getIndex() {
57     return index;
58     }
59
60     /**
61      * Retrieves the description of the error.
62      *
63      * @return The description of the error
64      */

65     public String JavaDoc getDescription() {
66     return desc;
67     }
68
69     /**
70      * Retrieves the erroneous regular-expression pattern.
71      *
72      * @return The erroneous pattern
73      */

74     public String JavaDoc getPattern() {
75     return pattern;
76     }
77
78     private static String JavaDoc nl;
79
80     static {
81     nl = (String JavaDoc)java.security.AccessController
82         .doPrivileged(new GetPropertyAction("line.separator"));
83     }
84
85     /**
86      * Returns a multi-line string containing the description of the syntax
87      * error and its index, the erroneous regular-expression pattern, and a
88      * visual indication of the error index within the pattern.
89      *
90      * @return The full detail message
91      */

92     public String JavaDoc getMessage() {
93     String JavaDoc nl = System.getProperty("line.separator");
94         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
95         sb.append(desc);
96     if (index >= 0) {
97         sb.append(" near index ");
98         sb.append(index);
99     }
100         sb.append(nl);
101         sb.append(pattern);
102     if (index >= 0) {
103         sb.append(nl);
104         for (int i = 0; i < index; i++) sb.append(' ');
105         sb.append('^');
106     }
107         return sb.toString();
108     }
109
110 }
111
Popular Tags