2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

1   /* 
2    * ========================================================================
3    * 
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   * 
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * 
19   * ========================================================================
20   */
21  package org.apache.cactus.internal.util;
22  
23  import java.io.BufferedReader;
24  import java.io.IOException;
25  import java.io.PrintWriter;
26  import java.io.StringReader;
27  import java.io.StringWriter;
28  
29  /**
30   * Various utility methods for string manipulation.
31   *
32   * @version $Id: StringUtil.java 239169 2005-05-05 09:21:54Z vmassol $
33   */
34  public class StringUtil
35  {
36      /**
37       * Returns the stack trace of an exception as String.
38       * 
39       * @param theThrowable the exception from which to extract the stack trace
40       *        as a String
41       * @return the exception stack trace as a String
42       */
43      public static String exceptionToString(Throwable theThrowable)
44      {
45          return exceptionToString(theThrowable, null);
46      }
47  
48      /**
49       * Returns the stack trace of an exception as String, optionally filtering
50       * out line from the stack trac.
51       * 
52       * @param theThrowable the exception from which to extract the stack trace
53       *        as a String
54       * @param theFilterPatterns Array containing a list of patterns to filter 
55       *        out from the stack trace
56       * @return the exception stack trace as a String
57       */
58      public static String exceptionToString(Throwable theThrowable,
59                                             String[] theFilterPatterns)
60      {
61          StringWriter sw = new StringWriter();
62          PrintWriter pw = new PrintWriter(sw);
63  
64          theThrowable.printStackTrace(pw);
65          String stackTrace = sw.toString();
66          return filterStackTrace(stackTrace, theFilterPatterns);
67      }
68  
69      /**
70       * 
71       * 
72       * @param theStackTrace The original, unfiltered stack trace
73       * @param theFilterPatterns The patterns to filter out
74       * @return The filtered stack trace
75       */
76      static String filterStackTrace(String theStackTrace,
77                                     String[] theFilterPatterns)
78      { 
79          if ((theFilterPatterns == null) || (theFilterPatterns.length == 0) 
80              || (theStackTrace == null)) 
81          {
82              return theStackTrace;
83          }
84  
85          StringWriter stringWriter = new StringWriter();
86          PrintWriter printWriter = new PrintWriter(stringWriter);
87          StringReader stringReader = new StringReader(theStackTrace);
88          BufferedReader bufferedReader = new BufferedReader(stringReader);    
89  
90          String line;
91          try
92          {   
93              while ((line = bufferedReader.readLine()) != null)
94              {
95                  if (!filterLine(line, theFilterPatterns))
96                  {
97                      printWriter.println(line);
98                  }
99              }
100         }
101         catch (IOException e)
102         {
103             return theStackTrace;
104         }
105         return stringWriter.toString();
106     }
107 
108     /**
109      * 
110      * 
111      * @param theLine The line to check
112      * @param theFilterPatterns The patterns to filter out
113      * @return boolean Whether the specified line should be filtered from the
114      *         stack trace
115      */
116     public static boolean filterLine(String theLine, String[] theFilterPatterns)
117     {
118         for (int i = 0; i < theFilterPatterns.length; i++)
119         {
120             if (theLine.indexOf(theFilterPatterns[i]) > 0)
121             {
122                 return true;
123             }
124         }
125         return false;
126     }
127 
128     /**
129      * Replaces a character in a string by a substring.
130      *
131      * @param theBaseString the base string in which to perform replacements
132      * @param theChar the char to look for
133      * @param theNewString the string with which to replace the char
134      * @return the string with replacements done or null if the input string
135      *          was null
136      */
137     public static String replace(String theBaseString, char theChar, 
138         String theNewString)
139     {
140         if (theBaseString == null)
141         {
142             return null;
143         }
144 
145         int pos = theBaseString.indexOf(theChar);
146         if (pos < 0)
147         {
148             return theBaseString;
149         }
150         
151         int lastPos = 0;
152         StringBuffer result = new StringBuffer();
153         while (pos > -1)
154         {
155             result.append(theBaseString.substring(lastPos, pos));
156             result.append(theNewString);
157             
158             lastPos = pos + 1;
159             pos = theBaseString.indexOf(theChar, lastPos);
160         }
161 
162         if (lastPos < theBaseString.length())
163         {
164             result.append(theBaseString.substring(lastPos));
165         }
166         
167         return result.toString();
168     }
169 }