001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: FOPException.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.apps;
021:
022: import org.xml.sax.Locator;
023: import org.xml.sax.SAXException;
024:
025: /**
026: * Exception thrown when FOP has a problem.
027: */
028: public class FOPException extends SAXException {
029:
030: private static final String EXCEPTION_SEPARATOR = "\n---------\n";
031:
032: private String systemId;
033: private int line;
034: private int column;
035:
036: /**
037: * Constructs a new FOP exception with the specified detail message.
038: * @param message the detail message.
039: */
040: public FOPException(String message) {
041: super (message);
042: }
043:
044: /**
045: * Constructs a new FOP exception with the specified detail message and location.
046: * @param message the detail message
047: * @param systemId the system id of the FO document which is associated with the exception
048: * may be null.
049: * @param line line number in the FO document which is associated with the exception.
050: * @param column clolumn number in the line which is associated with the exception.
051: */
052: public FOPException(String message, String systemId, int line,
053: int column) {
054: super (message);
055: this .systemId = systemId;
056: this .line = line;
057: this .column = column;
058: }
059:
060: /**
061: * Constructs a new FOP exception with the specified detail message and location.
062: * @param message the detail message.
063: * @param locator the locator holding the location.
064: */
065: public FOPException(String message, Locator locator) {
066: super (message);
067: setLocator(locator);
068: }
069:
070: /**
071: * Constructs a new FOP exception with the specified cause.
072: * @param cause the cause.
073: */
074: public FOPException(Exception cause) {
075: super (cause);
076: }
077:
078: /**
079: * Constructs a new exception with the specified detail message and cause.
080: * @param message the detail message
081: * @param cause the cause
082: */
083: public FOPException(String message, Exception cause) {
084: super (message, cause);
085: }
086:
087: /**
088: * Set a location associated with the exception.
089: * @param locator the locator holding the location.
090: */
091: public void setLocator(Locator locator) {
092: if (locator != null) {
093: this .systemId = locator.getSystemId();
094: this .line = locator.getLineNumber();
095: this .column = locator.getColumnNumber();
096: }
097: }
098:
099: /**
100: * Set a location associated with the exception.
101: * @param systemId the system id of the FO document which is associated with the exception;
102: * may be null.
103: * @param line line number in the FO document which is associated with the exception.
104: * @param column clolumn number in the line which is associated with the exception.
105: */
106: public void setLocation(String systemId, int line, int column) {
107: this .systemId = systemId;
108: this .line = line;
109: this .column = column;
110: }
111:
112: /**
113: * Indicate whether a location was set.
114: * @return whether a location was set
115: */
116: public boolean isLocationSet() {
117: // TODO: this is actually a dangerous assumption: A line
118: // number of 0 or -1 might be used to indicate an unknown line
119: // number, while the system ID might still be of use.
120: return line > 0;
121: }
122:
123: /**
124: * Returns the detail message string of this FOP exception.
125: * If a location was set, the message is prepended with it in the
126: * form
127: * <pre>
128: * SystemId:LL:CC: &the message&
129: * </pre>
130: * (the format used by most GNU tools)
131: * @return the detail message string of this FOP exception
132: */
133: public String getMessage() {
134: if (isLocationSet()) {
135: return systemId + ":" + line + ":" + column + ": "
136: + super .getMessage();
137: } else {
138: return super .getMessage();
139: }
140: }
141:
142: /**
143: * Attempts to recast the exception as other Throwable types.
144: * @return the exception recast as another type if possible, otherwise null.
145: */
146: protected Throwable getRootException() {
147: Throwable result = getException();
148:
149: if (result instanceof SAXException) {
150: result = ((SAXException) result).getException();
151: }
152: if (result instanceof java.lang.reflect.InvocationTargetException) {
153: result = ((java.lang.reflect.InvocationTargetException) result)
154: .getTargetException();
155: }
156: if (result != getException()) {
157: return result;
158: }
159: return null;
160: }
161:
162: /**
163: * Prints this FOP exception and its backtrace to the standard error stream.
164: */
165: public void printStackTrace() {
166: synchronized (System.err) {
167: super .printStackTrace();
168: if (getException() != null) {
169: System.err.println(EXCEPTION_SEPARATOR);
170: getException().printStackTrace();
171: }
172: if (getRootException() != null) {
173: System.err.println(EXCEPTION_SEPARATOR);
174: getRootException().printStackTrace();
175: }
176: }
177: }
178:
179: /**
180: * Prints this FOP exception and its backtrace to the specified print stream.
181: * @param stream PrintStream to use for output
182: */
183: public void printStackTrace(java.io.PrintStream stream) {
184: synchronized (stream) {
185: super .printStackTrace(stream);
186: if (getException() != null) {
187: stream.println(EXCEPTION_SEPARATOR);
188: getException().printStackTrace(stream);
189: }
190: if (getRootException() != null) {
191: stream.println(EXCEPTION_SEPARATOR);
192: getRootException().printStackTrace(stream);
193: }
194: }
195: }
196:
197: /**
198: * Prints this FOP exception and its backtrace to the specified print writer.
199: * @param writer PrintWriter to use for output
200: */
201: public void printStackTrace(java.io.PrintWriter writer) {
202: synchronized (writer) {
203: super.printStackTrace(writer);
204: if (getException() != null) {
205: writer.println(EXCEPTION_SEPARATOR);
206: getException().printStackTrace(writer);
207: }
208: if (getRootException() != null) {
209: writer.println(EXCEPTION_SEPARATOR);
210: getRootException().printStackTrace(writer);
211: }
212: }
213: }
214:
215: }
|