|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| StringUtil.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* ====================================================================
|
|
| 3 |
*
|
|
| 4 |
* The Apache Software License, Version 1.1
|
|
| 5 |
*
|
|
| 6 |
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
|
| 7 |
* reserved.
|
|
| 8 |
*
|
|
| 9 |
* Redistribution and use in source and binary forms, with or without
|
|
| 10 |
* modification, are permitted provided that the following conditions
|
|
| 11 |
* are met:
|
|
| 12 |
*
|
|
| 13 |
* 1. Redistributions of source code must retain the above copyright
|
|
| 14 |
* notice, this list of conditions and the following disclaimer.
|
|
| 15 |
*
|
|
| 16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
| 17 |
* notice, this list of conditions and the following disclaimer in
|
|
| 18 |
* the documentation and/or other materials provided with the
|
|
| 19 |
* distribution.
|
|
| 20 |
*
|
|
| 21 |
* 3. The end-user documentation included with the redistribution, if
|
|
| 22 |
* any, must include the following acknowlegement:
|
|
| 23 |
* "This product includes software developed by the
|
|
| 24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
| 25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
| 26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
| 27 |
*
|
|
| 28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
| 29 |
* Foundation" must not be used to endorse or promote products
|
|
| 30 |
* derived from this software without prior written permission. For
|
|
| 31 |
* written permission, please contact apache@apache.org.
|
|
| 32 |
*
|
|
| 33 |
* 5. Products derived from this software may not be called "Apache"
|
|
| 34 |
* nor may "Apache" appear in their names without prior written
|
|
| 35 |
* permission of the Apache Group.
|
|
| 36 |
*
|
|
| 37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
| 38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
| 39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
| 40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
| 41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
| 42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
| 43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
| 44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
| 45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
| 46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
| 47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
| 48 |
* SUCH DAMAGE.
|
|
| 49 |
* ====================================================================
|
|
| 50 |
*
|
|
| 51 |
* This software consists of voluntary contributions made by many
|
|
| 52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
| 53 |
* information on the Apache Software Foundation, please see
|
|
| 54 |
* <http://www.apache.org/>.
|
|
| 55 |
*
|
|
| 56 |
*/
|
|
| 57 |
package org.apache.cactus.util;
|
|
| 58 |
|
|
| 59 |
import java.io.BufferedReader;
|
|
| 60 |
import java.io.IOException;
|
|
| 61 |
import java.io.PrintWriter;
|
|
| 62 |
import java.io.StringReader;
|
|
| 63 |
import java.io.StringWriter;
|
|
| 64 |
|
|
| 65 |
/**
|
|
| 66 |
* Various utility methods for string manipulation.
|
|
| 67 |
*
|
|
| 68 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
| 69 |
* @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
|
|
| 70 |
*
|
|
| 71 |
* @version $Id: StringUtil.java,v 1.5 2003/01/03 15:35:46 vmassol Exp $
|
|
| 72 |
*/
|
|
| 73 |
public class StringUtil |
|
| 74 |
{
|
|
| 75 |
/**
|
|
| 76 |
* Returns the stack trace of an exception as String.
|
|
| 77 |
*
|
|
| 78 |
* @param theThrowable the exception from which to extract the stack trace
|
|
| 79 |
* as a String
|
|
| 80 |
* @return the exception stack trace as a String
|
|
| 81 |
*/
|
|
| 82 | 0 |
public static String exceptionToString(Throwable theThrowable) |
| 83 |
{
|
|
| 84 | 0 |
return exceptionToString(theThrowable, null); |
| 85 |
} |
|
| 86 |
|
|
| 87 |
/**
|
|
| 88 |
* Returns the stack trace of an exception as String, optionally filtering
|
|
| 89 |
* out line from the stack trac
|
|
| 90 |
*
|
|
| 91 |
* @param theThrowable the exception from which to extract the stack trace
|
|
| 92 |
* as a String
|
|
| 93 |
* @param theFilterPatterns Array containing a list of patterns to filter
|
|
| 94 |
* out from the stack trace
|
|
| 95 |
* @return the exception stack trace as a String
|
|
| 96 |
*/
|
|
| 97 | 0 |
public static String exceptionToString(Throwable theThrowable, |
| 98 |
String[] theFilterPatterns) |
|
| 99 |
{
|
|
| 100 | 0 |
StringWriter sw = new StringWriter();
|
| 101 | 0 |
PrintWriter pw = new PrintWriter(sw);
|
| 102 |
|
|
| 103 | 0 |
theThrowable.printStackTrace(pw); |
| 104 | 0 |
String stackTrace = sw.toString(); |
| 105 | 0 |
return filterStackTrace(stackTrace, theFilterPatterns);
|
| 106 |
} |
|
| 107 |
|
|
| 108 |
/**
|
|
| 109 |
*
|
|
| 110 |
*
|
|
| 111 |
* @param theStackTrace The original, unfiltered stack trace
|
|
| 112 |
* @param theFilterPatterns The patterns to filter out
|
|
| 113 |
* @return The filtered stack trace
|
|
| 114 |
*/
|
|
| 115 | 0 |
static String filterStackTrace(String theStackTrace,
|
| 116 |
String[] theFilterPatterns) |
|
| 117 |
{
|
|
| 118 | 0 |
if ((theFilterPatterns == null) || (theFilterPatterns.length == 0) |
| 119 |
|| (theStackTrace == null))
|
|
| 120 |
{
|
|
| 121 | 0 |
return theStackTrace;
|
| 122 |
} |
|
| 123 |
|
|
| 124 | 0 |
StringWriter stringWriter = new StringWriter();
|
| 125 | 0 |
PrintWriter printWriter = new PrintWriter(stringWriter);
|
| 126 | 0 |
StringReader stringReader = new StringReader(theStackTrace);
|
| 127 | 0 |
BufferedReader bufferedReader = new BufferedReader(stringReader);
|
| 128 |
|
|
| 129 | 0 |
String line; |
| 130 | 0 |
try
|
| 131 |
{
|
|
| 132 | 0 |
while ((line = bufferedReader.readLine()) != null) |
| 133 |
{
|
|
| 134 | 0 |
if (!filterLine(line, theFilterPatterns))
|
| 135 |
{
|
|
| 136 | 0 |
printWriter.println(line); |
| 137 |
} |
|
| 138 |
} |
|
| 139 |
} |
|
| 140 |
catch (IOException e)
|
|
| 141 |
{
|
|
| 142 | 0 |
return theStackTrace;
|
| 143 |
} |
|
| 144 | 0 |
return stringWriter.toString();
|
| 145 |
} |
|
| 146 |
|
|
| 147 |
/**
|
|
| 148 |
*
|
|
| 149 |
*
|
|
| 150 |
* @param theLine The line to check
|
|
| 151 |
* @param theFilterPatterns The patterns to filter out
|
|
| 152 |
* @return boolean Whether the specified line should be filtered from the
|
|
| 153 |
* stack trace
|
|
| 154 |
*/
|
|
| 155 | 0 |
static boolean filterLine(String theLine, String[] theFilterPatterns) |
| 156 |
{
|
|
| 157 | 0 |
for (int i = 0; i < theFilterPatterns.length; i++) |
| 158 |
{
|
|
| 159 | 0 |
if (theLine.indexOf(theFilterPatterns[i]) > 0)
|
| 160 |
{
|
|
| 161 | 0 |
return true; |
| 162 |
} |
|
| 163 |
} |
|
| 164 | 0 |
return false; |
| 165 |
} |
|
| 166 |
|
|
| 167 |
} |
|
| 168 |
|
|
||||||||||