|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| StringUtil.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* ========================================================================
|
|
| 3 |
*
|
|
| 4 |
* Copyright 2001-2005 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;
|
|
| 23 |
import java.io.IOException;
|
|
| 24 |
import java.io.PrintWriter;
|
|
| 25 |
import java.io.StringReader;
|
|
| 26 |
import java.io.StringWriter;
|
|
| 27 |
|
|
| 28 |
/**
|
|
| 29 |
* Various utility methods for string manipulation.
|
|
| 30 |
*
|
|
| 31 |
* @version $Id: StringUtil.java 239169 2005-05-05 09:21:54Z vmassol $
|
|
| 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 | 0 |
public static String exceptionToString(Throwable theThrowable) |
| 43 |
{
|
|
| 44 | 0 |
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 | 0 |
public static String exceptionToString(Throwable theThrowable, |
| 58 |
String[] theFilterPatterns) |
|
| 59 |
{
|
|
| 60 | 0 |
StringWriter sw = new StringWriter();
|
| 61 | 0 |
PrintWriter pw = new PrintWriter(sw);
|
| 62 |
|
|
| 63 | 0 |
theThrowable.printStackTrace(pw); |
| 64 | 0 |
String stackTrace = sw.toString(); |
| 65 | 0 |
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 | 0 |
static String filterStackTrace(String theStackTrace,
|
| 76 |
String[] theFilterPatterns) |
|
| 77 |
{
|
|
| 78 | 0 |
if ((theFilterPatterns == null) || (theFilterPatterns.length == 0) |
| 79 |
|| (theStackTrace == null))
|
|
| 80 |
{
|
|
| 81 | 0 |
return theStackTrace;
|
| 82 |
} |
|
| 83 |
|
|
| 84 | 0 |
StringWriter stringWriter = new StringWriter();
|
| 85 | 0 |
PrintWriter printWriter = new PrintWriter(stringWriter);
|
| 86 | 0 |
StringReader stringReader = new StringReader(theStackTrace);
|
| 87 | 0 |
BufferedReader bufferedReader = new BufferedReader(stringReader);
|
| 88 |
|
|
| 89 | 0 |
String line; |
| 90 | 0 |
try
|
| 91 |
{
|
|
| 92 | 0 |
while ((line = bufferedReader.readLine()) != null) |
| 93 |
{
|
|
| 94 | 0 |
if (!filterLine(line, theFilterPatterns))
|
| 95 |
{
|
|
| 96 | 0 |
printWriter.println(line); |
| 97 |
} |
|
| 98 |
} |
|
| 99 |
} |
|
| 100 |
catch (IOException e)
|
|
| 101 |
{
|
|
| 102 | 0 |
return theStackTrace;
|
| 103 |
} |
|
| 104 | 0 |
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 | 0 |
public static boolean filterLine(String theLine, String[] theFilterPatterns) |
| 116 |
{
|
|
| 117 | 0 |
for (int i = 0; i < theFilterPatterns.length; i++) |
| 118 |
{
|
|
| 119 | 0 |
if (theLine.indexOf(theFilterPatterns[i]) > 0)
|
| 120 |
{
|
|
| 121 | 0 |
return true; |
| 122 |
} |
|
| 123 |
} |
|
| 124 | 0 |
return false; |
| 125 |
} |
|
| 126 |
|
|
| 127 |
/**
|
|
| 128 |
* Replaces a character in a string by a substring.
|
|
| 129 |
*
|
|
| 130 |
* @param theBaseString the base string in which to perform replacements
|
|
| 131 |
* @param theChar the char to look for
|
|
| 132 |
* @param theNewString the string with which to replace the char
|
|
| 133 |
* @return the string with replacements done or null if the input string
|
|
| 134 |
* was null
|
|
| 135 |
*/
|
|
| 136 | 0 |
public static String replace(String theBaseString, char theChar, |
| 137 |
String theNewString) |
|
| 138 |
{
|
|
| 139 | 0 |
if (theBaseString == null) |
| 140 |
{
|
|
| 141 | 0 |
return null; |
| 142 |
} |
|
| 143 |
|
|
| 144 | 0 |
int pos = theBaseString.indexOf(theChar);
|
| 145 | 0 |
if (pos < 0)
|
| 146 |
{
|
|
| 147 | 0 |
return theBaseString;
|
| 148 |
} |
|
| 149 |
|
|
| 150 | 0 |
int lastPos = 0;
|
| 151 | 0 |
StringBuffer result = new StringBuffer();
|
| 152 | 0 |
while (pos > -1)
|
| 153 |
{
|
|
| 154 | 0 |
result.append(theBaseString.substring(lastPos, pos)); |
| 155 | 0 |
result.append(theNewString); |
| 156 |
|
|
| 157 | 0 |
lastPos = pos + 1; |
| 158 | 0 |
pos = theBaseString.indexOf(theChar, lastPos); |
| 159 |
} |
|
| 160 |
|
|
| 161 | 0 |
if (lastPos < theBaseString.length())
|
| 162 |
{
|
|
| 163 | 0 |
result.append(theBaseString.substring(lastPos)); |
| 164 |
} |
|
| 165 |
|
|
| 166 | 0 |
return result.toString();
|
| 167 |
} |
|
| 168 |
} |
|
| 169 |
|
|
||||||||||