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

This page was automatically generated by Maven