1 50 51 package org.openlaszlo.iv.flash.util; 52 53 import java.util.*; 54 import java.io.*; 55 56 76 public class IVException extends Exception { 77 78 private String message; 79 private Throwable cause; 80 private String key; 81 private Object [] parms; 82 private ResourceBundle bundle; 83 84 public IVException() { 85 } 86 87 public IVException( String key ) { 88 this.key = key; 89 } 90 91 public IVException( ResourceBundle bundle, String key ) { 92 this.key = key; 93 this.bundle = bundle; 94 } 95 96 public IVException( String key, Object [] parms ) { 97 this.key = key; 98 this.parms = parms; 99 } 100 101 public IVException( ResourceBundle bundle, String key, Object [] parms ) { 102 this.key = key; 103 this.parms = parms; 104 this.bundle = bundle; 105 } 106 107 public IVException( String key, Throwable cause ) { 108 this.key = key; 109 this.cause = cause; 110 } 111 112 public IVException( ResourceBundle bundle, String key, Throwable cause ) { 113 this.key = key; 114 this.cause = cause; 115 this.bundle = bundle; 116 } 117 118 public IVException( String key, Object [] parms, Throwable cause ) { 119 this.key = key; 120 this.parms = parms; 121 this.cause = cause; 122 } 123 124 public IVException( ResourceBundle bundle, String key, Object [] parms, Throwable cause ) { 125 this.key = key; 126 this.parms = parms; 127 this.cause = cause; 128 this.bundle = bundle; 129 } 130 131 public IVException( Throwable cause ) { 132 this.cause = cause; 133 } 134 135 public Throwable getCause() { 136 return cause; 137 } 138 139 public String getMessageKey() { 140 return key; 141 } 142 143 public String getMessage() { 144 if( message == null ) { 145 String ourMsg = bundle==null? Log.getMessage(key, parms): Log.getMessage(bundle, key, parms); 146 StringBuffer msg = new StringBuffer (); 147 if (ourMsg != null) { 148 msg.append(ourMsg); 149 } 150 if (getCause() != null) { 151 String causeMsg = getCause().getMessage(); 152 if (causeMsg != null) { 153 if (ourMsg != null) { 154 msg.append("\n\t-> "); 155 } 156 msg.append(causeMsg); 157 } 158 } 159 message = msg.toString(); 160 } 161 return message; 162 } 163 164 169 public void printStackTrace(PrintStream s) { 170 if( Util.javaVersion >= 1.4 ) super.printStackTrace(s); 171 else { 172 synchronized (s) { 173 s.println(this); 174 IVVector trace = getStackTrace(this); 175 for (int i=0; i < trace.size(); i++) 176 s.println(trace.elementAt(i)); 177 178 Throwable ourCause = getCause(); 179 if (ourCause != null) 180 printStackTraceAsCause(ourCause, s, trace); 181 } 182 } 183 } 184 185 188 private static void printStackTraceAsCause(Throwable t, PrintStream s, IVVector causedTrace) { 189 190 IVVector trace = getStackTrace(t); 192 int m = trace.size()-1, n = causedTrace.size()-1; 193 while (m >= 0 && n >=0 && trace.elementAt(m).equals(causedTrace.elementAt(n)) ) { 194 m--; n--; 195 } 196 int framesInCommon = trace.size() - 1 - m; 197 198 s.println("caused by: " + t); 199 for (int i=0; i <= m; i++) 200 s.println(trace.elementAt(i)); 201 if (framesInCommon != 0) 202 s.println("\t... " + framesInCommon + " more"); 203 204 if( t instanceof IVException ) { 206 Throwable ourCause = ((IVException)t).getCause(); 207 if (ourCause != null) 208 printStackTraceAsCause(ourCause, s, trace); 209 } 210 } 211 212 219 public void printStackTrace(PrintWriter s) { 220 if( Util.javaVersion >= 1.4 ) super.printStackTrace(s); 221 else { 222 synchronized (s) { 223 s.println(this); 224 IVVector trace = getStackTrace(this); 225 for (int i=0; i < trace.size(); i++) 226 s.println(trace.elementAt(i)); 227 228 Throwable ourCause = getCause(); 229 if (ourCause != null) 230 printStackTraceAsCause(ourCause, s, trace); 231 } 232 } 233 } 234 235 238 private static void printStackTraceAsCause(Throwable t, PrintWriter s, IVVector causedTrace) { 239 240 IVVector trace = getStackTrace(t); 242 int m = trace.size()-1, n = causedTrace.size()-1; 243 while (m >= 0 && n >=0 && trace.elementAt(m).equals(causedTrace.elementAt(n)) ) { 244 m--; n--; 245 } 246 int framesInCommon = trace.size() - 1 - m; 247 248 s.println("caused by: " + t); 249 for (int i=0; i <= m; i++) 250 s.println(trace.elementAt(i)); 251 if (framesInCommon != 0) 252 s.println("\t... " + framesInCommon + " more"); 253 254 if( t instanceof IVException ) { 256 Throwable ourCause = ((IVException)t).getCause(); 257 if (ourCause != null) 258 printStackTraceAsCause(ourCause, s, trace); 259 } 260 } 261 262 private void super_printStackTrace( PrintWriter pw ) { 263 super.printStackTrace(pw); 264 } 265 266 private static IVVector getStackTrace( Throwable t ) { 267 StringWriter sw = new StringWriter(); 268 PrintWriter pw = new PrintWriter(sw, true); 269 270 if( t instanceof IVException ) { 271 ((IVException)t).super_printStackTrace(pw); 272 } else { 273 t.printStackTrace(pw); 274 } 275 276 StringTokenizer st = new StringTokenizer(sw.getBuffer().toString(), Util.lineSeparator); 277 IVVector v = new IVVector(20); 278 while (st.hasMoreTokens()) { 279 String s = st.nextToken(); 280 if( s.startsWith("\tat") ) 281 v.addElement(s); 282 } 283 return v; 284 } 285 286 } 287 | Popular Tags |