View Javadoc
1 package org.apache.turbine.util; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 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, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" 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 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 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 import java.io.ByteArrayOutputStream; 58 import java.io.OutputStream; 59 import java.io.PrintWriter; 60 import java.util.NoSuchElementException; 61 import java.util.StringTokenizer; 62 63 /*** 64 * This is where common String manipulation routines should go. 65 * 66 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> 67 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a> 68 * @author <a href="mailto:gcoladonato@yahoo.com">Greg Coladonato</a> 69 * @version $Id: StringUtils.java,v 1.17 2001/05/30 23:44:44 dlr Exp $ 70 */ 71 public class StringUtils 72 { 73 /*** 74 * Deal with null strings converting them to "" instead. It also 75 * invokes String.trim() on the output. 76 * 77 * @param foo A String. 78 * @return A String. 79 */ 80 public static final String makeString(String foo) 81 { 82 return (foo == null ? "" : foo.trim()); 83 } 84 85 /*** 86 * Validates that the supplied string is neither <code>null</code> 87 * nor the empty string. 88 * 89 * @param foo The text to check. 90 * @return Whether valid. 91 */ 92 public static final boolean isValid(String foo) 93 { 94 return (foo != null && foo.length() > 0); 95 } 96 97 /*** 98 * Determine whether a (trimmed) string is empty 99 * 100 * @param foo The text to check. 101 * @return Whether empty. 102 */ 103 public static final boolean isEmpty(String foo) 104 { 105 return (foo == null || foo.trim().length() == 0); 106 } 107 108 /*** 109 * Returns the output of printStackTrace as a String. 110 * 111 * @param e A Throwable. 112 * @return A String. 113 */ 114 public static final String stackTrace(Throwable e) 115 { 116 String foo = null; 117 try 118 { 119 // And show the Error Screen. 120 ByteArrayOutputStream buf = new ByteArrayOutputStream(); 121 e.printStackTrace( new PrintWriter(buf, true) ); 122 foo = buf.toString(); 123 } 124 catch (Exception f) 125 { 126 // Do nothing. 127 } 128 return foo; 129 } 130 131 /*** 132 * Returns the output of printStackTrace as a String. 133 * 134 * @param e A Throwable. 135 * @param addPre a boolean to add HTML <pre> tags around the stacktrace 136 * @return A String. 137 */ 138 public static final String stackTrace(Throwable e, boolean addPre) 139 { 140 if (addPre) 141 { 142 return "<pre>" + stackTrace(e) + "</pre>"; 143 } 144 else 145 { 146 return stackTrace(e); 147 } 148 } 149 150 /*** 151 * Compares two Strings, returns true if their values are the 152 * same. 153 * 154 * @param s1 The first string. 155 * @param s2 The second string. 156 * @return True if the values of both strings are the same. 157 */ 158 public static boolean equals( String s1, 159 String s2 ) 160 { 161 if (s1 == null) 162 { 163 return (s2 == null); 164 } 165 else if (s2 == null) 166 { 167 // s1 is not null 168 return false; 169 } 170 else 171 { 172 return s1.equals(s2); 173 } 174 } 175 176 public static final int PPKEY_CLASSNAME = 0; 177 public static final int PPKEY_ID = 1; 178 public static final int PPKEY_PROPERTY = 2; 179 180 /*** 181 * Takes a String of the form substring[substring]subtring and 182 * returns the 3 substrings 183 * 184 * @returns a three element String array 185 */ 186 public static String[] parseObjectKey(String s) 187 { 188 String[] p = new String[3]; 189 StringTokenizer st = new StringTokenizer(s, "[]"); 190 int count = st.countTokens(); 191 if ( count > 1) 192 { 193 p[0] = st.nextToken(); 194 p[1] = st.nextToken(); 195 if (count == 3) 196 { 197 p[2] = st.nextToken(); 198 } 199 } 200 return p; 201 } 202 203 204 /*** 205 * Remove Underscores from a string and replaces first 206 * Letters with Capitals. foo_bar becomes FooBar 207 */ 208 public static String removeUnderScores (String data) 209 { 210 String temp = null; 211 StringBuffer out = new StringBuffer(); 212 temp = data; 213 214 StringTokenizer st = new StringTokenizer(temp, "_"); 215 while (st.hasMoreTokens()) 216 { 217 String element = (String) st.nextElement(); 218 out.append ( firstLetterCaps(element)); 219 } 220 return out.toString(); 221 } 222 223 /*** 224 * Makes the first letter caps and leaves the rest as is. 225 */ 226 public static String firstLetterCaps ( String data ) 227 { 228 StringBuffer sbuf = new StringBuffer(data.length()); 229 sbuf.append(data.substring(0, 1).toUpperCase()) 230 .append(data.substring(1)); 231 return sbuf.toString(); 232 } 233 234 /*** 235 * Splits the provided CSV text into a list. 236 * 237 * @param text The CSV list of values to split apart. 238 * @param separator The separator character. 239 * @return The list of values. 240 */ 241 public static String[] split(String text, String separator) 242 { 243 StringTokenizer st = new StringTokenizer(text, separator); 244 String[] values = new String[st.countTokens()]; 245 int pos = 0; 246 while (st.hasMoreTokens()) 247 { 248 values[pos++] = st.nextToken(); 249 } 250 return values; 251 } 252 253 /*** 254 * Joins the elements of the provided array into a single string 255 * containing a list of CSV elements. 256 * 257 * @param list The list of values to join together. 258 * @param separator The separator character. 259 * @return The CSV text. 260 */ 261 public static String join(String[] list, String separator) 262 { 263 StringBuffer csv = new StringBuffer(); 264 for (int i = 0; i < list.length; i++) 265 { 266 if (i > 0) 267 { 268 csv.append(separator); 269 } 270 csv.append(list[i]); 271 } 272 return csv.toString(); 273 } 274 275 /*** 276 * Takes a block of text which might have long lines in it and wraps 277 * the long lines based on the supplied wrapColumn parameter. It was 278 * initially implemented for use by VelocityEmail. If there are tabs 279 * in inString, you are going to get results that are a bit strange, 280 * since tabs are a single character but are displayed as 4 or 8 281 * spaces. Remove the tabs. 282 * 283 * @param inString Text which is in need of word-wrapping. 284 * @param newline The characters that define a newline. 285 * @param wrapColumn The column to wrap the words at. 286 * @return The text with all the long lines word-wrapped. 287 */ 288 289 public static String wrapText (String inString, String newline, 290 int wrapColumn) 291 { 292 StringTokenizer lineTokenizer = new StringTokenizer ( 293 inString, newline, true); 294 StringBuffer stringBuffer = new StringBuffer(); 295 296 while (lineTokenizer.hasMoreTokens ()) 297 { 298 try 299 { 300 String nextLine = lineTokenizer.nextToken(); 301 302 if (nextLine.length() > wrapColumn) 303 { 304 // This line is long enough to be wrapped. 305 nextLine = wrapLine (nextLine, newline, wrapColumn); 306 } 307 308 stringBuffer.append (nextLine); 309 } 310 catch (NoSuchElementException nsee) 311 { 312 // thrown by nextToken(), but I don't know why it would 313 break; 314 } 315 } 316 317 return (stringBuffer.toString()); 318 } 319 320 /*** 321 * Wraps a single line of text. Called by wrapText(). I can't 322 * think of any good reason for exposing this to the public, 323 * since wrapText should always be used AFAIK. 324 * 325 * @param line A line which is in need of word-wrapping. 326 * @param newline The characters that define a newline. 327 * @param wrapColumn The column to wrap the words at. 328 * @return A line with newlines inserted. 329 */ 330 331 protected static String wrapLine (String line, String newline, 332 int wrapColumn) 333 { 334 StringBuffer wrappedLine = new StringBuffer(); 335 336 while (line.length() > wrapColumn) 337 { 338 int spaceToWrapAt = line.lastIndexOf (' ', wrapColumn); 339 340 if (spaceToWrapAt >= 0) 341 { 342 wrappedLine.append (line.substring (0, spaceToWrapAt)); 343 wrappedLine.append (newline); 344 line = line.substring (spaceToWrapAt + 1); 345 } 346 347 // This must be a really long word or URL. Pass it 348 // through unchanged even though it's longer than the 349 // wrapColumn would allow. This behavior could be 350 // dependent on a parameter for those situations when 351 // someone wants long words broken at line length. 352 else 353 { 354 spaceToWrapAt = line.indexOf (' ', wrapColumn); 355 356 if (spaceToWrapAt >= 0) 357 { 358 wrappedLine.append (line.substring (0, spaceToWrapAt)); 359 wrappedLine.append (newline); 360 line = line.substring (spaceToWrapAt + 1); 361 } 362 else 363 { 364 wrappedLine.append (line); 365 line = ""; 366 } 367 } 368 } 369 370 // Whatever is left in line is short enough to just pass through, 371 // just like a small small kidney stone 372 wrappedLine.append (line); 373 374 return (wrappedLine.toString()); 375 } 376 }

This page was automatically generated by Maven