View Javadoc

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;
20  import java.io.PrintWriter;
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: 155420 $
27   */
28  
29  public class JellyException extends Exception implements LocationAware {
30      
31      /*** the underlying cause of the exception */
32      private Throwable cause;
33  
34      /*** the Jelly file which caused the problem */
35      private String fileName;
36  
37      /*** the tag name which caused the problem */
38      private String 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 message) {
50          super(message);
51      }
52  
53      public JellyException(String message, Throwable cause) {
54          super(message);
55          this.cause = cause;
56      }
57      
58      public JellyException(Throwable cause) {
59          super(cause.getLocalizedMessage());
60          this.cause = cause;
61      }
62      
63      public JellyException(Throwable cause, String fileName, String elementName, int columnNumber, int lineNumber) {
64          this(cause.getLocalizedMessage(), cause, fileName, elementName, columnNumber, lineNumber);
65      }
66      
67      public JellyException(String reason, Throwable cause, String fileName, String 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 reason, String fileName, String 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 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 getFileName() {
121         return fileName;
122     }
123 
124     /*** 
125      * Sets the Jelly file which caused the problem 
126      */
127     public void setFileName(String fileName) {
128         this.fileName = fileName;
129     }
130     
131 
132     /*** 
133      * @return the element name which caused the problem
134      */
135     public String getElementName() {
136         return elementName;
137     }
138 
139     /*** 
140      * Sets the element name which caused the problem
141      */
142     public void setElementName(String elementName) {
143         this.elementName = elementName;
144     }
145     
146     
147     public String getMessage() {
148         return fileName + ":" + lineNumber + ":" + columnNumber + ": <" + elementName + "> " + getReason();
149     }
150 
151     public String getReason() {
152         return super.getMessage();
153     }
154 
155     // #### overload the printStackTrace methods...
156     public void printStackTrace(PrintWriter s) { 
157         synchronized (s) {
158             super.printStackTrace(s);
159             if  (cause != null) {
160                 s.println("Root cause");
161                 cause.printStackTrace(s);
162             }
163         }
164     }
165         
166     public void printStackTrace(PrintStream s) {
167         synchronized (s) {
168             super.printStackTrace(s);
169             if  (cause != null) {
170                 s.println("Root cause");
171                 cause.printStackTrace(s);
172             }
173         }
174     }
175 
176     public void printStackTrace() {
177         super.printStackTrace();
178         if (cause != null) {
179             System.out.println("Root cause");
180             cause.printStackTrace();
181         }
182     }
183 
184 }