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: package org.apache.tools.ant;
019:
020: import java.io.PrintStream;
021: import java.io.PrintWriter;
022:
023: /**
024: * Signals an error condition during a build
025: */
026: public class BuildException extends RuntimeException {
027:
028: /** Exception that might have caused this one. */
029: private Throwable cause;
030:
031: /** Location in the build file where the exception occurred */
032: private Location location = Location.UNKNOWN_LOCATION;
033:
034: /**
035: * Constructs a build exception with no descriptive information.
036: */
037: public BuildException() {
038: super ();
039: }
040:
041: /**
042: * Constructs an exception with the given descriptive message.
043: *
044: * @param message A description of or information about the exception.
045: * Should not be <code>null</code>.
046: */
047: public BuildException(String message) {
048: super (message);
049: }
050:
051: /**
052: * Constructs an exception with the given message and exception as
053: * a root cause.
054: *
055: * @param message A description of or information about the exception.
056: * Should not be <code>null</code> unless a cause is specified.
057: * @param cause The exception that might have caused this one.
058: * May be <code>null</code>.
059: */
060: public BuildException(String message, Throwable cause) {
061: super (message);
062: this .cause = cause;
063: }
064:
065: /**
066: * Constructs an exception with the given message and exception as
067: * a root cause and a location in a file.
068: *
069: * @param msg A description of or information about the exception.
070: * Should not be <code>null</code> unless a cause is specified.
071: * @param cause The exception that might have caused this one.
072: * May be <code>null</code>.
073: * @param location The location in the project file where the error
074: * occurred. Must not be <code>null</code>.
075: */
076: public BuildException(String msg, Throwable cause, Location location) {
077: this (msg, cause);
078: this .location = location;
079: }
080:
081: /**
082: * Constructs an exception with the given exception as a root cause.
083: *
084: * @param cause The exception that might have caused this one.
085: * Should not be <code>null</code>.
086: */
087: public BuildException(Throwable cause) {
088: super (cause.toString());
089: this .cause = cause;
090: }
091:
092: /**
093: * Constructs an exception with the given descriptive message and a
094: * location in a file.
095: *
096: * @param message A description of or information about the exception.
097: * Should not be <code>null</code>.
098: * @param location The location in the project file where the error
099: * occurred. Must not be <code>null</code>.
100: */
101: public BuildException(String message, Location location) {
102: super (message);
103: this .location = location;
104: }
105:
106: /**
107: * Constructs an exception with the given exception as
108: * a root cause and a location in a file.
109: *
110: * @param cause The exception that might have caused this one.
111: * Should not be <code>null</code>.
112: * @param location The location in the project file where the error
113: * occurred. Must not be <code>null</code>.
114: */
115: public BuildException(Throwable cause, Location location) {
116: this (cause);
117: this .location = location;
118: }
119:
120: /**
121: * Returns the nested exception, if any.
122: *
123: * @return the nested exception, or <code>null</code> if no
124: * exception is associated with this one
125: */
126: public Throwable getException() {
127: return cause;
128: }
129:
130: /**
131: * Returns the nested exception, if any.
132: *
133: * @return the nested exception, or <code>null</code> if no
134: * exception is associated with this one
135: */
136: public Throwable getCause() {
137: return getException();
138: }
139:
140: /**
141: * Returns the location of the error and the error message.
142: *
143: * @return the location of the error and the error message
144: */
145: public String toString() {
146: return location.toString() + getMessage();
147: }
148:
149: /**
150: * Sets the file location where the error occurred.
151: *
152: * @param location The file location where the error occurred.
153: * Must not be <code>null</code>.
154: */
155: public void setLocation(Location location) {
156: this .location = location;
157: }
158:
159: /**
160: * Returns the file location where the error occurred.
161: *
162: * @return the file location where the error occurred.
163: */
164: public Location getLocation() {
165: return location;
166: }
167:
168: /**
169: * Prints the stack trace for this exception and any
170: * nested exception to <code>System.err</code>.
171: */
172: public void printStackTrace() {
173: printStackTrace(System.err);
174: }
175:
176: /**
177: * Prints the stack trace of this exception and any nested
178: * exception to the specified PrintStream.
179: *
180: * @param ps The PrintStream to print the stack trace to.
181: * Must not be <code>null</code>.
182: */
183: public void printStackTrace(PrintStream ps) {
184: synchronized (ps) {
185: super .printStackTrace(ps);
186: if (cause != null) {
187: ps.println("--- Nested Exception ---");
188: cause.printStackTrace(ps);
189: }
190: }
191: }
192:
193: /**
194: * Prints the stack trace of this exception and any nested
195: * exception to the specified PrintWriter.
196: *
197: * @param pw The PrintWriter to print the stack trace to.
198: * Must not be <code>null</code>.
199: */
200: public void printStackTrace(PrintWriter pw) {
201: synchronized (pw) {
202: super .printStackTrace(pw);
203: if (cause != null) {
204: pw.println("--- Nested Exception ---");
205: cause.printStackTrace(pw);
206: }
207: }
208: }
209: }
|