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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.SAXException;
  19. /**
  20. * Exception thrown when FOP has a problem.
  21. */
  22. public class FOPException extends Exception {
  23. private static final String EXCEPTION_SEPARATOR = "\n---------\n";
  24. private Throwable exception;
  25. private String systemId;
  26. private int line;
  27. private int column;
  28. /**
  29. * create a new FOP Exception
  30. *
  31. * @param message descriptive message
  32. */
  33. public FOPException(String message) {
  34. super(message);
  35. }
  36. public FOPException(String message, String systemId, int line, int column) {
  37. super(message);
  38. this.systemId = systemId;
  39. this.line = line;
  40. this.column = column;
  41. }
  42. /**
  43. *
  44. * @param e Throwable object
  45. */
  46. public FOPException(Throwable e) {
  47. super(e.getMessage());
  48. setException(e);
  49. }
  50. /**
  51. *
  52. * @param message descriptive message
  53. * @param e Throwable object
  54. */
  55. public FOPException(String message, Throwable e) {
  56. super(message);
  57. setException(e);
  58. }
  59. /**
  60. * Sets exception
  61. * @param t Throwable object
  62. */
  63. protected void setException(Throwable t) {
  64. exception = t;
  65. }
  66. /**
  67. * Accessor for exception
  68. * @return exception
  69. */
  70. public Throwable getException() {
  71. return exception;
  72. }
  73. public void setLocation(String systemId, int line, int column) {
  74. this.systemId = systemId;
  75. this.line = line;
  76. this.column = column;
  77. }
  78. public boolean isLocationSet() {
  79. return line>=0;
  80. }
  81. /**
  82. * Attempts to recast the exception as other Throwable types.
  83. * @return the exception recast as another type if possible, otherwise null.
  84. */
  85. protected Throwable getRootException() {
  86. Throwable result = exception;
  87. if (result instanceof SAXException) {
  88. result = ((SAXException)result).getException();
  89. }
  90. if (result instanceof java.lang.reflect.InvocationTargetException) {
  91. result =
  92. ((java.lang.reflect.InvocationTargetException)result).getTargetException();
  93. }
  94. if (result != exception) {
  95. return result;
  96. }
  97. return null;
  98. }
  99. /**
  100. * Write stack trace to stderr
  101. */
  102. public void printStackTrace() {
  103. synchronized (System.err) {
  104. super.printStackTrace();
  105. if (exception != null) {
  106. System.err.println(EXCEPTION_SEPARATOR);
  107. exception.printStackTrace();
  108. }
  109. if (getRootException() != null) {
  110. System.err.println(EXCEPTION_SEPARATOR);
  111. getRootException().printStackTrace();
  112. }
  113. }
  114. }
  115. /**
  116. * write stack trace on a PrintStream
  117. * @param stream PrintStream on which to write stack trace
  118. */
  119. public void printStackTrace(java.io.PrintStream stream) {
  120. synchronized (stream) {
  121. super.printStackTrace(stream);
  122. if (exception != null) {
  123. stream.println(EXCEPTION_SEPARATOR);
  124. exception.printStackTrace(stream);
  125. }
  126. if (getRootException() != null) {
  127. stream.println(EXCEPTION_SEPARATOR);
  128. getRootException().printStackTrace(stream);
  129. }
  130. }
  131. }
  132. /**
  133. * Write stack trace on a PrintWriter
  134. * @param writer PrintWriter on which to write stack trace
  135. */
  136. public void printStackTrace(java.io.PrintWriter writer) {
  137. synchronized (writer) {
  138. super.printStackTrace(writer);
  139. if (exception != null) {
  140. writer.println(EXCEPTION_SEPARATOR);
  141. exception.printStackTrace(writer);
  142. }
  143. if (getRootException() != null) {
  144. writer.println(EXCEPTION_SEPARATOR);
  145. getRootException().printStackTrace(writer);
  146. }
  147. }
  148. }
  149. }