001 /* TransformerException.java -- 002 Copyright (C) 2004, 2005 Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 package javax.xml.transform; 038 039 import java.io.PrintStream; 040 import java.io.PrintWriter; 041 042 /** 043 * An exception occurred during the transformation process. 044 * 045 * @author (a href='mailto:dog@gnu.org'>Chris Burdess</a) 046 */ 047 public class TransformerException 048 extends Exception 049 { 050 private static final long serialVersionUID = 975798773772956428L; 051 052 // Field names fixed by serialization spec. 053 private SourceLocator locator; 054 private Throwable containedException; 055 056 /** 057 * Constructor with a detail message. 058 */ 059 public TransformerException(String msg) 060 { 061 this(msg, null, null); 062 } 063 064 /** 065 * Constructor with an underlying cause. 066 */ 067 public TransformerException(Throwable cause) 068 { 069 this(cause.getMessage(), null, cause); 070 } 071 072 /** 073 * Constructor with a detail message and underlying cause. 074 */ 075 public TransformerException(String msg, Throwable cause) 076 { 077 this(msg, null, cause); 078 } 079 080 /** 081 * Constructor with a detail message and locator. 082 */ 083 public TransformerException(String msg, SourceLocator locator) 084 { 085 this(msg, locator, null); 086 } 087 088 /** 089 * Constructor with detail message, locator and underlying cause. 090 */ 091 public TransformerException(String msg, SourceLocator locator, 092 Throwable cause) 093 { 094 super(msg); 095 this.locator = locator; 096 if (cause != null) 097 { 098 initCause(cause); 099 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 }