libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / javax / xml / transform / TransformerException.java
blob7a0b5ad9888dc79944f92410199696db852f2339
1 /* TransformerException.java --
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.xml.transform;
40 import gnu.java.lang.CPStringBuilder;
42 import java.io.PrintStream;
43 import java.io.PrintWriter;
45 /**
46 * An exception occurred during the transformation process.
48 * @author (a href='mailto:dog@gnu.org'>Chris Burdess</a)
50 public class TransformerException
51 extends Exception
53 private static final long serialVersionUID = 975798773772956428L;
55 // Field names fixed by serialization spec.
56 private SourceLocator locator;
57 private Throwable containedException;
59 /**
60 * Constructor with a detail message.
62 public TransformerException(String msg)
64 this(msg, null, null);
67 /**
68 * Constructor with an underlying cause.
70 public TransformerException(Throwable cause)
72 this(cause.getMessage(), null, cause);
75 /**
76 * Constructor with a detail message and underlying cause.
78 public TransformerException(String msg, Throwable cause)
80 this(msg, null, cause);
83 /**
84 * Constructor with a detail message and locator.
86 public TransformerException(String msg, SourceLocator locator)
88 this(msg, locator, null);
91 /**
92 * Constructor with detail message, locator and underlying cause.
94 public TransformerException(String msg, SourceLocator locator,
95 Throwable cause)
97 super(msg);
98 this.locator = locator;
99 if (cause != null)
101 initCause(cause);
102 this.containedException = cause;
107 * Returns a locator indicating where the error occurred.
109 public SourceLocator getLocator()
111 return locator;
115 * Sets the locator indicating where the error occurred.
117 public void setLocator(SourceLocator location)
119 locator = location;
123 * Returns the underlying cause of this exception.
125 public Throwable getException()
127 return containedException;
131 * Returns the underlying cause of this exception.
133 public Throwable getCause()
135 return containedException;
139 * Initializes the root cause of this exception.
140 * This method may be called only once, and will be called by the
141 * constructor if a non-null cause is specified.
142 * Really phenomenally poor API design.
143 * @param cause the underlying cause
144 * @exception IllegalArgumentException if this exception is passed as the
145 * argument
146 * @exception IllegalStateException if a cause has already been
147 * initialized
149 public Throwable initCause(Throwable cause)
151 if (this.containedException != null)
153 throw new IllegalStateException();
155 if (cause == this)
157 throw new IllegalArgumentException();
159 this.containedException = cause;
160 return this;
164 * Returns the exception message with location information appended.
166 public String getMessageAndLocation()
168 return (locator == null) ? getMessage() :
169 getMessage() + ": " + getLocationAsString();
173 * Returns the location information as a string.
175 public String getLocationAsString()
177 if (locator == null)
179 return null;
181 String publicId = locator.getPublicId();
182 String systemId = locator.getSystemId();
183 int lineNumber = locator.getLineNumber();
184 int columnNumber = locator.getColumnNumber();
185 CPStringBuilder buffer = new CPStringBuilder ();
186 if (publicId != null)
188 buffer.append ("publicId=");
189 buffer.append (publicId);
191 if (systemId != null)
193 if (buffer.length() > 0)
195 buffer.append(' ');
197 buffer.append ("systemId=");
198 buffer.append (systemId);
200 if (lineNumber != -1)
202 if (buffer.length() > 0)
204 buffer.append(' ');
206 buffer.append ("lineNumber=");
207 buffer.append (lineNumber);
209 if (columnNumber != -1)
211 if (buffer.length() > 0)
213 buffer.append(' ');
215 buffer.append ("columnNumber=");
216 buffer.append (columnNumber);
218 return buffer.toString();
221 public void printStackTrace()
223 printStackTrace(System.out);
226 public void printStackTrace(PrintStream s)
228 super.printStackTrace(s);
229 if (containedException != null)
231 s.print("caused by ");
232 containedException.printStackTrace(s);
236 public void printStackTrace(PrintWriter s)
238 super.printStackTrace(s);
239 if (containedException != null)
241 s.print("caused by ");
242 containedException.printStackTrace(s);