/Users/lyon/j4p/src/classUtils/pack/util/StackTraceLineSet.java

1    package classUtils.pack.util; 
2     
3    import java.io.BufferedReader; 
4    import java.io.ByteArrayOutputStream; 
5    import java.io.IOException; 
6    import java.io.PrintStream; 
7    import java.io.StringReader; 
8    import java.util.ArrayList; 
9    import java.util.List; 
10    
11   /** 
12    * A special lineset with services for exception stack traces. 
13    *  
14    * @author cris 
15    */ 
16   public class StackTraceLineSet extends LineSet { 
17        
18       /** 
19        * Constructor for StackTraceLineSet. 
20        */ 
21       public StackTraceLineSet(Throwable e) { 
22           this(extractTraceLines(e)); 
23       } 
24        
25       public StackTraceLineSet(String stackTrace) { 
26           this(extractTraceLines(stackTrace)); 
27       } 
28        
29       public StackTraceLineSet(String[] stackTraceLines) { 
30           for(int i=0;i<stackTraceLines.length;i++) 
31               add(stackTraceLines[i]); 
32       } 
33        
34       private static String[] extractTraceLines(Throwable e) { 
35           ByteArrayOutputStream os = new ByteArrayOutputStream(); 
36           PrintStream ps = new PrintStream(os); 
37           e.printStackTrace(ps); 
38           return extractTraceLines(os.toString()); 
39            
40       } 
41        
42       private static String[] extractTraceLines(String stackTrace) { 
43           BufferedReader br=new BufferedReader(new StringReader(stackTrace)); 
44           String line; 
45           List l=new ArrayList(); 
46           try {        
47               while((line=br.readLine())!=null) l.add(line); 
48           } catch(IOException e) { 
49               // Can't happen 
50           } 
51           String []result=new String[l.size()]; 
52           l.toArray(result); 
53           return result; 
54       } 
55        
56   } 
57