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: 
  38: package javax.xml.transform;
  39: 
  40: import gnu.java.lang.CPStringBuilder;
  41: 
  42: import java.io.PrintStream;
  43: import java.io.PrintWriter;
  44: 
  45: /**
  46:  * An exception occurred during the transformation process.
  47:  *
  48:  * @author (a href='mailto:dog@gnu.org'>Chris Burdess</a)
  49:  */
  50: public class TransformerException
  51:   extends Exception
  52: {
  53:   private static final long serialVersionUID = 975798773772956428L;
  54: 
  55:   // Field names fixed by serialization spec.
  56:   private SourceLocator  locator;
  57:   private Throwable  containedException;
  58: 
  59:   /**
  60:    * Constructor with a detail message.
  61:    */
  62:   public TransformerException(String msg)
  63:   {
  64:     this(msg, null, null);
  65:   }
  66: 
  67:   /**
  68:    * Constructor with an underlying cause.
  69:    */
  70:   public TransformerException(Throwable cause)
  71:   {
  72:     this(cause.getMessage(), null, cause);
  73:   }
  74: 
  75:   /**
  76:    * Constructor with a detail message and underlying cause.
  77:    */
  78:   public TransformerException(String msg, Throwable cause)
  79:   {
  80:     this(msg, null, cause);
  81:   }
  82: 
  83:   /**
  84:    * Constructor with a detail message and locator.
  85:    */
  86:   public TransformerException(String msg, SourceLocator locator)
  87:   {
  88:     this(msg, locator, null);
  89:   }
  90: 
  91:   /**
  92:    * Constructor with detail message, locator and underlying cause.
  93:    */
  94:   public TransformerException(String msg, SourceLocator locator,
  95:                               Throwable cause)
  96:   {
  97:     super(msg);
  98:     this.locator = locator;
  99:     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: }