KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jpath > expression > ValidationException


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.lang.jpath.expression;
18
19
20 /**
21  * The ValidationException class
22  *
23  *
24  * @author <a HREF='mailto:scott.hasse@isthmusgroup.com'>Scott Hasse</a>
25  * @version
26  */

27 public class ValidationException extends Exception JavaDoc {
28
29     protected boolean specialConstructor;
30     public String JavaDoc image;
31     protected SimpleNode node;
32
33     /**
34      * The end of line string for this machine.
35      */

36     protected String JavaDoc eol = System.getProperty("line.separator", "\n");
37
38     /**
39      * The following constructors are for use by you for whatever
40      * purpose you can think of. Constructing the exception in this
41      * manner makes the exception behave in the normal way - i.e., as
42      * documented in the class "Throwable".
43      */

44     public ValidationException() {
45
46         super();
47
48         specialConstructor = false;
49     }
50
51     /**
52      * Used to create an instance of the ValidationException class
53      *
54      *
55      * @param message
56      *
57      */

58     public ValidationException(String JavaDoc message) {
59
60         super(message);
61
62         specialConstructor = false;
63     }
64
65     /**
66      * Used to create an instance of the ValidationException class
67      *
68      *
69      * @param currentNode
70      * @param message
71      *
72      */

73     public ValidationException(SimpleNode currentNode, String JavaDoc message) {
74
75         super(message);
76
77         specialConstructor = true;
78         node = currentNode;
79     }
80
81     /**
82      * This method has the standard behavior when this object has been
83      * created using the standard constructors. Otherwise, it uses
84      * "currentToken" to generate an evaluation
85      * error message and returns it. If this object has been created
86      * due to an evaluation error, and you do not catch it (it gets thrown
87      * during the evaluation), then this method is called during the printing
88      * of the final stack trace, and hence the correct error message
89      * gets displayed.
90      *
91      * @return
92      */

93     public String JavaDoc getMessage() {
94
95         if (!specialConstructor) {
96             return super.getMessage();
97         }
98
99         String JavaDoc expected = "";
100         int maxSize = 1;
101         String JavaDoc retval = eol + "Encountered \"";
102
103         retval += super.getMessage();
104         retval += "\"" + eol;
105         retval += "at : " + eol;
106         retval += node.rootOriginalString() + eol;
107
108         for (int i = 0; i < node.firstToken.beginColumn - 1; i++) {
109             retval += " ";
110         }
111
112         for (int i = 0; i
113                 < node.lastToken.endColumn
114                     - (node.firstToken.beginColumn - 1); i++) {
115             retval += "^";
116         }
117
118         retval += eol;
119         retval += "beginning at column " + node.firstToken.beginColumn + "."
120                 + eol;
121         retval += "ending at column " + node.lastToken.endColumn + "." + eol;
122
123         return retval;
124     }
125
126     /**
127      * Used to convert raw characters to their escaped version
128      * when these raw version cannot be used as part of an ASCII
129      * string literal.
130      *
131      * @param str
132      *
133      * @return
134      */

135     protected String JavaDoc add_escapes(String JavaDoc str) {
136
137         StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
138         char ch;
139
140         for (int i = 0; i < str.length(); i++) {
141             switch (str.charAt(i)) {
142
143             case 0 :
144                 continue;
145             case '\b' :
146                 retval.append("\\b");
147
148                 continue;
149             case '\t' :
150                 retval.append("\\t");
151
152                 continue;
153             case '\n' :
154                 retval.append("\\n");
155
156                 continue;
157             case '\f' :
158                 retval.append("\\f");
159
160                 continue;
161             case '\r' :
162                 retval.append("\\r");
163
164                 continue;
165             case '\"' :
166                 retval.append("\\\"");
167
168                 continue;
169             case '\'' :
170                 retval.append("\\\'");
171
172                 continue;
173             case '\\' :
174                 retval.append("\\\\");
175
176                 continue;
177             default :
178                 if ((ch = str.charAt(i)) < 0x20 || (ch > 0x7e)) {
179                     String JavaDoc s = "0000" + Integer.toString(ch, 16);
180
181                     retval.append("\\u"
182                             + s.substring(s.length() - 4, s.length()));
183                 } else {
184                     retval.append(ch);
185                 }
186
187                 continue;
188             }
189         }
190
191         return retval.toString();
192     }
193 }
194
Popular Tags