TransformerException.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* TransformerException.java --
  2. Copyright (C) 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.xml.transform;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.io.PrintStream;
  34. import java.io.PrintWriter;
  35. /**
  36. * An exception occurred during the transformation process.
  37. *
  38. * @author (a href='mailto:dog@gnu.org'>Chris Burdess</a)
  39. */
  40. public class TransformerException
  41. extends Exception
  42. {
  43. private static final long serialVersionUID = 975798773772956428L;
  44. // Field names fixed by serialization spec.
  45. private SourceLocator locator;
  46. private Throwable containedException;
  47. /**
  48. * Constructor with a detail message.
  49. */
  50. public TransformerException(String msg)
  51. {
  52. this(msg, null, null);
  53. }
  54. /**
  55. * Constructor with an underlying cause.
  56. */
  57. public TransformerException(Throwable cause)
  58. {
  59. this(cause.getMessage(), null, cause);
  60. }
  61. /**
  62. * Constructor with a detail message and underlying cause.
  63. */
  64. public TransformerException(String msg, Throwable cause)
  65. {
  66. this(msg, null, cause);
  67. }
  68. /**
  69. * Constructor with a detail message and locator.
  70. */
  71. public TransformerException(String msg, SourceLocator locator)
  72. {
  73. this(msg, locator, null);
  74. }
  75. /**
  76. * Constructor with detail message, locator and underlying cause.
  77. */
  78. public TransformerException(String msg, SourceLocator locator,
  79. Throwable cause)
  80. {
  81. super(msg);
  82. this.locator = locator;
  83. if (cause != null)
  84. {
  85. initCause(cause);
  86. this.containedException = cause;
  87. }
  88. }
  89. /**
  90. * Returns a locator indicating where the error occurred.
  91. */
  92. public SourceLocator getLocator()
  93. {
  94. return locator;
  95. }
  96. /**
  97. * Sets the locator indicating where the error occurred.
  98. */
  99. public void setLocator(SourceLocator location)
  100. {
  101. locator = location;
  102. }
  103. /**
  104. * Returns the underlying cause of this exception.
  105. */
  106. public Throwable getException()
  107. {
  108. return containedException;
  109. }
  110. /**
  111. * Returns the underlying cause of this exception.
  112. */
  113. public Throwable getCause()
  114. {
  115. return containedException;
  116. }
  117. /**
  118. * Initializes the root cause of this exception.
  119. * This method may be called only once, and will be called by the
  120. * constructor if a non-null cause is specified.
  121. * Really phenomenally poor API design.
  122. * @param cause the underlying cause
  123. * @exception IllegalArgumentException if this exception is passed as the
  124. * argument
  125. * @exception IllegalStateException if a cause has already been
  126. * initialized
  127. */
  128. public Throwable initCause(Throwable cause)
  129. {
  130. if (this.containedException != null)
  131. {
  132. throw new IllegalStateException();
  133. }
  134. if (cause == this)
  135. {
  136. throw new IllegalArgumentException();
  137. }
  138. this.containedException = cause;
  139. return this;
  140. }
  141. /**
  142. * Returns the exception message with location information appended.
  143. */
  144. public String getMessageAndLocation()
  145. {
  146. return (locator == null) ? getMessage() :
  147. getMessage() + ": " + getLocationAsString();
  148. }
  149. /**
  150. * Returns the location information as a string.
  151. */
  152. public String getLocationAsString()
  153. {
  154. if (locator == null)
  155. {
  156. return null;
  157. }
  158. String publicId = locator.getPublicId();
  159. String systemId = locator.getSystemId();
  160. int lineNumber = locator.getLineNumber();
  161. int columnNumber = locator.getColumnNumber();
  162. CPStringBuilder buffer = new CPStringBuilder ();
  163. if (publicId != null)
  164. {
  165. buffer.append ("publicId=");
  166. buffer.append (publicId);
  167. }
  168. if (systemId != null)
  169. {
  170. if (buffer.length() > 0)
  171. {
  172. buffer.append(' ');
  173. }
  174. buffer.append ("systemId=");
  175. buffer.append (systemId);
  176. }
  177. if (lineNumber != -1)
  178. {
  179. if (buffer.length() > 0)
  180. {
  181. buffer.append(' ');
  182. }
  183. buffer.append ("lineNumber=");
  184. buffer.append (lineNumber);
  185. }
  186. if (columnNumber != -1)
  187. {
  188. if (buffer.length() > 0)
  189. {
  190. buffer.append(' ');
  191. }
  192. buffer.append ("columnNumber=");
  193. buffer.append (columnNumber);
  194. }
  195. return buffer.toString();
  196. }
  197. public void printStackTrace()
  198. {
  199. printStackTrace(System.out);
  200. }
  201. public void printStackTrace(PrintStream s)
  202. {
  203. super.printStackTrace(s);
  204. if (containedException != null)
  205. {
  206. s.print("caused by ");
  207. containedException.printStackTrace(s);
  208. }
  209. }
  210. public void printStackTrace(PrintWriter s)
  211. {
  212. super.printStackTrace(s);
  213. if (containedException != null)
  214. {
  215. s.print("caused by ");
  216. containedException.printStackTrace(s);
  217. }
  218. }
  219. }