|
| 1 | +/* ==================================================================== |
| 2 | + * The Apache Software License, Version 1.1 |
| 3 | + * |
| 4 | + * Copyright (c) 2002 The Apache Software Foundation. All rights |
| 5 | + * reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions |
| 9 | + * are met: |
| 10 | + * |
| 11 | + * 1. Redistributions of source code must retain the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer. |
| 13 | + * |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | + * notice, this list of conditions and the following disclaimer in |
| 16 | + * the documentation and/or other materials provided with the |
| 17 | + * distribution. |
| 18 | + * |
| 19 | + * 3. The end-user documentation included with the redistribution, if |
| 20 | + * any, must include the following acknowledgement: |
| 21 | + * "This product includes software developed by the |
| 22 | + * Apache Software Foundation (http://www.codehaus.org/)." |
| 23 | + * Alternately, this acknowledgement may appear in the software itself, |
| 24 | + * if and wherever such third-party acknowledgements normally appear. |
| 25 | + * |
| 26 | + * 4. The names "The Jakarta Project", "Commons", and "Apache Software |
| 27 | + * Foundation" must not be used to endorse or promote products derived |
| 28 | + * from this software without prior written permission. For written |
| 29 | + * permission, please contact [email protected]. |
| 30 | + * |
| 31 | + * 5. Products derived from this software may not be called "Apache" |
| 32 | + * nor may "Apache" appear in their names without prior written |
| 33 | + * permission of the Apache Software Foundation. |
| 34 | + * |
| 35 | + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
| 36 | + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 37 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 38 | + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR |
| 39 | + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 42 | + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 43 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 44 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 45 | + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 46 | + * SUCH DAMAGE. |
| 47 | + * ==================================================================== |
| 48 | + * |
| 49 | + * This software consists of voluntary contributions made by many |
| 50 | + * individuals on behalf of the Apache Software Foundation. For more |
| 51 | + * information on the Apache Software Foundation, please see |
| 52 | + * <http://www.codehaus.org/>. |
| 53 | + */ |
| 54 | +package org.codehaus.plexus.util.xml; |
| 55 | + |
| 56 | +import java.util.StringTokenizer; |
| 57 | + |
| 58 | +/** |
| 59 | + * <p> |
| 60 | + * Common <code>String</code> manipulation routines, extracted from Plexus Utils and trimmed down for Plexus Xml. |
| 61 | + * </p> |
| 62 | + * <p> |
| 63 | + * Originally from <a href="http://jakarta.apache.org/turbine/">Turbine</a> and the GenerationJavaCore library. |
| 64 | + * </p> |
| 65 | + * |
| 66 | + * @author <a href="mailto:[email protected]">Jon S. Stevens</a> |
| 67 | + * @author <a href="mailto:[email protected]">Daniel Rall</a> |
| 68 | + * @author <a href="mailto:[email protected]">Greg Coladonato</a> |
| 69 | + * @author <a href="mailto:[email protected]">Henri Yandell</a> |
| 70 | + * @author <a href="mailto:[email protected]">Ed Korthof</a> |
| 71 | + * @author <a href="mailto:[email protected]">Rand McNeely</a> |
| 72 | + * @author Stephen Colebourne |
| 73 | + * @author <a href="mailto:[email protected]">Fredrik Westermarck</a> |
| 74 | + * @author Holger Krauth |
| 75 | + * @author <a href="mailto:[email protected]">Alexander Day Chaffee</a> |
| 76 | + * @author <a href="mailto:[email protected]">Vincent Siveton</a> |
| 77 | + * @since 1.0 |
| 78 | + * |
| 79 | + */ |
| 80 | +class StringUtils |
| 81 | +{ |
| 82 | + /** |
| 83 | + * <p> |
| 84 | + * <code>StringUtils</code> instances should NOT be constructed in standard programming. Instead, the class should |
| 85 | + * be used as <code>StringUtils.trim(" foo ");</code>. |
| 86 | + * </p> |
| 87 | + * <p> |
| 88 | + * This constructor is public to permit tools that require a JavaBean manager to operate. |
| 89 | + * </p> |
| 90 | + */ |
| 91 | + private StringUtils() |
| 92 | + { |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Checks if a String is <code>null</code> or empty. |
| 97 | + * <p> |
| 98 | + * <strong>Note:</strong> In releases prior 3.5.0, this method trimmed the input string such that it worked |
| 99 | + * the same as {@link #isBlank(String)}. Since release 3.5.0 it no longer returns {@code true} for strings |
| 100 | + * containing only whitespace characters. |
| 101 | + * |
| 102 | + * @param str the String to check |
| 103 | + * @return <code>true</code> if the String is <code>null</code>, or length zero |
| 104 | + */ |
| 105 | + public static boolean isEmpty( String str ) |
| 106 | + { |
| 107 | + return ( ( str == null ) || ( str.isEmpty() ) ); |
| 108 | + } |
| 109 | + |
| 110 | + // Splitting |
| 111 | + // -------------------------------------------------------------------------- |
| 112 | + |
| 113 | + /** |
| 114 | + * @param text The string to parse. |
| 115 | + * @param separator Characters used as the delimiters. If <code>null</code>, splits on whitespace. |
| 116 | + * @return an array of parsed Strings |
| 117 | + */ |
| 118 | + public static String[] split( String text, String separator ) |
| 119 | + { |
| 120 | + return split( text, separator, -1 ); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * <p> |
| 125 | + * Splits the provided text into a array, based on a given separator. |
| 126 | + * </p> |
| 127 | + * <p> |
| 128 | + * The separator is not included in the returned String array. The maximum number of splits to perform can be |
| 129 | + * controlled. A <code>null</code> separator will cause parsing to be on whitespace. |
| 130 | + * </p> |
| 131 | + * <p> |
| 132 | + * This is useful for quickly splitting a String directly into an array of tokens, instead of an enumeration of |
| 133 | + * tokens (as <code>StringTokenizer</code> does). |
| 134 | + * </p> |
| 135 | + * |
| 136 | + * @param str The string to parse. |
| 137 | + * @param separator Characters used as the delimiters. If <code>null</code>, splits on whitespace. |
| 138 | + * @param max The maximum number of elements to include in the array. A zero or negative value implies no limit. |
| 139 | + * @return an array of parsed Strings |
| 140 | + */ |
| 141 | + private static String[] split( String str, String separator, int max ) |
| 142 | + { |
| 143 | + StringTokenizer tok; |
| 144 | + if ( separator == null ) |
| 145 | + { |
| 146 | + // Null separator means we're using StringTokenizer's default |
| 147 | + // delimiter, which comprises all whitespace characters. |
| 148 | + tok = new StringTokenizer( str ); |
| 149 | + } |
| 150 | + else |
| 151 | + { |
| 152 | + tok = new StringTokenizer( str, separator ); |
| 153 | + } |
| 154 | + |
| 155 | + int listSize = tok.countTokens(); |
| 156 | + if ( ( max > 0 ) && ( listSize > max ) ) |
| 157 | + { |
| 158 | + listSize = max; |
| 159 | + } |
| 160 | + |
| 161 | + String[] list = new String[listSize]; |
| 162 | + int i = 0; |
| 163 | + int lastTokenBegin; |
| 164 | + int lastTokenEnd = 0; |
| 165 | + while ( tok.hasMoreTokens() ) |
| 166 | + { |
| 167 | + if ( ( max > 0 ) && ( i == listSize - 1 ) ) |
| 168 | + { |
| 169 | + // In the situation where we hit the max yet have |
| 170 | + // tokens left over in our input, the last list |
| 171 | + // element gets all remaining text. |
| 172 | + String endToken = tok.nextToken(); |
| 173 | + lastTokenBegin = str.indexOf( endToken, lastTokenEnd ); |
| 174 | + list[i] = str.substring( lastTokenBegin ); |
| 175 | + break; |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + list[i] = tok.nextToken(); |
| 180 | + lastTokenBegin = str.indexOf( list[i], lastTokenEnd ); |
| 181 | + lastTokenEnd = lastTokenBegin + list[i].length(); |
| 182 | + } |
| 183 | + i++; |
| 184 | + } |
| 185 | + return list; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * <p> |
| 190 | + * Repeat a String <code>n</code> times to form a new string. |
| 191 | + * </p> |
| 192 | + * |
| 193 | + * @param str String to repeat |
| 194 | + * @param repeat number of times to repeat str |
| 195 | + * @return String with repeated String |
| 196 | + * @throws NegativeArraySizeException if <code>repeat < 0</code> |
| 197 | + * @throws NullPointerException if str is <code>null</code> |
| 198 | + */ |
| 199 | + public static String repeat( String str, int repeat ) |
| 200 | + { |
| 201 | + StringBuilder buffer = new StringBuilder( repeat * str.length() ); |
| 202 | + for ( int i = 0; i < repeat; i++ ) |
| 203 | + { |
| 204 | + buffer.append( str ); |
| 205 | + } |
| 206 | + return buffer.toString(); |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Remove all duplicate whitespace characters and line terminators are replaced with a single space. |
| 211 | + * |
| 212 | + * @param s a not null String |
| 213 | + * @return a string with unique whitespace. |
| 214 | + * @since 1.5.7 |
| 215 | + */ |
| 216 | + public static String removeDuplicateWhitespace( String s ) |
| 217 | + { |
| 218 | + StringBuilder result = new StringBuilder(); |
| 219 | + int length = s.length(); |
| 220 | + boolean isPreviousWhiteSpace = false; |
| 221 | + for ( int i = 0; i < length; i++ ) |
| 222 | + { |
| 223 | + char c = s.charAt( i ); |
| 224 | + boolean thisCharWhiteSpace = Character.isWhitespace( c ); |
| 225 | + if ( !( isPreviousWhiteSpace && thisCharWhiteSpace ) ) |
| 226 | + { |
| 227 | + result.append( c ); |
| 228 | + } |
| 229 | + isPreviousWhiteSpace = thisCharWhiteSpace; |
| 230 | + } |
| 231 | + return result.toString(); |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * Parses the given String and replaces all occurrences of '\n', '\r' and '\r\n' with the system line separator. |
| 236 | + * |
| 237 | + * @param s a not null String |
| 238 | + * @param ls the wanted line separator ("\n" on UNIX), if null using the System line separator. |
| 239 | + * @return a String that contains only System line separators. |
| 240 | + * @throws IllegalArgumentException if ls is not '\n', '\r' and '\r\n' characters. |
| 241 | + * @since 1.5.7 |
| 242 | + */ |
| 243 | + public static String unifyLineSeparators( String s, String ls ) |
| 244 | + { |
| 245 | + if ( s == null ) |
| 246 | + { |
| 247 | + return null; |
| 248 | + } |
| 249 | + |
| 250 | + if ( ls == null ) |
| 251 | + { |
| 252 | + ls = System.getProperty( "line.separator" ); |
| 253 | + } |
| 254 | + |
| 255 | + if ( !( ls.equals( "\n" ) || ls.equals( "\r" ) || ls.equals( "\r\n" ) ) ) |
| 256 | + { |
| 257 | + throw new IllegalArgumentException( "Requested line separator is invalid." ); |
| 258 | + } |
| 259 | + |
| 260 | + int length = s.length(); |
| 261 | + |
| 262 | + StringBuilder buffer = new StringBuilder( length ); |
| 263 | + for ( int i = 0; i < length; i++ ) |
| 264 | + { |
| 265 | + if ( s.charAt( i ) == '\r' ) |
| 266 | + { |
| 267 | + if ( ( i + 1 ) < length && s.charAt( i + 1 ) == '\n' ) |
| 268 | + { |
| 269 | + i++; |
| 270 | + } |
| 271 | + |
| 272 | + buffer.append( ls ); |
| 273 | + } |
| 274 | + else if ( s.charAt( i ) == '\n' ) |
| 275 | + { |
| 276 | + buffer.append( ls ); |
| 277 | + } |
| 278 | + else |
| 279 | + { |
| 280 | + buffer.append( s.charAt( i ) ); |
| 281 | + } |
| 282 | + } |
| 283 | + |
| 284 | + return buffer.toString(); |
| 285 | + } |
| 286 | +} |
0 commit comments