1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xpath;
  58. import java.io.PrintStream;
  59. import java.io.PrintWriter;
  60. import javax.xml.transform.TransformerException;
  61. import org.w3c.dom.Node;
  62. /**
  63. * <meta name="usage" content="general"/>
  64. * This class implements an exception object that all
  65. * XPath classes will throw in case of an error. This class
  66. * extends TransformerException, and may hold other exceptions. In the
  67. * case of nested exceptions, printStackTrace will dump
  68. * all the traces of the nested exceptions, not just the trace
  69. * of this object.
  70. */
  71. public class XPathException extends TransformerException
  72. {
  73. /** The home of the expression that caused the error.
  74. * @serial */
  75. Object m_styleNode = null;
  76. /**
  77. * Get the stylesheet node from where this error originated.
  78. * @return The stylesheet node from where this error originated, or null.
  79. */
  80. public Object getStylesheetNode()
  81. {
  82. return m_styleNode;
  83. }
  84. /**
  85. * Set the stylesheet node from where this error originated.
  86. * @param styleNode The stylesheet node from where this error originated, or null.
  87. */
  88. public void setStylesheetNode(Object styleNode)
  89. {
  90. m_styleNode = styleNode;
  91. }
  92. /** A nested exception.
  93. * @serial */
  94. protected Exception m_exception;
  95. /**
  96. * Create an XPathException object that holds
  97. * an error message.
  98. * @param message The error message.
  99. */
  100. public XPathException(String message, ExpressionNode ex)
  101. {
  102. super(message);
  103. this.setLocator(ex);
  104. setStylesheetNode(getStylesheetNode(ex));
  105. }
  106. /**
  107. * Create an XPathException object that holds
  108. * an error message.
  109. * @param message The error message.
  110. */
  111. public XPathException(String message)
  112. {
  113. super(message);
  114. }
  115. /**
  116. * Get the XSLT ElemVariable that this sub-expression references. In order for
  117. * this to work, the SourceLocator must be the owning ElemTemplateElement.
  118. * @return The dereference to the ElemVariable, or null if not found.
  119. */
  120. public org.w3c.dom.Node getStylesheetNode(ExpressionNode ex)
  121. {
  122. ExpressionNode owner = getExpressionOwner(ex);
  123. if (null != owner && owner instanceof org.w3c.dom.Node)
  124. {
  125. return ((org.w3c.dom.Node)owner);
  126. }
  127. return null;
  128. }
  129. /**
  130. * Get the first non-Expression parent of this node.
  131. * @return null or first ancestor that is not an Expression.
  132. */
  133. protected ExpressionNode getExpressionOwner(ExpressionNode ex)
  134. {
  135. ExpressionNode parent = ex.exprGetParent();
  136. while((null != parent) && (parent instanceof Expression))
  137. parent = parent.exprGetParent();
  138. return parent;
  139. }
  140. /**
  141. * Create an XPathException object that holds
  142. * an error message and the stylesheet node that
  143. * the error originated from.
  144. * @param message The error message.
  145. * @param styleNode The stylesheet node that the error originated from.
  146. */
  147. public XPathException(String message, Object styleNode)
  148. {
  149. super(message);
  150. m_styleNode = styleNode;
  151. }
  152. /**
  153. * Create an XPathException object that holds
  154. * an error message, the stylesheet node that
  155. * the error originated from, and another exception
  156. * that caused this exception.
  157. * @param message The error message.
  158. * @param styleNode The stylesheet node that the error originated from.
  159. * @param e The exception that caused this exception.
  160. */
  161. public XPathException(String message, Node styleNode, Exception e)
  162. {
  163. super(message);
  164. m_styleNode = styleNode;
  165. this.m_exception = e;
  166. }
  167. /**
  168. * Create an XPathException object that holds
  169. * an error message, and another exception
  170. * that caused this exception.
  171. * @param message The error message.
  172. * @param e The exception that caused this exception.
  173. */
  174. public XPathException(String message, Exception e)
  175. {
  176. super(message);
  177. this.m_exception = e;
  178. }
  179. /**
  180. * Print the the trace of methods from where the error
  181. * originated. This will trace all nested exception
  182. * objects, as well as this object.
  183. * @param s The stream where the dump will be sent to.
  184. */
  185. public void printStackTrace(java.io.PrintStream s)
  186. {
  187. if (s == null)
  188. s = System.err;
  189. try
  190. {
  191. super.printStackTrace(s);
  192. }
  193. catch (Exception e){}
  194. Throwable exception = m_exception;
  195. for (int i = 0; (i < 10) && (null != exception); i++)
  196. {
  197. s.println("---------");
  198. exception.printStackTrace(s);
  199. if (exception instanceof TransformerException)
  200. {
  201. TransformerException se = (TransformerException) exception;
  202. Throwable prev = exception;
  203. exception = se.getException();
  204. if (prev == exception)
  205. break;
  206. }
  207. else
  208. {
  209. exception = null;
  210. }
  211. }
  212. }
  213. /**
  214. * Find the most contained message.
  215. *
  216. * @return The error message of the originating exception.
  217. */
  218. public String getMessage()
  219. {
  220. String lastMessage = super.getMessage();
  221. Throwable exception = m_exception;
  222. while (null != exception)
  223. {
  224. String nextMessage = exception.getMessage();
  225. if (null != nextMessage)
  226. lastMessage = nextMessage;
  227. if (exception instanceof TransformerException)
  228. {
  229. TransformerException se = (TransformerException) exception;
  230. Throwable prev = exception;
  231. exception = se.getException();
  232. if (prev == exception)
  233. break;
  234. }
  235. else
  236. {
  237. exception = null;
  238. }
  239. }
  240. return (null != lastMessage) ? lastMessage : "";
  241. }
  242. /**
  243. * Print the the trace of methods from where the error
  244. * originated. This will trace all nested exception
  245. * objects, as well as this object.
  246. * @param s The writer where the dump will be sent to.
  247. */
  248. public void printStackTrace(java.io.PrintWriter s)
  249. {
  250. if (s == null)
  251. s = new java.io.PrintWriter(System.err);
  252. try
  253. {
  254. super.printStackTrace(s);
  255. }
  256. catch (Exception e){}
  257. Throwable exception = m_exception;
  258. for (int i = 0; (i < 10) && (null != exception); i++)
  259. {
  260. s.println("---------");
  261. try
  262. {
  263. exception.printStackTrace(s);
  264. }
  265. catch (Exception e)
  266. {
  267. s.println("Could not print stack trace...");
  268. }
  269. if (exception instanceof TransformerException)
  270. {
  271. TransformerException se = (TransformerException) exception;
  272. Throwable prev = exception;
  273. exception = se.getException();
  274. if (prev == exception)
  275. {
  276. exception = null;
  277. break;
  278. }
  279. }
  280. else
  281. {
  282. exception = null;
  283. }
  284. }
  285. }
  286. /**
  287. * Return the embedded exception, if any.
  288. * Overrides javax.xml.transform.TransformerException.getException().
  289. *
  290. * @return The embedded exception, or null if there is none.
  291. */
  292. public Throwable getException()
  293. {
  294. return m_exception;
  295. }
  296. }