1 21 22 package org.apache.derby.iapi.services.context; 23 24 import org.apache.derby.iapi.error.StandardException; 25 import org.apache.derby.iapi.services.stream.PrintWriterGetHeader; 26 27 import java.io.StringWriter ; 28 import java.io.PrintWriter ; 29 30 36 public class ErrorStringBuilder 37 { 38 private StringWriter stringWriter; 39 private PrintWriter printWriter; 40 private PrintWriterGetHeader headerGetter; 41 42 45 public ErrorStringBuilder(PrintWriterGetHeader headerGetter) 46 { 47 this.headerGetter = headerGetter; 48 this.stringWriter = new StringWriter (); 49 this.printWriter = new PrintWriter (stringWriter); 50 } 51 52 57 public void append(String s) 58 { 59 if (headerGetter != null) 60 printWriter.print(headerGetter.getHeader()); 61 printWriter.print(s); 62 } 63 64 65 70 public void appendln(String s) 71 { 72 if (headerGetter != null) 73 printWriter.print(headerGetter.getHeader()); 74 printWriter.println(s); 75 } 76 77 83 public void stackTrace(Throwable t) 84 { 85 int level = 0; 86 while(t != null) 87 { 88 if (level > 0) 89 printWriter.println("============= begin nested exception, level (" + 90 level + ") ==========="); 91 92 t.printStackTrace(printWriter); 93 94 95 if (t instanceof StandardException) { 96 t = ((StandardException)t).getNestedException(); 97 } 98 else if (t instanceof ExceptionInInitializerError ) { 99 t = ((ExceptionInInitializerError ) t).getException(); 100 } 101 else if (t instanceof java.lang.reflect.InvocationTargetException ) { 102 t = ((java.lang.reflect.InvocationTargetException ) t).getTargetException(); 103 } 104 else if (t instanceof java.sql.SQLException ) { 105 t = ((java.sql.SQLException )t).getNextException(); 106 } else { 107 t = null; 108 } 109 110 if (level > 0) 111 printWriter.println("============= end nested exception, level (" + 112 level + ") ==========="); 113 114 level++; 115 116 } 117 118 } 119 120 124 public void reset() 125 { 126 stringWriter.getBuffer().setLength(0); 128 } 129 130 133 public StringBuffer get() 134 { 135 return stringWriter.getBuffer(); 136 } 137 } 138 | Popular Tags |