KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > util > StringUtil


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.internal.util;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.io.StringReader JavaDoc;
26 import java.io.StringWriter JavaDoc;
27
28 /**
29  * Various utility methods for string manipulation.
30  *
31  * @version $Id: StringUtil.java,v 1.1 2004/05/22 11:34:48 vmassol Exp $
32  */

33 public class StringUtil
34 {
35     /**
36      * Returns the stack trace of an exception as String.
37      *
38      * @param theThrowable the exception from which to extract the stack trace
39      * as a String
40      * @return the exception stack trace as a String
41      */

42     public static String JavaDoc exceptionToString(Throwable JavaDoc theThrowable)
43     {
44         return exceptionToString(theThrowable, null);
45     }
46
47     /**
48      * Returns the stack trace of an exception as String, optionally filtering
49      * out line from the stack trac
50      *
51      * @param theThrowable the exception from which to extract the stack trace
52      * as a String
53      * @param theFilterPatterns Array containing a list of patterns to filter
54      * out from the stack trace
55      * @return the exception stack trace as a String
56      */

57     public static String JavaDoc exceptionToString(Throwable JavaDoc theThrowable,
58                                            String JavaDoc[] theFilterPatterns)
59     {
60         StringWriter JavaDoc sw = new StringWriter JavaDoc();
61         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
62
63         theThrowable.printStackTrace(pw);
64         String JavaDoc stackTrace = sw.toString();
65         return filterStackTrace(stackTrace, theFilterPatterns);
66     }
67
68     /**
69      *
70      *
71      * @param theStackTrace The original, unfiltered stack trace
72      * @param theFilterPatterns The patterns to filter out
73      * @return The filtered stack trace
74      */

75     static String JavaDoc filterStackTrace(String JavaDoc theStackTrace,
76                                    String JavaDoc[] theFilterPatterns)
77     {
78         if ((theFilterPatterns == null) || (theFilterPatterns.length == 0)
79             || (theStackTrace == null))
80         {
81             return theStackTrace;
82         }
83
84         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
85         PrintWriter JavaDoc printWriter = new PrintWriter JavaDoc(stringWriter);
86         StringReader JavaDoc stringReader = new StringReader JavaDoc(theStackTrace);
87         BufferedReader JavaDoc bufferedReader = new BufferedReader JavaDoc(stringReader);
88
89         String JavaDoc 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 JavaDoc e)
101         {
102             return theStackTrace;
103         }
104         return stringWriter.toString();
105     }
106
107     /**
108      *
109      *
110      * @param theLine The line to check
111      * @param theFilterPatterns The patterns to filter out
112      * @return boolean Whether the specified line should be filtered from the
113      * stack trace
114      */

115     public static boolean filterLine(String JavaDoc theLine, String JavaDoc[] 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