Source for javax.xml.transform.TransformerException

   1: /* TransformerException.java -- 
   2:    Copyright (C) 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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.
  20: 
  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.
  25: 
  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. */
  37: package javax.xml.transform;
  38: 
  39: import java.io.PrintStream;
  40: import java.io.PrintWriter;
  41: 
  42: /**
  43:  * An exception occurred during the transformation process.
  44:  *
  45:  * @author (a href='mailto:dog@gnu.org'>Chris Burdess</a)
  46:  */
  47: public class TransformerException
  48:   extends Exception
  49: {
  50:   private static final long serialVersionUID = 975798773772956428L;
  51: 
  52:   // Field names fixed by serialization spec.
  53:   private SourceLocator  locator;
  54:   private Throwable  containedException;
  55: 
  56:   /**
  57:    * Constructor with a detail message.
  58:    */
  59:   public TransformerException(String msg)
  60:   {
  61:     this(msg, null, null);
  62:   }
  63: 
  64:   /**
  65:    * Constructor with an underlying cause.
  66:    */
  67:   public TransformerException(Throwable cause)
  68:   {
  69:     this(cause.getMessage(), null, cause);
  70:   }
  71: 
  72:   /**
  73:    * Constructor with a detail message and underlying cause.
  74:    */
  75:   public TransformerException(String msg, Throwable cause)
  76:   {
  77:     this(msg, null, cause);
  78:   }
  79: 
  80:   /**
  81:    * Constructor with a detail message and locator.
  82:    */
  83:   public TransformerException(String msg, SourceLocator locator)
  84:   {
  85:     this(msg, locator, null);
  86:   }
  87: 
  88:   /**
  89:    * Constructor with detail message, locator and underlying cause.
  90:    */
  91:   public TransformerException(String msg, SourceLocator locator, 
  92:                               Throwable cause)
  93:   {
  94:     super(msg);
  95:     this.locator = locator;
  96:     if (cause != null)
  97:       {
  98:         initCause(cause);
  99:         this.containedException = cause;
 100:       }
 101:   }
 102: 
 103:   /**
 104:    * Returns a locator indicating where the error occurred.
 105:    */
 106:   public SourceLocator getLocator()
 107:   {
 108:     return locator;
 109:   }
 110: 
 111:   /**
 112:    * Sets the locator indicating where the error occurred.
 113:    */
 114:   public void setLocator(SourceLocator location)
 115:   {
 116:     locator = location;
 117:   }
 118: 
 119:   /**
 120:    * Returns the underlying cause of this exception.
 121:    */
 122:   public Throwable getException()
 123:   {
 124:     return containedException;
 125:   }
 126: 
 127:   /**
 128:    * Returns the underlying cause of this exception.
 129:    */
 130:   public Throwable getCause()
 131:   {
 132:     return containedException;
 133:   }
 134: 
 135:   /**
 136:    * Initializes the root cause of this exception.
 137:    * This method may be called only once, and will be called by the
 138:    * constructor if a non-null cause is specified.
 139:    * Really phenomenally poor API design.
 140:    * @param cause the underlying cause
 141:    * @exception IllegalArgumentException if this exception is passed as the
 142:    * argument
 143:    * @exception IllegalStateException if a cause has already been
 144:    * initialized
 145:    */
 146:   public Throwable initCause(Throwable cause)
 147:   {
 148:     if (this.containedException != null)
 149:       {
 150:         throw new IllegalStateException();
 151:       }
 152:     if (cause == this)
 153:       {
 154:         throw new IllegalArgumentException();
 155:       }
 156:     this.containedException = cause;
 157:     return this;
 158:   }
 159: 
 160:   /**
 161:    * Returns the exception message with location information appended.
 162:    */
 163:   public String getMessageAndLocation()
 164:   {
 165:     return (locator == null) ? getMessage() :
 166:       getMessage() + ": " + getLocationAsString();
 167:   }
 168: 
 169:   /**
 170:    * Returns the location information as a string.
 171:    */
 172:   public String getLocationAsString()
 173:   {
 174:     if (locator == null)
 175:       {
 176:         return null;
 177:       }
 178:     String publicId = locator.getPublicId();
 179:     String systemId = locator.getSystemId();
 180:     int lineNumber = locator.getLineNumber();
 181:     int columnNumber = locator.getColumnNumber();
 182:     StringBuffer buffer = new StringBuffer ();
 183:     if (publicId != null)
 184:       {
 185:         buffer.append ("publicId=");
 186:         buffer.append (publicId);
 187:       }
 188:     if (systemId != null)
 189:       {
 190:         if (buffer.length() > 0)
 191:           {
 192:             buffer.append(' ');
 193:           }
 194:         buffer.append ("systemId=");
 195:         buffer.append (systemId);
 196:       }
 197:     if (lineNumber != -1)
 198:       {
 199:         if (buffer.length() > 0)
 200:           {
 201:             buffer.append(' ');
 202:           }
 203:         buffer.append ("lineNumber=");
 204:         buffer.append (lineNumber);
 205:       }
 206:     if (columnNumber != -1)
 207:       {
 208:         if (buffer.length() > 0)
 209:           {
 210:             buffer.append(' ');
 211:           }
 212:         buffer.append ("columnNumber=");
 213:         buffer.append (columnNumber);
 214:       }
 215:     return buffer.toString();
 216:   }
 217: 
 218:   public void printStackTrace()
 219:   {
 220:     printStackTrace(System.out);
 221:   }
 222: 
 223:   public void printStackTrace(PrintStream s)
 224:   {
 225:     super.printStackTrace(s);
 226:     if (containedException != null)
 227:       {
 228:         s.print("caused by ");
 229:         containedException.printStackTrace(s);
 230:       }
 231:   }
 232: 
 233:   public void printStackTrace(PrintWriter s)
 234:   {
 235:     super.printStackTrace(s);
 236:     if (containedException != null)
 237:       {
 238:         s.print("caused by ");
 239:         containedException.printStackTrace(s);
 240:       }
 241:   }
 242: 
 243: }