KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jelly > JellyException


1 /*
2  * Copyright 2002,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.commons.jelly;
18
19 import java.io.PrintStream JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21
22 /**
23  * <p><code>JellyException</code> is the root of all Jelly exceptions.</p>
24  *
25  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
26  * @version $Revision: 429129 $
27  */

28
29 public class JellyException extends Exception JavaDoc implements LocationAware {
30
31     /** the underlying cause of the exception */
32     private Throwable JavaDoc cause;
33
34     /** the Jelly file which caused the problem */
35     private String JavaDoc fileName;
36
37     /** the tag name which caused the problem */
38     private String JavaDoc elementName;
39
40     /** the line number in the script of the error */
41     private int lineNumber = -1;
42
43     /** the column number in the script of the error */
44     private int columnNumber = -1;
45
46     public JellyException() {
47     }
48
49     public JellyException(String JavaDoc message) {
50         super(message);
51     }
52
53     public JellyException(String JavaDoc message, Throwable JavaDoc cause) {
54         super(message);
55         this.cause = cause;
56     }
57
58     public JellyException(Throwable JavaDoc cause) {
59         super(cause.getLocalizedMessage());
60         this.cause = cause;
61     }
62
63     public JellyException(Throwable JavaDoc cause, String JavaDoc fileName, String JavaDoc elementName, int columnNumber, int lineNumber) {
64         this(cause.getLocalizedMessage(), cause, fileName, elementName, columnNumber, lineNumber);
65     }
66
67     public JellyException(String JavaDoc reason, Throwable JavaDoc cause, String JavaDoc fileName, String JavaDoc elementName, int columnNumber, int lineNumber) {
68         super( (reason==null?cause.getClass().getName():reason) );
69         this.cause = cause;
70         this.fileName = fileName;
71         this.elementName = elementName;
72         this.columnNumber = columnNumber;
73         this.lineNumber = lineNumber;
74     }
75
76     public JellyException(String JavaDoc reason, String JavaDoc fileName, String JavaDoc elementName, int columnNumber, int lineNumber) {
77         super(reason);
78         this.fileName = fileName;
79         this.elementName = elementName;
80         this.columnNumber = columnNumber;
81         this.lineNumber = lineNumber;
82     }
83
84     public Throwable JavaDoc getCause() {
85         return cause;
86     }
87
88
89     /**
90      * @return the line number of the tag
91      */

92     public int getLineNumber() {
93         return lineNumber;
94     }
95
96     /**
97      * Sets the line number of the tag
98      */

99     public void setLineNumber(int lineNumber) {
100         this.lineNumber = lineNumber;
101     }
102
103     /**
104      * @return the column number of the tag
105      */

106     public int getColumnNumber() {
107         return columnNumber;
108     }
109
110     /**
111      * Sets the column number of the tag
112      */

113     public void setColumnNumber(int columnNumber) {
114         this.columnNumber = columnNumber;
115     }
116
117     /**
118      * @return the Jelly file which caused the problem
119      */

120     public String JavaDoc getFileName() {
121         return fileName;
122     }
123
124     /**
125      * Sets the Jelly file which caused the problem
126      */

127     public void setFileName(String JavaDoc fileName) {
128         this.fileName = fileName;
129     }
130
131
132     /**
133      * @return the element name which caused the problem
134      */

135     public String JavaDoc getElementName() {
136         return elementName;
137     }
138
139     /**
140      * Sets the element name which caused the problem
141      */

142     public void setElementName(String JavaDoc elementName) {
143         this.elementName = elementName;
144     }
145
146
147     public String JavaDoc getMessage() {
148         if (fileName == null && lineNumber == -1 && columnNumber == -1 && elementName == null) {
149             return getReason();
150         } else {
151             return fileName + ":" + lineNumber + ":" + columnNumber + ": <" + elementName + "> " + getReason();
152         }
153     }
154
155     public String JavaDoc getReason() {
156         return super.getMessage();
157     }
158
159     // #### overload the printStackTrace methods...
160
public void printStackTrace(PrintWriter JavaDoc s) {
161         synchronized (s) {
162             super.printStackTrace(s);
163             if (cause != null && !isChainingSupported()) {
164                 s.println("Root cause");
165                 cause.printStackTrace(s);
166             }
167         }
168     }
169
170     public void printStackTrace(PrintStream JavaDoc s) {
171         synchronized (s) {
172             super.printStackTrace(s);
173             if (cause != null && !isChainingSupported()) {
174                 s.println("Root cause");
175                 cause.printStackTrace(s);
176             }
177         }
178     }
179
180     public void printStackTrace() {
181         super.printStackTrace();
182         if (cause != null && !isChainingSupported()) {
183             System.out.println("Root cause");
184             cause.printStackTrace();
185         }
186     }
187
188     private boolean isChainingSupported() {
189         try {
190             Throwable JavaDoc.class.getMethod("getCause", new Class JavaDoc[0]);
191             return true;
192         } catch (NoSuchMethodException JavaDoc e) {
193             return false;
194         } catch (SecurityException JavaDoc e) {
195             return false;
196         }
197     }
198 }
199
Popular Tags