1 package org.apache.maven.surefire.util.internal; 2 3 /* 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 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 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 19 * under the License. 20 */ 21 22 import java.util.StringTokenizer; 23 24 /** 25 * <p>Common <code>String</code> manipulation routines.</p> 26 * 27 * <p>Originally from 28 * <a href="http://jakarta.apache.org/turbine/">Turbine</a> and the 29 * GenerationJavaCore library.</p> 30 * 31 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> 32 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a> 33 * @author <a href="mailto:gcoladonato@yahoo.com">Greg Coladonato</a> 34 * @author <a href="mailto:bayard@generationjava.com">Henri Yandell</a> 35 * @author <a href="mailto:ed@codehaus.org">Ed Korthof</a> 36 * @author <a href="mailto:rand_mcneely@yahoo.com">Rand McNeely</a> 37 * @author Stephen Colebourne 38 * @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a> 39 * @author Holger Krauth 40 * @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a> 41 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a> 42 * @since 1.0 43 * @version $Id: StringUtils.java 8001 2009-01-03 13:17:09Z vsiveton $ 44 * @noinspection JavaDoc 45 * 46 * A quick borrow from plexus-utils by Kristian Rosenvold, to restore jdk1.3 compat 47 * Threw away all the unused stuff. 48 * 49 * NOTE: This class is not part of any api and is public purely for technical reasons ! 50 */ 51 public class StringUtils 52 { 53 54 // Splitting 55 //-------------------------------------------------------------------------- 56 57 public static String[] split( String text, String separator ) 58 { 59 int max = -1; 60 StringTokenizer tok; 61 if ( separator == null ) 62 { 63 // Null separator means we're using StringTokenizer's default 64 // delimiter, which comprises all whitespace characters. 65 tok = new StringTokenizer( text ); 66 } 67 else 68 { 69 tok = new StringTokenizer( text, separator ); 70 } 71 72 int listSize = tok.countTokens(); 73 if ( ( max > 0 ) && ( listSize > max ) ) 74 { 75 listSize = max; 76 } 77 78 String[] list = new String[listSize]; 79 int i = 0; 80 int lastTokenBegin; 81 int lastTokenEnd = 0; 82 while ( tok.hasMoreTokens() ) 83 { 84 if ( ( max > 0 ) && ( i == listSize - 1 ) ) 85 { 86 // In the situation where we hit the max yet have 87 // tokens left over in our input, the last list 88 // element gets all remaining text. 89 String endToken = tok.nextToken(); 90 lastTokenBegin = text.indexOf( endToken, lastTokenEnd ); 91 list[i] = text.substring( lastTokenBegin ); 92 break; 93 } 94 else 95 { 96 list[i] = tok.nextToken(); 97 lastTokenBegin = text.indexOf( list[i], lastTokenEnd ); 98 lastTokenEnd = lastTokenBegin + list[i].length(); 99 } 100 i++; 101 } 102 return list; 103 } 104 105 // Replacing 106 //-------------------------------------------------------------------------- 107 108 /** 109 * <p>Replace all occurrences of a String within another String.</p> 110 * 111 * <p>A <code>null</code> reference passed to this method is a no-op.</p> 112 * 113 * @param text text to search and replace in 114 * @param repl String to search for 115 * @param with String to replace with 116 * @return the text with any replacements processed 117 */ 118 public static String replace( String text, String repl, String with ) 119 { 120 int max = -1; 121 if ( ( text == null ) || ( repl == null ) || ( with == null ) || ( repl.length() == 0 ) ) 122 { 123 return text; 124 } 125 126 StringBuffer buf = new StringBuffer( text.length() ); 127 int start = 0, end; 128 while ( ( end = text.indexOf( repl, start ) ) != -1 ) 129 { 130 buf.append( text.substring( start, end ) ).append( with ); 131 start = end + repl.length(); 132 133 if ( --max == 0 ) 134 { 135 break; 136 } 137 } 138 buf.append( text.substring( start ) ); 139 return buf.toString(); 140 } 141 142 143 /** 144 * <p>Checks if a (trimmed) String is <code>null</code> or blank.</p> 145 * 146 * @param str the String to check 147 * @return <code>true</code> if the String is <code>null</code>, or 148 * length zero once trimmed 149 */ 150 public static boolean isBlank( String str ) 151 { 152 return ( ( str == null ) || ( str.trim().length() == 0 ) ); 153 } 154 } 155