KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xpath > internal > XPathException


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  * $Id: XPathException.java,v 1.12 2004/02/17 04:30:02 minchau Exp $
18  */

19 package com.sun.org.apache.xpath.internal;
20
21 import javax.xml.transform.TransformerException JavaDoc;
22
23 import org.w3c.dom.Node JavaDoc;
24
25 /**
26  * This class implements an exception object that all
27  * XPath classes will throw in case of an error. This class
28  * extends TransformerException, and may hold other exceptions. In the
29  * case of nested exceptions, printStackTrace will dump
30  * all the traces of the nested exceptions, not just the trace
31  * of this object.
32  * @xsl.usage general
33  */

34 public class XPathException extends TransformerException JavaDoc
35 {
36
37   /** The home of the expression that caused the error.
38    * @serial */

39   Object JavaDoc m_styleNode = null;
40
41   /**
42    * Get the stylesheet node from where this error originated.
43    * @return The stylesheet node from where this error originated, or null.
44    */

45   public Object JavaDoc getStylesheetNode()
46   {
47     return m_styleNode;
48   }
49   
50   /**
51    * Set the stylesheet node from where this error originated.
52    * @param styleNode The stylesheet node from where this error originated, or null.
53    */

54   public void setStylesheetNode(Object JavaDoc styleNode)
55   {
56     m_styleNode = styleNode;
57   }
58
59
60   /** A nested exception.
61    * @serial */

62   protected Exception JavaDoc m_exception;
63
64   /**
65    * Create an XPathException object that holds
66    * an error message.
67    * @param message The error message.
68    */

69   public XPathException(String JavaDoc message, ExpressionNode ex)
70   {
71     super(message);
72     this.setLocator(ex);
73     setStylesheetNode(getStylesheetNode(ex));
74   }
75   
76   /**
77    * Create an XPathException object that holds
78    * an error message.
79    * @param message The error message.
80    */

81   public XPathException(String JavaDoc message)
82   {
83     super(message);
84   }
85
86   
87   /**
88    * Get the XSLT ElemVariable that this sub-expression references. In order for
89    * this to work, the SourceLocator must be the owning ElemTemplateElement.
90    * @return The dereference to the ElemVariable, or null if not found.
91    */

92   public org.w3c.dom.Node JavaDoc getStylesheetNode(ExpressionNode ex)
93   {
94     
95     ExpressionNode owner = getExpressionOwner(ex);
96
97     if (null != owner && owner instanceof org.w3c.dom.Node JavaDoc)
98     {
99         return ((org.w3c.dom.Node JavaDoc)owner);
100     }
101     return null;
102
103   }
104   
105   /**
106    * Get the first non-Expression parent of this node.
107    * @return null or first ancestor that is not an Expression.
108    */

109   protected ExpressionNode getExpressionOwner(ExpressionNode ex)
110   {
111     ExpressionNode parent = ex.exprGetParent();
112     while((null != parent) && (parent instanceof Expression))
113         parent = parent.exprGetParent();
114     return parent;
115   }
116
117
118
119   /**
120    * Create an XPathException object that holds
121    * an error message and the stylesheet node that
122    * the error originated from.
123    * @param message The error message.
124    * @param styleNode The stylesheet node that the error originated from.
125    */

126   public XPathException(String JavaDoc message, Object JavaDoc styleNode)
127   {
128
129     super(message);
130
131     m_styleNode = styleNode;
132   }
133
134   /**
135    * Create an XPathException object that holds
136    * an error message, the stylesheet node that
137    * the error originated from, and another exception
138    * that caused this exception.
139    * @param message The error message.
140    * @param styleNode The stylesheet node that the error originated from.
141    * @param e The exception that caused this exception.
142    */

143   public XPathException(String JavaDoc message, Node JavaDoc styleNode, Exception JavaDoc e)
144   {
145
146     super(message);
147
148     m_styleNode = styleNode;
149     this.m_exception = e;
150   }
151
152   /**
153    * Create an XPathException object that holds
154    * an error message, and another exception
155    * that caused this exception.
156    * @param message The error message.
157    * @param e The exception that caused this exception.
158    */

159   public XPathException(String JavaDoc message, Exception JavaDoc e)
160   {
161
162     super(message);
163
164     this.m_exception = e;
165   }
166
167   /**
168    * Print the the trace of methods from where the error
169    * originated. This will trace all nested exception
170    * objects, as well as this object.
171    * @param s The stream where the dump will be sent to.
172    */

173   public void printStackTrace(java.io.PrintStream JavaDoc s)
174   {
175
176     if (s == null)
177       s = System.err;
178
179     try
180     {
181       super.printStackTrace(s);
182     }
183     catch (Exception JavaDoc e){}
184
185     Throwable JavaDoc exception = m_exception;
186
187     for (int i = 0; (i < 10) && (null != exception); i++)
188     {
189       s.println("---------");
190       exception.printStackTrace(s);
191
192       if (exception instanceof TransformerException JavaDoc)
193       {
194         TransformerException JavaDoc se = (TransformerException JavaDoc) exception;
195         Throwable JavaDoc prev = exception;
196
197         exception = se.getException();
198
199         if (prev == exception)
200           break;
201       }
202       else
203       {
204         exception = null;
205       }
206     }
207   }
208
209   /**
210    * Find the most contained message.
211    *
212    * @return The error message of the originating exception.
213    */

214   public String JavaDoc getMessage()
215   {
216
217     String JavaDoc lastMessage = super.getMessage();
218     Throwable JavaDoc exception = m_exception;
219
220     while (null != exception)
221     {
222       String JavaDoc nextMessage = exception.getMessage();
223
224       if (null != nextMessage)
225         lastMessage = nextMessage;
226
227       if (exception instanceof TransformerException JavaDoc)
228       {
229         TransformerException JavaDoc se = (TransformerException JavaDoc) exception;
230         Throwable JavaDoc prev = exception;
231
232         exception = se.getException();
233
234         if (prev == exception)
235           break;
236       }
237       else
238       {
239         exception = null;
240       }
241     }
242
243     return (null != lastMessage) ? lastMessage : "";
244   }
245
246   /**
247    * Print the the trace of methods from where the error
248    * originated. This will trace all nested exception
249    * objects, as well as this object.
250    * @param s The writer where the dump will be sent to.
251    */

252   public void printStackTrace(java.io.PrintWriter JavaDoc s)
253   {
254
255     if (s == null)
256       s = new java.io.PrintWriter JavaDoc(System.err);
257
258     try
259     {
260       super.printStackTrace(s);
261     }
262     catch (Exception JavaDoc e){}
263
264     Throwable JavaDoc exception = m_exception;
265
266     for (int i = 0; (i < 10) && (null != exception); i++)
267     {
268       s.println("---------");
269
270       try
271       {
272         exception.printStackTrace(s);
273       }
274       catch (Exception JavaDoc e)
275       {
276         s.println("Could not print stack trace...");
277       }
278
279       if (exception instanceof TransformerException JavaDoc)
280       {
281         TransformerException JavaDoc se = (TransformerException JavaDoc) exception;
282         Throwable JavaDoc prev = exception;
283
284         exception = se.getException();
285
286         if (prev == exception)
287         {
288           exception = null;
289
290           break;
291         }
292       }
293       else
294       {
295         exception = null;
296       }
297     }
298   }
299
300   /**
301    * Return the embedded exception, if any.
302    * Overrides javax.xml.transform.TransformerException.getException().
303    *
304    * @return The embedded exception, or null if there is none.
305    */

306   public Throwable JavaDoc getException()
307   {
308     return m_exception;
309   }
310 }
311
Popular Tags