1
package org.apache.maven.surefire.util.internal;
4
* Licensed to the Apache Software Foundation (ASF) under one
5
* or more contributor license agreements. See the NOTICE file
6
* distributed with this work for additional information
7
* regarding copyright ownership. The ASF licenses this file
8
* to you under the Apache License, Version 2.0 (the
9
* "License"); you may not use this file except in compliance
10
* with the License. You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing,
15
* software distributed under the License is distributed on an
16
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17
* KIND, either express or implied. See the License for the
18
* specific language governing permissions and limitations
22
import java.io.IOException;
23
import java.io.PrintStream;
24
import java.io.StringWriter;
25
import java.io.Writer;
26
import java.util.StringTokenizer;
27
import org.apache.maven.surefire.util.NestedRuntimeException;
30
* <p>Common <code>String</code> manipulation routines.</p>
33
* <a href="http://jakarta.apache.org/turbine/">Turbine</a> and the
34
* GenerationJavaCore library.</p>
36
* @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
37
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
38
* @author <a href="mailto:gcoladonato@yahoo.com">Greg Coladonato</a>
39
* @author <a href="mailto:bayard@generationjava.com">Henri Yandell</a>
40
* @author <a href="mailto:ed@codehaus.org">Ed Korthof</a>
41
* @author <a href="mailto:rand_mcneely@yahoo.com">Rand McNeely</a>
42
* @author Stephen Colebourne
43
* @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
44
* @author Holger Krauth
45
* @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
46
* @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
47
* @version $Id: StringUtils.java 8001 2009-01-03 13:17:09Z vsiveton $
48
* @noinspection JavaDoc
50
* A quick borrow from plexus-utils by Kristian Rosenvold, to restore jdk1.3 compat
51
* Threw away all the unused stuff.
53
* NOTE: This class is not part of any api and is public purely for technical reasons !
56
public class StringUtils
60
//--------------------------------------------------------------------------
62
public static String[] split( String text, String separator )
66
if ( separator == null )
68
// Null separator means we're using StringTokenizer's default
69
// delimiter, which comprises all whitespace characters.
70
tok = new StringTokenizer( text );
74
tok = new StringTokenizer( text, separator );
77
int listSize = tok.countTokens();
78
if ( ( max > 0 ) && ( listSize > max ) )
83
String[] list = new String[listSize];
87
while ( tok.hasMoreTokens() )
89
if ( ( max > 0 ) && ( i == listSize - 1 ) )
91
// In the situation where we hit the max yet have
92
// tokens left over in our input, the last list
93
// element gets all remaining text.
94
String endToken = tok.nextToken();
95
lastTokenBegin = text.indexOf( endToken, lastTokenEnd );
96
list[i] = text.substring( lastTokenBegin );
101
list[i] = tok.nextToken();
102
lastTokenBegin = text.indexOf( list[i], lastTokenEnd );
103
lastTokenEnd = lastTokenBegin + list[i].length();
112
* <p>Checks if a (trimmed) String is <code>null</code> or blank.</p>
114
* @param str the String to check
115
* @return <code>true</code> if the String is <code>null</code>, or
116
* length zero once trimmed
118
public static boolean isBlank( String str )
120
return ( ( str == null ) || ( str.trim().length() == 0 ) );
124
// Ripped from commons-lang StringEscapeUtils. Maybe Use dependency instead
125
public static void unescapeJava( StringWriter out, String str )
129
throw new IllegalArgumentException( "The Writer must not be null" );
135
int sz = str.length();
136
StringBuffer unicode = new StringBuffer( 4 );
137
boolean hadSlash = false;
138
boolean inUnicode = false;
139
for ( int i = 0; i < sz; i++ )
141
char ch = str.charAt( i );
144
// if in unicode, then we're reading unicode
146
unicode.append( ch );
147
if ( unicode.length() == 4 )
149
// unicode now contains the four hex digits
150
// which represents our unicode character
153
int value = Integer.parseInt( unicode.toString(), 16 );
154
out.write( (char) value );
155
unicode.setLength( 0 );
159
catch ( NumberFormatException nfe )
161
throw new NestedRuntimeException( "Unable to parse unicode value: " + unicode, nfe );
168
// handle an escaped value
198
// uh-oh, we're in unicode country....
208
else if ( ch == '\\' )
217
// then we're in the weird case of a \ at the end of the
218
// string, let's output it anyway.
223
// Ripped from commons-lang StringEscapeUtils. Maybe Use dependency instead
224
public static int unescapeJava( byte[] out, String str )
229
throw new IllegalArgumentException( "The Writer must not be null" );
235
int sz = str.length();
236
StringBuffer unicode = new StringBuffer( 4 );
237
boolean hadSlash = false;
238
boolean inUnicode = false;
239
for ( int i = 0; i < sz; i++ )
241
char ch = str.charAt( i );
244
// if in unicode, then we're reading unicode
246
unicode.append( ch );
247
if ( unicode.length() == 4 )
249
// unicode now contains the four hex digits
250
// which represents our unicode character
253
int value = Integer.parseInt( unicode.toString(), 16 );
254
out[outPos++] = (byte) value;
255
unicode.setLength( 0 );
259
catch ( NumberFormatException nfe )
261
throw new NestedRuntimeException( "Unable to parse unicode value: " + unicode, nfe );
268
// handle an escaped value
273
out[outPos++] = '\\';
276
out[outPos++] = '\'';
282
out[outPos++] = '\r';
285
out[outPos++] = '\f';
288
out[outPos++] = '\t';
291
out[outPos++] = '\n';
294
out[outPos++] = '\b';
298
// uh-oh, we're in unicode country....
303
out[outPos++] = (byte) ch;
308
else if ( ch == '\\' )
313
out[outPos++] = (byte) ch;
317
// then we're in the weird case of a \ at the end of the
318
// string, let's output it anyway.
319
out[outPos++] = '\\';
324
// Ripped from commons-lang StringEscapeUtils. With a minor modification, we unicode-quote commas
325
// to avoid csv decoding problems ;)
328
* @param out write to receieve the escaped string
329
* @param str String to escape values in, may be null
330
* @param escapeSingleQuote escapes single quotes if <code>true</code>
331
* @throws java.io.IOException if an IOException occurs
333
public static void escapeJavaStyleString( Writer out, String str, boolean escapeSingleQuote )
338
throw new IllegalArgumentException( "The Writer must not be null" );
346
for ( int i = 0; i < sz; i++ )
348
char ch = str.charAt( i );
353
out.write( "\\u" + hex( ch ) );
355
else if ( ch > 0xff )
357
out.write( "\\u0" + hex( ch ) );
359
else if ( ch > 0x7f || ch == ',' )
360
{ // Kr - this line modified from commons
361
out.write( "\\u00" + hex( ch ) );
390
out.write( "\\u00" + hex( ch ) );
394
out.write( "\\u000" + hex( ch ) );
404
if ( escapeSingleQuote )
430
public static void escapeJavaStyleString( ByteBuffer out, byte[] str, int off, int len )
434
throw new IllegalArgumentException( "The Writer must not be null" );
436
final int inputLength = str.length;
437
if ( str == null || inputLength == 0 )
443
for ( int i = off; i < end; i++ )
445
char ch = (char) str[i];
450
outputPos = writeOut( out, outputPos, "\\u" + hex( ch ) );
452
else if ( ch > 0xff )
454
outputPos = writeOut( out, outputPos, "\\u0" + hex( ch ) );
456
else if ( ch > 0x7f || ch == ',' )
457
{ // Kr - this line modified from commons
458
outputPos = writeOut( out, outputPos, "\\u00" + hex( ch ) );
487
outputPos = writeOut( out, outputPos, "\\u00" + hex( ch ) );
491
outputPos = writeOut( out, outputPos, "\\u000" + hex( ch ) );
524
public static void escapeJavaStyleString( PrintStream out, byte[] str, int off, int len )
528
throw new IllegalArgumentException( "The Writer must not be null" );
530
final int inputLength = str.length;
531
if ( str == null || inputLength == 0 )
537
for ( int i = off; i < end; i++ )
539
char ch = (char) str[i];
544
outputPos = writeOut( out, outputPos, "\\u" + hex( ch ) );
546
else if ( ch > 0xff )
548
outputPos = writeOut( out, outputPos, "\\u0" + hex( ch ) );
550
else if ( ch > 0x7f || ch == ',' )
551
{ // Kr - this line modified from commons
552
outputPos = writeOut( out, outputPos, "\\u00" + hex( ch ) );
581
outputPos = writeOut( out, outputPos, "\\u00" + hex( ch ) );
585
outputPos = writeOut( out, outputPos, "\\u000" + hex( ch ) );
618
public static int escapeJavaStyleString( byte[] out, int outoff, byte[] str, int off, int len )
622
throw new IllegalArgumentException( "The Writer must not be null" );
624
final int inputLength = str.length;
625
if ( str == null || inputLength == 0 )
629
int outputPos = outoff;
631
for ( int i = off; i < end; i++ )
633
char ch = (char) str[i];
638
outputPos = writeOut( out, outputPos, "\\u" + hex( ch ) );
640
else if ( ch > 0xff )
642
outputPos = writeOut( out, outputPos, "\\u0" + hex( ch ) );
644
else if ( ch > 0x7f || ch == ',' )
645
{ // Kr - this line modified from commons
646
outputPos = writeOut( out, outputPos, "\\u00" + hex( ch ) );
653
out[outputPos++] = '\\';
654
out[outputPos++] = 'b';
657
out[outputPos++] = '\\';
658
out[outputPos++] = 'n';
661
out[outputPos++] = '\\';
662
out[outputPos++] = 't';
665
out[outputPos++] = '\\';
666
out[outputPos++] = 'f';
669
out[outputPos++] = '\\';
670
out[outputPos++] = 'r';
675
outputPos = writeOut( out, outputPos, "\\u00" + hex( ch ) );
679
outputPos = writeOut( out, outputPos, "\\u000" + hex( ch ) );
689
out[outputPos++] = '\\';
690
out[outputPos++] = '\'';
693
out[outputPos++] = '\\';
694
out[outputPos++] = '"';
697
out[outputPos++] = '\\';
698
out[outputPos++] = '\\';
701
out[outputPos++] = '\\';
702
out[outputPos++] = '/';
705
out[outputPos++] = (byte) ch;
710
return outputPos - outoff;
713
private static int writeOut( ByteBuffer out, int outputPos, final String msg )
715
byte[] bytes = msg.getBytes();
716
for ( int cnt = 0; cnt < bytes.length; cnt++ )
718
out.append( bytes[cnt] );
723
private static int writeOut( PrintStream out, int outputPos, final String msg )
725
byte[] bytes = msg.getBytes();
726
for ( int cnt = 0; cnt < bytes.length; cnt++ )
728
out.write( bytes[cnt] );
734
private static int writeOut( byte[] out, int outputPos, final String msg )
736
byte[] bytes = msg.getBytes();
737
for ( int cnt = 0; cnt < bytes.length; cnt++ )
739
out[outputPos++] = bytes[cnt];
745
public static String hex( char ch )
747
return Integer.toHexString( ch ).toUpperCase();
751
* Courtesy of commons-lang StringEscapeUtils, slightly modified, see below
753
* @param str String to escape values in, may be null
754
* @return the escaped string
756
public static void escapeJavaStyleString( StringBuffer target, String str )
764
StringWriter writer = new StringWriter( str.length() * 2 );
765
escapeJavaStyleString( writer, str, true );
766
target.append( writer.toString() ); // todo: be bit smarter
768
catch ( IOException ioe )
770
// this should never ever happen while writing to a StringWriter
771
ioe.printStackTrace();
775
public static void escapeJavaStyleString( PrintStream target, String str )
783
StringWriter writer = new StringWriter( str.length() * 2 );
784
escapeJavaStyleString( writer, str, true );
785
target.append( writer.toString() ); // todo: be bit smarter
787
catch ( IOException ioe )
789
// this should never ever happen while writing to a StringWriter
790
ioe.printStackTrace();