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 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.apps;
  19. import org.xml.sax.Locator;
  20. import org.xml.sax.SAXException;
  21. /**
  22. * Exception thrown when FOP has a problem.
  23. */
  24. public class FOPException extends SAXException {
  25. private static final String EXCEPTION_SEPARATOR = "\n---------\n";
  26. private String systemId;
  27. private int line;
  28. private int column;
  29. private String localizedMessage;
  30. /**
  31. * Constructs a new FOP exception with the specified detail message.
  32. * @param message the detail message.
  33. */
  34. public FOPException(String message) {
  35. super(message);
  36. }
  37. /**
  38. * Constructs a new FOP exception with the specified detail message and location.
  39. * @param message the detail message
  40. * @param systemId the system id of the FO document which is associated with the exception
  41. * may be null.
  42. * @param line line number in the FO document which is associated with the exception.
  43. * @param column clolumn number in the line which is associated with the exception.
  44. */
  45. public FOPException(String message, String systemId, int line, int column) {
  46. super(message);
  47. this.systemId = systemId;
  48. this.line = line;
  49. this.column = column;
  50. }
  51. /**
  52. * Constructs a new FOP exception with the specified detail message and location.
  53. * @param message the detail message.
  54. * @param locator the locator holding the location.
  55. */
  56. public FOPException(String message, Locator locator) {
  57. super(message);
  58. setLocator(locator);
  59. }
  60. /**
  61. * Constructs a new FOP exception with the specified cause.
  62. * @param cause the cause.
  63. */
  64. public FOPException(Exception cause) {
  65. super(cause);
  66. }
  67. /**
  68. * Constructs a new exception with the specified detail message and cause.
  69. * @param message the detail message
  70. * @param cause the cause
  71. */
  72. public FOPException(String message, Exception cause) {
  73. super(message, cause);
  74. }
  75. /**
  76. * Set a location associated with the exception.
  77. * @param locator the locator holding the location.
  78. */
  79. public void setLocator(Locator locator) {
  80. if (locator != null) {
  81. this.systemId = locator.getSystemId();
  82. this.line = locator.getLineNumber();
  83. this.column = locator.getColumnNumber();
  84. }
  85. }
  86. /**
  87. * Set a location associated with the exception.
  88. * @param systemId the system id of the FO document which is associated with the exception;
  89. * may be null.
  90. * @param line line number in the FO document which is associated with the exception.
  91. * @param column clolumn number in the line which is associated with the exception.
  92. */
  93. public void setLocation(String systemId, int line, int column) {
  94. this.systemId = systemId;
  95. this.line = line;
  96. this.column = column;
  97. }
  98. /**
  99. * Indicate whether a location was set.
  100. * @return whether a location was set
  101. */
  102. public boolean isLocationSet() {
  103. // TODO: this is actually a dangerous assumption: A line
  104. // number of 0 or -1 might be used to indicate an unknown line
  105. // number, while the system ID might still be of use.
  106. return line > 0;
  107. }
  108. /**
  109. * Returns the detail message string of this FOP exception.
  110. * If a location was set, the message is prepended with it in the
  111. * form
  112. * <pre>
  113. * SystemId:LL:CC: &amp;the message&amp;
  114. * </pre>
  115. * (the format used by most GNU tools)
  116. * @return the detail message string of this FOP exception
  117. */
  118. public String getMessage() {
  119. if (isLocationSet()) {
  120. return systemId + ":" + line + ":" + column + ": " + super.getMessage();
  121. } else {
  122. return super.getMessage();
  123. }
  124. }
  125. /**
  126. * Attempts to recast the exception as other Throwable types.
  127. * @return the exception recast as another type if possible, otherwise null.
  128. */
  129. protected Throwable getRootException() {
  130. Throwable result = getException();
  131. if (result instanceof SAXException) {
  132. result = ((SAXException)result).getException();
  133. }
  134. if (result instanceof java.lang.reflect.InvocationTargetException) {
  135. result
  136. = ((java.lang.reflect.InvocationTargetException)result).getTargetException();
  137. }
  138. if (result != getException()) {
  139. return result;
  140. }
  141. return null;
  142. }
  143. /**
  144. * Prints this FOP exception and its backtrace to the standard error stream.
  145. */
  146. public void printStackTrace() {
  147. synchronized (System.err) {
  148. super.printStackTrace();
  149. if (getException() != null) {
  150. System.err.println(EXCEPTION_SEPARATOR);
  151. getException().printStackTrace();
  152. }
  153. if (getRootException() != null) {
  154. System.err.println(EXCEPTION_SEPARATOR);
  155. getRootException().printStackTrace();
  156. }
  157. }
  158. }
  159. /**
  160. * Prints this FOP exception and its backtrace to the specified print stream.
  161. * @param stream PrintStream to use for output
  162. */
  163. public void printStackTrace(java.io.PrintStream stream) {
  164. synchronized (stream) {
  165. super.printStackTrace(stream);
  166. if (getException() != null) {
  167. stream.println(EXCEPTION_SEPARATOR);
  168. getException().printStackTrace(stream);
  169. }
  170. if (getRootException() != null) {
  171. stream.println(EXCEPTION_SEPARATOR);
  172. getRootException().printStackTrace(stream);
  173. }
  174. }
  175. }
  176. /**
  177. * Prints this FOP exception and its backtrace to the specified print writer.
  178. * @param writer PrintWriter to use for output
  179. */
  180. public void printStackTrace(java.io.PrintWriter writer) {
  181. synchronized (writer) {
  182. super.printStackTrace(writer);
  183. if (getException() != null) {
  184. writer.println(EXCEPTION_SEPARATOR);
  185. getException().printStackTrace(writer);
  186. }
  187. if (getRootException() != null) {
  188. writer.println(EXCEPTION_SEPARATOR);
  189. getRootException().printStackTrace(writer);
  190. }
  191. }
  192. }
  193. /**
  194. * Sets the localized message for this exception.
  195. * @param msg the localized message
  196. */
  197. public void setLocalizedMessage(String msg) {
  198. this.localizedMessage = msg;
  199. }
  200. /** {@inheritDoc} */
  201. public String getLocalizedMessage() {
  202. if (this.localizedMessage != null) {
  203. return this.localizedMessage;
  204. } else {
  205. return super.getLocalizedMessage();
  206. }
  207. }
  208. }