1 11 14 package org.eclipse.jdt.internal.junit.ui; 15 16 import java.io.BufferedReader ; 17 import java.io.IOException ; 18 import java.io.PrintWriter ; 19 import java.io.StringReader ; 20 import java.io.StringWriter ; 21 22 public class TextualTrace { 23 public static final int LINE_TYPE_EXCEPTION = 1; 24 25 public static final int LINE_TYPE_NORMAL = 0; 26 27 public static final int LINE_TYPE_STACKFRAME = 2; 28 29 private final String fTrace; 30 31 public TextualTrace(String trace, String [] filterPatterns) { 32 super(); 33 fTrace = filterStack(trace, filterPatterns); 34 } 35 36 public void display(ITraceDisplay display, int maxLabelLength) { 37 StringReader stringReader = new StringReader (fTrace); 38 BufferedReader bufferedReader = new BufferedReader (stringReader); 39 String line; 40 41 try { 42 line = readLine(bufferedReader); 44 if (line == null) 45 return; 46 47 displayWrappedLine(display, maxLabelLength, line, 48 LINE_TYPE_EXCEPTION); 49 50 while ((line = readLine(bufferedReader)) != null) { 52 int type = isAStackFrame(line) ? LINE_TYPE_STACKFRAME 53 : LINE_TYPE_NORMAL; 54 displayWrappedLine(display, maxLabelLength, line, type); 55 } 56 } catch (IOException e) { 57 display.addTraceLine(LINE_TYPE_NORMAL, fTrace); 58 } 59 } 60 61 private void displayWrappedLine(ITraceDisplay display, int maxLabelLength, 62 String line, int type) { 63 final int labelLength = line.length(); 64 if (labelLength < maxLabelLength) { 65 display.addTraceLine(type, line); 66 } else { 67 display.addTraceLine(type, line.substring(0, maxLabelLength)); 70 int offset = maxLabelLength; 71 while (offset < labelLength) { 72 int nextOffset = Math.min(labelLength, offset + maxLabelLength); 73 display.addTraceLine(LINE_TYPE_NORMAL, line.substring(offset, 74 nextOffset)); 75 offset = nextOffset; 76 } 77 } 78 } 79 80 private boolean filterLine(String [] patterns, String line) { 81 String pattern; 82 int len; 83 for (int i = (patterns.length - 1); i >= 0; --i) { 84 pattern = patterns[i]; 85 len = pattern.length() - 1; 86 if (pattern.charAt(len) == '*') { 87 pattern = pattern.substring(0, len); 89 } else if (Character.isUpperCase(pattern.charAt(0))) { 90 pattern = FailureTrace.FRAME_PREFIX + pattern + '.'; 92 } else { 93 final int lastDotIndex = pattern.lastIndexOf('.'); 95 if ((lastDotIndex != -1) 96 && (lastDotIndex != len) 97 && Character.isUpperCase(pattern.charAt(lastDotIndex + 1))) 98 pattern += '.'; } 100 101 if (line.indexOf(pattern) > 0) 102 return true; 103 } 104 return false; 105 } 106 107 private String filterStack(String stackTrace, String [] filterPatterns) { 108 if (filterPatterns.length == 0 || stackTrace == null) 109 return stackTrace; 110 111 StringWriter stringWriter = new StringWriter (); 112 PrintWriter printWriter = new PrintWriter (stringWriter); 113 StringReader stringReader = new StringReader (stackTrace); 114 BufferedReader bufferedReader = new BufferedReader (stringReader); 115 116 String line; 117 String [] patterns = filterPatterns; 118 try { 119 while ((line = bufferedReader.readLine()) != null) { 120 if (!filterLine(patterns, line)) 121 printWriter.println(line); 122 } 123 } catch (IOException e) { 124 return stackTrace; } 126 return stringWriter.toString(); 127 } 128 129 private boolean isAStackFrame(String itemLabel) { 130 return itemLabel.indexOf(" at ") >= 0; } 133 134 private String readLine(BufferedReader bufferedReader) throws IOException { 135 String readLine = bufferedReader.readLine(); 136 return readLine == null ? null : readLine.replace('\t', ' '); 137 } 138 } 139 | Popular Tags |