6
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 1999-2004 The Apache Software Foundation.
|
|
7 |
*
|
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
* you may not use this file except in compliance with the License.
|
|
10 |
* You may obtain a copy of the License at
|
|
11 |
*
|
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13 |
*
|
|
14 |
* Unless required by applicable law or agreed to in writing, software
|
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17 |
* See the License for the specific language governing permissions and
|
|
18 |
* limitations under the License.
|
|
19 |
*/
|
|
20 |
/*
|
|
21 |
* $Id: XPathException.java,v 1.3 2005/09/28 13:49:30 pvedula Exp $
|
|
22 |
*/
|
|
23 |
package com.sun.org.apache.xpath.internal;
|
|
24 |
|
|
25 |
import javax.xml.transform.TransformerException;
|
|
26 |
|
|
27 |
import org.w3c.dom.Node;
|
|
28 |
|
|
29 |
/**
|
|
30 |
* This class implements an exception object that all
|
|
31 |
* XPath classes will throw in case of an error. This class
|
|
32 |
* extends TransformerException, and may hold other exceptions. In the
|
|
33 |
* case of nested exceptions, printStackTrace will dump
|
|
34 |
* all the traces of the nested exceptions, not just the trace
|
|
35 |
* of this object.
|
|
36 |
* @xsl.usage general
|
|
37 |
*/
|
|
38 |
public class XPathException extends TransformerException
|
|
39 |
{
|
|
40 |
static final long serialVersionUID = 4263549717619045963L;
|
|
41 |
|
|
42 |
/** The home of the expression that caused the error.
|
|
43 |
* @serial */
|
|
44 |
Object m_styleNode = null;
|
|
45 |
|
|
46 |
/**
|
|
47 |
* Get the stylesheet node from where this error originated.
|
|
48 |
* @return The stylesheet node from where this error originated, or null.
|
|
49 |
*/
|
|
50 |
public Object getStylesheetNode()
|
|
51 |
{
|
|
52 |
return m_styleNode;
|
|
53 |
}
|
|
54 |
|
|
55 |
/**
|
|
56 |
* Set the stylesheet node from where this error originated.
|
|
57 |
* @param styleNode The stylesheet node from where this error originated, or null.
|
|
58 |
*/
|
|
59 |
public void setStylesheetNode(Object styleNode)
|
|
60 |
{
|
|
61 |
m_styleNode = styleNode;
|
|
62 |
}
|
|
63 |
|
|
64 |
|
|
65 |
/** A nested exception.
|
|
66 |
* @serial */
|
|
67 |
protected Exception m_exception;
|
|
68 |
|
|
69 |
/**
|
|
70 |
* Create an XPathException object that holds
|
|
71 |
* an error message.
|
|
72 |
* @param message The error message.
|
|
73 |
*/
|
|
74 |
public XPathException(String message, ExpressionNode ex)
|
|
75 |
{
|
|
76 |
super(message);
|
|
77 |
this.setLocator(ex);
|
|
78 |
setStylesheetNode(getStylesheetNode(ex));
|
|
79 |
}
|
|
80 |
|
|
81 |
/**
|
|
82 |
* Create an XPathException object that holds
|
|
83 |
* an error message.
|
|
84 |
* @param message The error message.
|
|
85 |
*/
|
|
86 |
public XPathException(String message)
|
|
87 |
{
|
|
88 |
super(message);
|
|
89 |
}
|
|
90 |
|
|
91 |
|
|
92 |
/**
|
|
93 |
* Get the XSLT ElemVariable that this sub-expression references. In order for
|
|
94 |
* this to work, the SourceLocator must be the owning ElemTemplateElement.
|
|
95 |
* @return The dereference to the ElemVariable, or null if not found.
|
|
96 |
*/
|
|
97 |
public org.w3c.dom.Node getStylesheetNode(ExpressionNode ex)
|
|
98 |
{
|
|
99 |
|
|
100 |
ExpressionNode owner = getExpressionOwner(ex);
|
|
101 |
|
|
102 |
if (null != owner && owner instanceof org.w3c.dom.Node)
|
|
103 |
{
|
|
104 |
return ((org.w3c.dom.Node)owner);
|
|
105 |
}
|
|
106 |
return null;
|
|
107 |
|
|
108 |
}
|
|
109 |
|
|
110 |
/**
|
|
111 |
* Get the first non-Expression parent of this node.
|
|
112 |
* @return null or first ancestor that is not an Expression.
|
|
113 |
*/
|
|
114 |
protected ExpressionNode getExpressionOwner(ExpressionNode ex)
|
|
115 |
{
|
|
116 |
ExpressionNode parent = ex.exprGetParent();
|
|
117 |
while((null != parent) && (parent instanceof Expression))
|
|
118 |
parent = parent.exprGetParent();
|
|
119 |
return parent;
|
|
120 |
}
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
/**
|
|
125 |
* Create an XPathException object that holds
|
|
126 |
* an error message and the stylesheet node that
|
|
127 |
* the error originated from.
|
|
128 |
* @param message The error message.
|
|
129 |
* @param styleNode The stylesheet node that the error originated from.
|
|
130 |
*/
|
|
131 |
public XPathException(String message, Object styleNode)
|
|
132 |
{
|
|
133 |
|
|
134 |
super(message);
|
|
135 |
|
|
136 |
m_styleNode = styleNode;
|
|
137 |
}
|
|
138 |
|
|
139 |
/**
|
|
140 |
* Create an XPathException object that holds
|
|
141 |
* an error message, the stylesheet node that
|
|
142 |
* the error originated from, and another exception
|
|
143 |
* that caused this exception.
|
|
144 |
* @param message The error message.
|
|
145 |
* @param styleNode The stylesheet node that the error originated from.
|
|
146 |
* @param e The exception that caused this exception.
|
|
147 |
*/
|
|
148 |
public XPathException(String message, Node styleNode, Exception e)
|
|
149 |
{
|
|
150 |
|
|
151 |
super(message);
|
|
152 |
|
|
153 |
m_styleNode = styleNode;
|
|
154 |
this.m_exception = e;
|
|
155 |
}
|
|
156 |
|
|
157 |
/**
|
|
158 |
* Create an XPathException object that holds
|
|
159 |
* an error message, and another exception
|
|
160 |
* that caused this exception.
|
|
161 |
* @param message The error message.
|
|
162 |
* @param e The exception that caused this exception.
|
|
163 |
*/
|
|
164 |
public XPathException(String message, Exception e)
|
|
165 |
{
|
|
166 |
|
|
167 |
super(message);
|
|
168 |
|
|
169 |
this.m_exception = e;
|
|
170 |
}
|
|
171 |
|
|
172 |
/**
|
|
173 |
* Print the the trace of methods from where the error
|
|
174 |
* originated. This will trace all nested exception
|
|
175 |
* objects, as well as this object.
|
|
176 |
* @param s The stream where the dump will be sent to.
|
|
177 |
*/
|
|
178 |
public void printStackTrace(java.io.PrintStream s)
|
|
179 |
{
|
|
180 |
|
|
181 |
if (s == null)
|
|
182 |
s = System.err;
|
|
183 |
|
|
184 |
try
|
|
185 |
{
|
|
186 |
super.printStackTrace(s);
|
|
187 |
}
|
|
188 |
catch (Exception e){}
|
|
189 |
|
|
190 |
Throwable exception = m_exception;
|
|
191 |
|
|
192 |
for (int i = 0; (i < 10) && (null != exception); i++)
|
|
193 |
{
|
|
194 |
s.println("---------");
|
|
195 |
exception.printStackTrace(s);
|
|
196 |
|
|
197 |
if (exception instanceof TransformerException)
|
|
198 |
{
|
|
199 |
TransformerException se = (TransformerException) exception;
|
|
200 |
Throwable prev = exception;
|
|
201 |
|
|
202 |
exception = se.getException();
|
|
203 |
|
|
204 |
if (prev == exception)
|
|
205 |
break;
|
|
206 |
}
|
|
207 |
else
|
|
208 |
{
|
|
209 |
exception = null;
|
|
210 |
}
|
|
211 |
}
|
|
212 |
}
|
|
213 |
|
|
214 |
/**
|
|
215 |
* Find the most contained message.
|
|
216 |
*
|
|
217 |
* @return The error message of the originating exception.
|
|
218 |
*/
|
|
219 |
public String getMessage()
|
|
220 |
{
|
|
221 |
|
|
222 |
String lastMessage = super.getMessage();
|
|
223 |
Throwable exception = m_exception;
|
|
224 |
|
|
225 |
while (null != exception)
|
|
226 |
{
|
|
227 |
String nextMessage = exception.getMessage();
|
|
228 |
|
|
229 |
if (null != nextMessage)
|
|
230 |
lastMessage = nextMessage;
|
|
231 |
|
|
232 |
if (exception instanceof TransformerException)
|
|
233 |
{
|
|
234 |
TransformerException se = (TransformerException) exception;
|
|
235 |
Throwable prev = exception;
|
|
236 |
|
|
237 |
exception = se.getException();
|
|
238 |
|
|
239 |
if (prev == exception)
|
|
240 |
break;
|
|
241 |
}
|
|
242 |
else
|
|
243 |
{
|
|
244 |
exception = null;
|
|
245 |
}
|
|
246 |
}
|
|
247 |
|
|
248 |
return (null != lastMessage) ? lastMessage : "";
|
|
249 |
}
|
|
250 |
|
|
251 |
/**
|
|
252 |
* Print the the trace of methods from where the error
|
|
253 |
* originated. This will trace all nested exception
|
|
254 |
* objects, as well as this object.
|
|
255 |
* @param s The writer where the dump will be sent to.
|
|
256 |
*/
|
|
257 |
public void printStackTrace(java.io.PrintWriter s)
|
|
258 |
{
|
|
259 |
|
|
260 |
if (s == null)
|
|
261 |
s = new java.io.PrintWriter(System.err);
|
|
262 |
|
|
263 |
try
|
|
264 |
{
|
|
265 |
super.printStackTrace(s);
|
|
266 |
}
|
|
267 |
catch (Exception e){}
|
|
268 |
|
|
269 |
|
|
270 |
boolean isJdk14OrHigher = false;
|
|
271 |
try {
|
|
272 |
Throwable.class.getMethod("getCause", (Class[]) null);
|
|
273 |
isJdk14OrHigher = true;
|
|
274 |
} catch (NoSuchMethodException nsme) {
|
|
275 |
// do nothing
|
|
276 |
}
|
|
277 |
|
|
278 |
// The printStackTrace method of the Throwable class in jdk 1.4
|
|
279 |
// and higher will include the cause when printing the backtrace.
|
|
280 |
// The following code is only required when using jdk 1.3 or lower
|
|
281 |
if (!isJdk14OrHigher) {
|
|
282 |
|
|
283 |
Throwable exception = m_exception;
|
|
284 |
|
|
285 |
for (int i = 0; (i < 10) && (null != exception); i++)
|
|
286 |
{
|
|
287 |
s.println("---------");
|
|
288 |
|
|
289 |
try
|
|
290 |
{
|
|
291 |
exception.printStackTrace(s);
|
|
292 |
}
|
|
293 |
catch (Exception e)
|
|
294 |
{
|
|
295 |
s.println("Could not print stack trace...");
|
|
296 |
}
|
|
297 |
|
|
298 |
if (exception instanceof TransformerException)
|
|
299 |
{
|
|
300 |
TransformerException se = (TransformerException) exception;
|
|
301 |
Throwable prev = exception;
|
|
302 |
|
|
303 |
exception = se.getException();
|
|
304 |
|
|
305 |
if (prev == exception)
|
|
306 |
{
|
|
307 |
exception = null;
|
|
308 |
|
|
309 |
break;
|
|
310 |
}
|
|
311 |
}
|
|
312 |
else
|
|
313 |
{
|
|
314 |
exception = null;
|
|
315 |
}
|
|
316 |
}
|
|
317 |
}
|
|
318 |
}
|
|
319 |
|
|
320 |
/**
|
|
321 |
* Return the embedded exception, if any.
|
|
322 |
* Overrides javax.xml.transform.TransformerException.getException().
|
|
323 |
*
|
|
324 |
* @return The embedded exception, or null if there is none.
|
|
325 |
*/
|
|
326 |
public Throwable getException()
|
|
327 |
{
|
|
328 |
return m_exception;
|
|
329 |
}
|
|
330 |
}
|