org.apache.maven.surefire.util.internal.StringUtils Maven / Gradle / Ivy
Show all versions of surefire-api Show documentation
package org.apache.maven.surefire.util.internal;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.util.StringTokenizer;
/**
*
* Common String
manipulation routines.
*
*
*
* Originally from Turbine and the GenerationJavaCore library.
*
*
* @author Jon S. Stevens
* @author Daniel Rall
* @author Greg Coladonato
* @author Henri Yandell
* @author Ed Korthof
* @author Rand McNeely
* @author Stephen Colebourne
* @author Fredrik Westermarck
* @author Holger Krauth
* @author Alexander Day Chaffee
* @author Vincent Siveton
* @version $Id: StringUtils.java 8001 2009-01-03 13:17:09Z vsiveton $
* @noinspection JavaDoc
*
* A quick borrow from plexus-utils by Kristian Rosenvold, to restore jdk1.3 compat Threw away all the
* unused stuff.
*
* NOTE: This class is not part of any api and is public purely for technical reasons !
* @since 1.0
*/
public final class StringUtils
{
public static final String NL = System.getProperty( "line.separator" );
private static final byte[] HEX_CHARS = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
private static final Charset DEFAULT_CHARSET = Charset.defaultCharset();
// 8-bit charset Latin-1
public static final String FORK_STREAM_CHARSET_NAME = "ISO-8859-1";
private StringUtils()
{
throw new IllegalStateException( "no instantiable constructor" );
}
public static String[] split( String text, String separator )
{
int max = -1;
StringTokenizer tok;
if ( separator == null )
{
// Null separator means we're using StringTokenizer's default
// delimiter, which comprises all whitespace characters.
tok = new StringTokenizer( text );
}
else
{
tok = new StringTokenizer( text, separator );
}
int listSize = tok.countTokens();
if ( max > 0 && listSize > max )
{
listSize = max;
}
String[] list = new String[listSize];
int i = 0;
int lastTokenBegin;
int lastTokenEnd = 0;
while ( tok.hasMoreTokens() )
{
if ( max > 0 && i == listSize - 1 )
{
// In the situation where we hit the max yet have
// tokens left over in our input, the last list
// element gets all remaining text.
String endToken = tok.nextToken();
lastTokenBegin = text.indexOf( endToken, lastTokenEnd );
list[i] = text.substring( lastTokenBegin );
break;
}
else
{
list[i] = tok.nextToken();
lastTokenBegin = text.indexOf( list[i], lastTokenEnd );
lastTokenEnd = lastTokenBegin + list[i].length();
}
i++;
}
return list;
}
/**
*
* Checks if a (trimmed) String is null
or blank.
*
*
* @param str the String to check
* @return true
if the String is null
, or length zero once trimmed
*/
public static boolean isBlank( String str )
{
return str == null || str.trim().length() == 0;
}
/**
*
* Checks if a (trimmed) String is not null
and not blank.
*
*
* @param str the String to check
* @return true
if the String is not null
and length of trimmed
* str
is not zero.
*/
public static boolean isNotBlank( String str )
{
return !isBlank( str );
}
/**
* Escape the specified string to a representation that only consists of nicely printable characters, without any
* newlines and without a comma.
*
* The reverse-method is {@link #unescapeString(StringBuilder, CharSequence)}.
*
* @param target target string buffer. The required space will be up to {@code str.getBytes().length * 5} chars.
* @param str String to escape values in, may be {@code null}.
*/
@SuppressWarnings( "checkstyle:magicnumber" )
public static void escapeToPrintable( StringBuilder target, CharSequence str )
{
if ( target == null )
{
throw new IllegalArgumentException( "The target buffer must not be null" );
}
if ( str == null )
{
return;
}
for ( int i = 0; i < str.length(); i++ )
{
char c = str.charAt( i );
// handle non-nicely printable chars and the comma
if ( c < 32 || c > 126 || c == '\\' || c == ',' )
{
target.append( '\\' );
target.append( (char) HEX_CHARS[( 0xF000 & c ) >> 12] );
target.append( (char) HEX_CHARS[( 0x0F00 & c ) >> 8] );
target.append( (char) HEX_CHARS[( 0x00F0 & c ) >> 4] );
target.append( (char) HEX_CHARS[( 0x000F & c )] );
}
else
{
target.append( c );
}
}
}
/**
* Reverses the effect of {@link #escapeToPrintable(StringBuilder, CharSequence)}.
*
* @param target target string buffer
* @param str the String to un-escape, as created by {@link #escapeToPrintable(StringBuilder, CharSequence)}
*/
public static void unescapeString( StringBuilder target, CharSequence str )
{
if ( target == null )
{
throw new IllegalArgumentException( "The target buffer must not be null" );
}
if ( str == null )
{
return;
}
for ( int i = 0; i < str.length(); i++ )
{
char ch = str.charAt( i );
if ( ch == '\\' )
{
target.append( (char) (
digit( str.charAt( ++i ) ) << 12
| digit( str.charAt( ++i ) ) << 8
| digit( str.charAt( ++i ) ) << 4
| digit( str.charAt( ++i ) )
) );
}
else
{
target.append( ch );
}
}
}
private static int digit( char ch )
{
if ( ch >= 'a' )
{
return 10 + ch - 'a';
}
else if ( ch >= 'A' )
{
return 10 + ch - 'A';
}
else
{
return ch - '0';
}
}
/**
* Escapes the bytes in the array {@code str} to contain only 'printable' bytes.
*
* Escaping is done by encoding the non-nicely printable bytes to {@code '\' + upperCaseHexBytes(byte)}.
*
* A save length of {@code out} is {@code len * 3 + outoff}.
*
* The reverse-method is {@link #unescapeBytes(byte[], String)}.
*
* @param out output buffer
* @param outoff offset in the output buffer
* @param input input buffer
* @param off offset in the input buffer
* @param len number of bytes to copy from the input buffer
* @return number of bytes written to {@code out}
*/
@SuppressWarnings( "checkstyle:magicnumber" )
public static int escapeBytesToPrintable( byte[] out, int outoff, byte[] input, int off, int len )
{
if ( out == null )
{
throw new IllegalArgumentException( "The output array must not be null" );
}
if ( input == null || input.length == 0 )
{
return 0;
}
int outputPos = outoff;
int end = off + len;
for ( int i = off; i < end; i++ )
{
byte b = input[i];
// handle non-nicely printable bytes
if ( b < 32 || b > 126 || b == '\\' || b == ',' )
{
int upper = ( 0xF0 & b ) >> 4;
int lower = ( 0x0F & b );
out[outputPos++] = '\\';
out[outputPos++] = HEX_CHARS[upper];
out[outputPos++] = HEX_CHARS[lower];
}
else
{
out[outputPos++] = b;
}
}
return outputPos - outoff;
}
/**
* Reverses the effect of {@link #escapeBytesToPrintable(byte[], int, byte[], int, int)}.
*
* @param str the input String
* @param charsetName the charset name
* @return the number of bytes written to {@code out}
*/
public static ByteBuffer unescapeBytes( String str, String charsetName )
{
int outPos = 0;
if ( str == null )
{
return ByteBuffer.wrap( new byte[0] );
}
byte[] out = new byte[str.length()];
for ( int i = 0; i < str.length(); i++ )
{
char ch = str.charAt( i );
if ( ch == '\\' )
{
int upper = digit( str.charAt( ++i ) );
int lower = digit( str.charAt( ++i ) );
out[outPos++] = (byte) ( upper << 4 | lower );
}
else
{
out[outPos++] = (byte) ch;
}
}
Charset sourceCharset = Charset.forName( charsetName );
if ( !DEFAULT_CHARSET.equals( sourceCharset ) )
{
CharBuffer decodedFromSourceCharset;
try
{
decodedFromSourceCharset = sourceCharset.newDecoder().decode( ByteBuffer.wrap( out, 0, outPos ) );
ByteBuffer defaultEncoded = DEFAULT_CHARSET.encode( decodedFromSourceCharset );
return defaultEncoded;
}
catch ( CharacterCodingException e )
{
// ignore and fall through to the non-recoded version
}
}
return ByteBuffer.wrap( out, 0, outPos );
}
public static byte[] encodeStringForForkCommunication( String string )
{
try
{
return string.getBytes( FORK_STREAM_CHARSET_NAME );
}
catch ( UnsupportedEncodingException e )
{
throw new RuntimeException( "The JVM must support Charset " + FORK_STREAM_CHARSET_NAME, e );
}
}
/**
*
* @param buffer Examined StringBuffer
* @param pattern a pattern which should start in buffer
* @return true if buffer's literal starts with given pattern
*/
public static boolean startsWith( StringBuffer buffer, String pattern )
{
if ( buffer.length() < pattern.length() )
{
return false;
}
else
{
for ( int i = 0, len = pattern.length(); i < len; i++ )
{
if ( buffer.charAt( i ) != pattern.charAt( i ) )
{
return false;
}
}
return true;
}
}
}