001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.internal.util;
021:
022: import java.io.BufferedReader;
023: import java.io.IOException;
024: import java.io.PrintWriter;
025: import java.io.StringReader;
026: import java.io.StringWriter;
027:
028: /**
029: * Various utility methods for string manipulation.
030: *
031: * @version $Id: StringUtil.java 239169 2005-05-05 09:21:54Z vmassol $
032: */
033: public class StringUtil {
034: /**
035: * Returns the stack trace of an exception as String.
036: *
037: * @param theThrowable the exception from which to extract the stack trace
038: * as a String
039: * @return the exception stack trace as a String
040: */
041: public static String exceptionToString(Throwable theThrowable) {
042: return exceptionToString(theThrowable, null);
043: }
044:
045: /**
046: * Returns the stack trace of an exception as String, optionally filtering
047: * out line from the stack trac
048: *
049: * @param theThrowable the exception from which to extract the stack trace
050: * as a String
051: * @param theFilterPatterns Array containing a list of patterns to filter
052: * out from the stack trace
053: * @return the exception stack trace as a String
054: */
055: public static String exceptionToString(Throwable theThrowable,
056: String[] theFilterPatterns) {
057: StringWriter sw = new StringWriter();
058: PrintWriter pw = new PrintWriter(sw);
059:
060: theThrowable.printStackTrace(pw);
061: String stackTrace = sw.toString();
062: return filterStackTrace(stackTrace, theFilterPatterns);
063: }
064:
065: /**
066: *
067: *
068: * @param theStackTrace The original, unfiltered stack trace
069: * @param theFilterPatterns The patterns to filter out
070: * @return The filtered stack trace
071: */
072: static String filterStackTrace(String theStackTrace,
073: String[] theFilterPatterns) {
074: if ((theFilterPatterns == null)
075: || (theFilterPatterns.length == 0)
076: || (theStackTrace == null)) {
077: return theStackTrace;
078: }
079:
080: StringWriter stringWriter = new StringWriter();
081: PrintWriter printWriter = new PrintWriter(stringWriter);
082: StringReader stringReader = new StringReader(theStackTrace);
083: BufferedReader bufferedReader = new BufferedReader(stringReader);
084:
085: String line;
086: try {
087: while ((line = bufferedReader.readLine()) != null) {
088: if (!filterLine(line, theFilterPatterns)) {
089: printWriter.println(line);
090: }
091: }
092: } catch (IOException e) {
093: return theStackTrace;
094: }
095: return stringWriter.toString();
096: }
097:
098: /**
099: *
100: *
101: * @param theLine The line to check
102: * @param theFilterPatterns The patterns to filter out
103: * @return boolean Whether the specified line should be filtered from the
104: * stack trace
105: */
106: public static boolean filterLine(String theLine,
107: String[] theFilterPatterns) {
108: for (int i = 0; i < theFilterPatterns.length; i++) {
109: if (theLine.indexOf(theFilterPatterns[i]) > 0) {
110: return true;
111: }
112: }
113: return false;
114: }
115:
116: /**
117: * Replaces a character in a string by a substring.
118: *
119: * @param theBaseString the base string in which to perform replacements
120: * @param theChar the char to look for
121: * @param theNewString the string with which to replace the char
122: * @return the string with replacements done or null if the input string
123: * was null
124: */
125: public static String replace(String theBaseString, char theChar,
126: String theNewString) {
127: if (theBaseString == null) {
128: return null;
129: }
130:
131: int pos = theBaseString.indexOf(theChar);
132: if (pos < 0) {
133: return theBaseString;
134: }
135:
136: int lastPos = 0;
137: StringBuffer result = new StringBuffer();
138: while (pos > -1) {
139: result.append(theBaseString.substring(lastPos, pos));
140: result.append(theNewString);
141:
142: lastPos = pos + 1;
143: pos = theBaseString.indexOf(theChar, lastPos);
144: }
145:
146: if (lastPos < theBaseString.length()) {
147: result.append(theBaseString.substring(lastPos));
148: }
149:
150: return result.toString();
151: }
152: }
|