1 52 53 package freemarker.template; 54 55 import java.io.OutputStreamWriter ; 56 import java.io.PrintWriter ; 57 import java.io.StringWriter ; 58 import java.lang.reflect.Method ; 59 60 import freemarker.core.Environment; 61 62 68 public class TemplateException extends Exception { 69 70 private static final boolean BEFORE_1_4 = before14(); 71 private static boolean before14() { 72 Class ec = Exception .class; 73 try { 74 ec.getMethod("getCause", new Class []{}); 75 } catch (Throwable e) { 76 return true; 77 } 78 return false; 79 } 80 81 private static final Class [] EMPTY_CLASS_ARRAY = new Class []{}; 82 83 private static final Object [] EMPTY_OBJECT_ARRAY = new Object []{}; 84 85 86 private final Exception causeException; 87 private final Environment env; 88 private final String ftlInstructionStack; 89 90 91 95 public TemplateException(Environment env) { 96 this(null, null, env); 97 } 98 99 105 public TemplateException(String description, Environment env) { 106 this(description, null, env); 107 } 108 109 116 public TemplateException(Exception cause, Environment env) { 117 this(null, cause, env); 118 } 119 120 129 public TemplateException(String description, Exception cause, Environment env) { 130 super(getDescription(description, cause)); 131 causeException = cause; 132 this.env = env; 133 if(env != null) 134 { 135 StringWriter sw = new StringWriter (); 136 PrintWriter pw = new PrintWriter (sw); 137 env.outputInstructionStack(pw); 138 pw.flush(); 139 ftlInstructionStack = sw.toString(); 140 } 141 else 142 { 143 ftlInstructionStack = ""; 144 } 145 } 146 147 private static String getDescription(String description, Exception cause) { 148 if(description != null) { 149 return description; 150 } 151 if(cause != null) { 152 return cause.getClass().getName() + ": " + cause.getMessage(); 153 } 154 return "No error message"; 155 } 156 157 168 public Exception getCauseException() { 169 return causeException; 170 } 171 172 180 public Throwable getCause() { 181 return causeException; 182 } 183 184 187 public String getFTLInstructionStack() { 188 return ftlInstructionStack; 189 } 190 191 194 public Environment getEnvironment() { 195 return env; 196 } 197 198 public void printStackTrace(java.io.PrintStream ps) { 199 PrintWriter pw = new PrintWriter (new OutputStreamWriter (ps), true); 200 printStackTrace(pw); 201 pw.flush(); 202 } 203 204 public void printStackTrace(PrintWriter pw) { 205 pw.println(); 206 pw.println(getMessage()); 207 if (ftlInstructionStack != null && ftlInstructionStack.length() != 0) { 208 pw.println("The problematic instruction:"); 209 pw.println(ftlInstructionStack); 210 } 211 pw.println("Java backtrace for programmers:"); 212 pw.println("----------"); 213 super.printStackTrace(pw); 214 if (BEFORE_1_4 && causeException != null) { 215 pw.println("Underlying cause: "); 216 causeException.printStackTrace(pw); 217 } 218 219 try { 223 Method m = causeException.getClass().getMethod("getRootCause", EMPTY_CLASS_ARRAY); 225 Throwable rootCause = (Throwable ) m.invoke(causeException, EMPTY_OBJECT_ARRAY); 226 if (rootCause != null) { 227 Throwable j14Cause = null; 228 if (!BEFORE_1_4) { 229 m = causeException.getClass().getMethod("getCause", EMPTY_CLASS_ARRAY); 230 j14Cause = (Throwable ) m.invoke(causeException, EMPTY_OBJECT_ARRAY); 231 } 232 if (j14Cause == null) { 233 pw.println("ServletException root cause: "); 234 rootCause.printStackTrace(pw); 235 } 236 } 237 } catch (Throwable exc) { 238 ; } 240 } 241 242 } 243 | Popular Tags |