1 20 package org.apache.cactus.internal.util; 21 22 import java.io.BufferedReader ; 23 import java.io.IOException ; 24 import java.io.PrintWriter ; 25 import java.io.StringReader ; 26 import java.io.StringWriter ; 27 28 33 public class StringUtil 34 { 35 42 public static String exceptionToString(Throwable theThrowable) 43 { 44 return exceptionToString(theThrowable, null); 45 } 46 47 57 public static String exceptionToString(Throwable theThrowable, 58 String [] theFilterPatterns) 59 { 60 StringWriter sw = new StringWriter (); 61 PrintWriter pw = new PrintWriter (sw); 62 63 theThrowable.printStackTrace(pw); 64 String stackTrace = sw.toString(); 65 return filterStackTrace(stackTrace, theFilterPatterns); 66 } 67 68 75 static String filterStackTrace(String theStackTrace, 76 String [] theFilterPatterns) 77 { 78 if ((theFilterPatterns == null) || (theFilterPatterns.length == 0) 79 || (theStackTrace == null)) 80 { 81 return theStackTrace; 82 } 83 84 StringWriter stringWriter = new StringWriter (); 85 PrintWriter printWriter = new PrintWriter (stringWriter); 86 StringReader stringReader = new StringReader (theStackTrace); 87 BufferedReader bufferedReader = new BufferedReader (stringReader); 88 89 String line; 90 try 91 { 92 while ((line = bufferedReader.readLine()) != null) 93 { 94 if (!filterLine(line, theFilterPatterns)) 95 { 96 printWriter.println(line); 97 } 98 } 99 } 100 catch (IOException e) 101 { 102 return theStackTrace; 103 } 104 return stringWriter.toString(); 105 } 106 107 115 public static boolean filterLine(String theLine, String [] theFilterPatterns) 116 { 117 for (int i = 0; i < theFilterPatterns.length; i++) 118 { 119 if (theLine.indexOf(theFilterPatterns[i]) > 0) 120 { 121 return true; 122 } 123 } 124 return false; 125 } 126 127 } 128 | Popular Tags |