You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FOPException.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. /* $Id$ */
  17. package org.apache.fop.apps;
  18. import org.xml.sax.Locator;
  19. import org.xml.sax.SAXException;
  20. /**
  21. * Exception thrown when FOP has a problem.
  22. */
  23. public class FOPException extends SAXException {
  24. private static final String EXCEPTION_SEPARATOR = "\n---------\n";
  25. private String systemId;
  26. private int line;
  27. private int column;
  28. /**
  29. * Constructs a new FOP exception with the specified detail message.
  30. * @param message the detail message.
  31. */
  32. public FOPException(String message) {
  33. super(message);
  34. }
  35. /**
  36. * Constructs a new FOP exception with the specified detail message and location.
  37. * @param message the detail message
  38. * @param systemId the system id of the FO document which is associated with the exception
  39. * may be null.
  40. * @param line line number in the FO document which is associated with the exception.
  41. * @param column clolumn number in the line which is associated with the exception.
  42. */
  43. public FOPException(String message, String systemId, int line, int column) {
  44. super(message);
  45. this.systemId = systemId;
  46. this.line = line;
  47. this.column = column;
  48. }
  49. /**
  50. * Constructs a new FOP exception with the specified detail message and location.
  51. * @param message the detail message.
  52. * @param locator the locator holding the location.
  53. */
  54. public FOPException(String message, Locator locator) {
  55. super(message);
  56. setLocator(locator);
  57. }
  58. /**
  59. * Constructs a new FOP exception with the specified cause.
  60. * @param cause the cause.
  61. */
  62. public FOPException(Exception cause) {
  63. super(cause);
  64. }
  65. /**
  66. * Constructs a new exception with the specified detail message and cause.
  67. * @param message the detail message
  68. * @param cause the cause
  69. */
  70. public FOPException(String message, Exception cause) {
  71. super(message, cause);
  72. }
  73. /**
  74. * Set a location associated with the exception.
  75. * @param locator the locator holding the location.
  76. */
  77. public void setLocator(Locator locator) {
  78. if (locator != null) {
  79. this.systemId = locator.getSystemId();
  80. this.line = locator.getLineNumber();
  81. this.column = locator.getColumnNumber();
  82. }
  83. }
  84. /**
  85. * Set a location associated with the exception.
  86. * @param systemId the system id of the FO document which is associated with the exception;
  87. * may be null.
  88. * @param line line number in the FO document which is associated with the exception.
  89. * @param column clolumn number in the line which is associated with the exception.
  90. */
  91. public void setLocation(String systemId, int line, int column) {
  92. this.systemId = systemId;
  93. this.line = line;
  94. this.column = column;
  95. }
  96. /**
  97. * Indicate whether a location was set.
  98. * @return whether a location was set
  99. */
  100. public boolean isLocationSet() {
  101. // TODO: this is actually a dangerous assumption: A line
  102. // number of 0 or -1 might be used to indicate an unknown line
  103. // number, while the system ID might still be of use.
  104. return line > 0;
  105. }
  106. /**
  107. * Returns the detail message string of this FOP exception.
  108. * If a location was set, the message is prepended with it in the
  109. * form
  110. * <pre>
  111. * SystemId:LL:CC: &amp;the message&amp;
  112. * </pre>
  113. * (the format used by most GNU tools)
  114. * @return the detail message string of this FOP exception
  115. */
  116. public String getMessage() {
  117. if (isLocationSet()) {
  118. return systemId + ":" + line + ":" + column + ": " + super.getMessage();
  119. } else {
  120. return super.getMessage();
  121. }
  122. }
  123. /**
  124. * Attempts to recast the exception as other Throwable types.
  125. * @return the exception recast as another type if possible, otherwise null.
  126. */
  127. protected Throwable getRootException() {
  128. Throwable result = getException();
  129. if (result instanceof SAXException) {
  130. result = ((SAXException)result).getException();
  131. }
  132. if (result instanceof java.lang.reflect.InvocationTargetException) {
  133. result
  134. = ((java.lang.reflect.InvocationTargetException)result).getTargetException();
  135. }
  136. if (result != getException()) {
  137. return result;
  138. }
  139. return null;
  140. }
  141. /**
  142. * Prints this FOP exception and its backtrace to the standard error stream.
  143. */
  144. public void printStackTrace() {
  145. synchronized (System.err) {
  146. super.printStackTrace();
  147. if (getException() != null) {
  148. System.err.println(EXCEPTION_SEPARATOR);
  149. getException().printStackTrace();
  150. }
  151. if (getRootException() != null) {
  152. System.err.println(EXCEPTION_SEPARATOR);
  153. getRootException().printStackTrace();
  154. }
  155. }
  156. }
  157. /**
  158. * Prints this FOP exception and its backtrace to the specified print stream.
  159. * @param stream PrintStream to use for output
  160. */
  161. public void printStackTrace(java.io.PrintStream stream) {
  162. synchronized (stream) {
  163. super.printStackTrace(stream);
  164. if (getException() != null) {
  165. stream.println(EXCEPTION_SEPARATOR);
  166. getException().printStackTrace(stream);
  167. }
  168. if (getRootException() != null) {
  169. stream.println(EXCEPTION_SEPARATOR);
  170. getRootException().printStackTrace(stream);
  171. }
  172. }
  173. }
  174. /**
  175. * Prints this FOP exception and its backtrace to the specified print writer.
  176. * @param writer PrintWriter to use for output
  177. */
  178. public void printStackTrace(java.io.PrintWriter writer) {
  179. synchronized (writer) {
  180. super.printStackTrace(writer);
  181. if (getException() != null) {
  182. writer.println(EXCEPTION_SEPARATOR);
  183. getException().printStackTrace(writer);
  184. }
  185. if (getRootException() != null) {
  186. writer.println(EXCEPTION_SEPARATOR);
  187. getRootException().printStackTrace(writer);
  188. }
  189. }
  190. }
  191. }