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