001/* TransformerException.java --
002   Copyright (C) 2004, 2005  Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038package javax.xml.transform;
039
040import gnu.java.lang.CPStringBuilder;
041
042import java.io.PrintStream;
043import java.io.PrintWriter;
044
045/**
046 * An exception occurred during the transformation process.
047 *
048 * @author (a href='mailto:dog@gnu.org'>Chris Burdess</a)
049 */
050public class TransformerException
051  extends Exception
052{
053  private static final long serialVersionUID = 975798773772956428L;
054
055  // Field names fixed by serialization spec.
056  private SourceLocator  locator;
057  private Throwable  containedException;
058
059  /**
060   * Constructor with a detail message.
061   */
062  public TransformerException(String msg)
063  {
064    this(msg, null, null);
065  }
066
067  /**
068   * Constructor with an underlying cause.
069   */
070  public TransformerException(Throwable cause)
071  {
072    this(cause.getMessage(), null, cause);
073  }
074
075  /**
076   * Constructor with a detail message and underlying cause.
077   */
078  public TransformerException(String msg, Throwable cause)
079  {
080    this(msg, null, cause);
081  }
082
083  /**
084   * Constructor with a detail message and locator.
085   */
086  public TransformerException(String msg, SourceLocator locator)
087  {
088    this(msg, locator, null);
089  }
090
091  /**
092   * Constructor with detail message, locator and underlying cause.
093   */
094  public TransformerException(String msg, SourceLocator locator,
095                              Throwable cause)
096  {
097    super(msg);
098    this.locator = locator;
099    if (cause != null)
100      {
101        initCause(cause);
102        this.containedException = cause;
103      }
104  }
105
106  /**
107   * Returns a locator indicating where the error occurred.
108   */
109  public SourceLocator getLocator()
110  {
111    return locator;
112  }
113
114  /**
115   * Sets the locator indicating where the error occurred.
116   */
117  public void setLocator(SourceLocator location)
118  {
119    locator = location;
120  }
121
122  /**
123   * Returns the underlying cause of this exception.
124   */
125  public Throwable getException()
126  {
127    return containedException;
128  }
129
130  /**
131   * Returns the underlying cause of this exception.
132   */
133  public Throwable getCause()
134  {
135    return containedException;
136  }
137
138  /**
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
148   */
149  public Throwable initCause(Throwable cause)
150  {
151    if (this.containedException != null)
152      {
153        throw new IllegalStateException();
154      }
155    if (cause == this)
156      {
157        throw new IllegalArgumentException();
158      }
159    this.containedException = cause;
160    return this;
161  }
162
163  /**
164   * Returns the exception message with location information appended.
165   */
166  public String getMessageAndLocation()
167  {
168    return (locator == null) ? getMessage() :
169      getMessage() + ": " + getLocationAsString();
170  }
171
172  /**
173   * Returns the location information as a string.
174   */
175  public String getLocationAsString()
176  {
177    if (locator == null)
178      {
179        return null;
180      }
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)
187      {
188        buffer.append ("publicId=");
189        buffer.append (publicId);
190      }
191    if (systemId != null)
192      {
193        if (buffer.length() > 0)
194          {
195            buffer.append(' ');
196          }
197        buffer.append ("systemId=");
198        buffer.append (systemId);
199      }
200    if (lineNumber != -1)
201      {
202        if (buffer.length() > 0)
203          {
204            buffer.append(' ');
205          }
206        buffer.append ("lineNumber=");
207        buffer.append (lineNumber);
208      }
209    if (columnNumber != -1)
210      {
211        if (buffer.length() > 0)
212          {
213            buffer.append(' ');
214          }
215        buffer.append ("columnNumber=");
216        buffer.append (columnNumber);
217      }
218    return buffer.toString();
219  }
220
221  public void printStackTrace()
222  {
223    printStackTrace(System.out);
224  }
225
226  public void printStackTrace(PrintStream s)
227  {
228    super.printStackTrace(s);
229    if (containedException != null)
230      {
231        s.print("caused by ");
232        containedException.printStackTrace(s);
233      }
234  }
235
236  public void printStackTrace(PrintWriter s)
237  {
238    super.printStackTrace(s);
239    if (containedException != null)
240      {
241        s.print("caused by ");
242        containedException.printStackTrace(s);
243      }
244  }
245
246}