1 package org.apache.velocity.util; 2 3 18 19 import java.io.File ; 20 import java.io.FileReader ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.PrintWriter ; 23 24 import java.net.MalformedURLException ; 25 26 import java.util.ArrayList ; 27 import java.util.Hashtable ; 28 import java.util.List ; 29 import java.util.StringTokenizer ; 30 import java.util.Map ; 31 32 33 44 public class StringUtils 45 { 46 49 private static final String EOL = System.getProperty("line.separator"); 50 51 54 private static final int EOL_LENGTH = EOL.length(); 55 56 62 public String concat(List list) 63 { 64 StringBuffer sb = new StringBuffer (); 65 int size = list.size(); 66 67 for (int i = 0; i < size; i++) 68 { 69 sb.append(list.get(i).toString()); 70 } 71 return sb.toString(); 72 } 73 74 80 static public String getPackageAsPath(String pckge) 81 { 82 return pckge.replace( '.', File.separator.charAt(0) ) + File.separator; 83 } 84 85 102 static public String removeUnderScores (String data) 103 { 104 String temp = null; 105 StringBuffer out = new StringBuffer (); 106 temp = data; 107 108 StringTokenizer st = new StringTokenizer (temp, "_"); 109 110 while (st.hasMoreTokens()) 111 { 112 String element = (String ) st.nextElement(); 113 out.append ( firstLetterCaps(element)); 114 } 115 116 return out.toString(); 117 } 118 119 136 static public String removeAndHump (String data) 137 { 138 return removeAndHump(data,"_"); 139 } 140 141 159 static public String removeAndHump (String data,String replaceThis) 160 { 161 String temp = null; 162 StringBuffer out = new StringBuffer (); 163 temp = data; 164 165 StringTokenizer st = new StringTokenizer (temp, replaceThis); 166 167 while (st.hasMoreTokens()) 168 { 169 String element = (String ) st.nextElement(); 170 out.append ( capitalizeFirstLetter(element)); 171 } 173 return out.toString(); 174 } 175 176 188 static public String firstLetterCaps ( String data ) 189 { 190 String firstLetter = data.substring(0,1).toUpperCase(); 191 String restLetters = data.substring(1).toLowerCase(); 192 return firstLetter + restLetters; 193 } 194 195 207 static public String capitalizeFirstLetter ( String data ) 208 { 209 String firstLetter = data.substring(0,1).toUpperCase(); 210 String restLetters = data.substring(1); 211 return firstLetter + restLetters; 212 } 213 214 221 public static String [] split(String line, String delim) 222 { 223 List list = new ArrayList (); 224 StringTokenizer t = new StringTokenizer (line, delim); 225 while (t.hasMoreTokens()) 226 { 227 list.add(t.nextToken()); 228 } 229 return (String []) list.toArray(new String [list.size()]); 230 } 231 232 242 public static String chop(String s, int i) 243 { 244 return chop(s, i, EOL); 245 } 246 247 256 public static String chop(String s, int i, String eol) 257 { 258 if ( i == 0 || s == null || eol == null ) 259 { 260 return s; 261 } 262 263 int length = s.length(); 264 265 269 if ( eol.length() == 2 && s.endsWith(eol )) 270 { 271 length -= 2; 272 i -= 1; 273 } 274 275 if ( i > 0) 276 { 277 length -= i; 278 } 279 280 if ( length < 0) 281 { 282 length = 0; 283 } 284 285 return s.substring( 0, length); 286 } 287 288 public static StringBuffer stringSubstitution( String argStr, 289 Hashtable vars ) 290 { 291 return stringSubstitution( argStr, (Map ) vars ); 292 } 293 294 304 public static StringBuffer stringSubstitution(String argStr, 305 Map vars) 306 { 307 StringBuffer argBuf = new StringBuffer (); 308 309 for (int cIdx = 0 ; cIdx < argStr.length();) 310 { 311 char ch = argStr.charAt(cIdx); 312 313 switch (ch) 314 { 315 case '$': 316 StringBuffer nameBuf = new StringBuffer (); 317 for (++cIdx ; cIdx < argStr.length(); ++cIdx) 318 { 319 ch = argStr.charAt(cIdx); 320 if (ch == '_' || Character.isLetterOrDigit(ch)) 321 nameBuf.append(ch); 322 else 323 break; 324 } 325 326 if (nameBuf.length() > 0) 327 { 328 String value = 329 (String ) vars.get(nameBuf.toString()); 330 331 if (value != null) 332 { 333 argBuf.append(value); 334 } 335 } 336 break; 337 338 default: 339 argBuf.append(ch); 340 ++cIdx; 341 break; 342 } 343 } 344 345 return argBuf; 346 } 347 348 355 public static String fileContentsToString(String file) 356 { 357 String contents = ""; 358 359 File f = new File (file); 360 361 if (f.exists()) 362 { 363 try 364 { 365 FileReader fr = new FileReader (f); 366 char[] template = new char[(int) f.length()]; 367 fr.read(template); 368 contents = new String (template); 369 } 370 catch (Exception e) 371 { 372 System.out.println(e); 373 e.printStackTrace(); 374 } 375 } 376 377 return contents; 378 } 379 380 386 public static String collapseNewlines(String argStr) 387 { 388 char last = argStr.charAt(0); 389 StringBuffer argBuf = new StringBuffer (); 390 391 for (int cIdx = 0 ; cIdx < argStr.length(); cIdx++) 392 { 393 char ch = argStr.charAt(cIdx); 394 if (ch != '\n' || last != '\n') 395 { 396 argBuf.append(ch); 397 last = ch; 398 } 399 } 400 401 return argBuf.toString(); 402 } 403 404 410 public static String collapseSpaces(String argStr) 411 { 412 char last = argStr.charAt(0); 413 StringBuffer argBuf = new StringBuffer (); 414 415 for (int cIdx = 0 ; cIdx < argStr.length(); cIdx++) 416 { 417 char ch = argStr.charAt(cIdx); 418 if (ch != ' ' || last != ' ') 419 { 420 argBuf.append(ch); 421 last = ch; 422 } 423 } 424 425 return argBuf.toString(); 426 } 427 428 437 public static final String sub(String line, String oldString, 438 String newString) 439 { 440 int i = 0; 441 if ((i = line.indexOf(oldString, i)) >= 0) 442 { 443 char [] line2 = line.toCharArray(); 444 char [] newString2 = newString.toCharArray(); 445 int oLength = oldString.length(); 446 StringBuffer buf = new StringBuffer (line2.length); 447 buf.append(line2, 0, i).append(newString2); 448 i += oLength; 449 int j = i; 450 while ((i = line.indexOf(oldString, i)) > 0) 451 { 452 buf.append(line2, j, i - j).append(newString2); 453 i += oLength; 454 j = i; 455 } 456 buf.append(line2, j, line2.length - j); 457 return buf.toString(); 458 } 459 return line; 460 } 461 462 468 public static final String stackTrace(Throwable e) 469 { 470 String foo = null; 471 try 472 { 473 ByteArrayOutputStream ostr = new ByteArrayOutputStream (); 475 e.printStackTrace( new PrintWriter (ostr,true) ); 476 foo = ostr.toString(); 477 } 478 catch (Exception f) 479 { 480 } 482 return foo; 483 } 484 485 495 public static final String normalizePath(String path) 496 { 497 String normalized = path; 499 if (normalized.indexOf('\\') >= 0) 500 { 501 normalized = normalized.replace('\\', '/'); 502 } 503 504 if (!normalized.startsWith("/")) 505 { 506 normalized = "/" + normalized; 507 } 508 509 while (true) 511 { 512 int index = normalized.indexOf("//"); 513 if (index < 0) 514 break; 515 normalized = normalized.substring(0, index) + 516 normalized.substring(index + 1); 517 } 518 519 while (true) 521 { 522 int index = normalized.indexOf("%20"); 523 if (index < 0) 524 break; 525 normalized = normalized.substring(0, index) + " " + 526 normalized.substring(index + 3); 527 } 528 529 while (true) 531 { 532 int index = normalized.indexOf("/./"); 533 if (index < 0) 534 break; 535 normalized = normalized.substring(0, index) + 536 normalized.substring(index + 2); 537 } 538 539 while (true) 541 { 542 int index = normalized.indexOf("/../"); 543 if (index < 0) 544 break; 545 if (index == 0) 546 return (null); int index2 = normalized.lastIndexOf('/', index - 1); 548 normalized = normalized.substring(0, index2) + 549 normalized.substring(index + 3); 550 } 551 552 return (normalized); 554 } 555 556 564 public String select(boolean state, String trueString, String falseString) 565 { 566 if (state) 567 { 568 return trueString; 569 } 570 else 571 { 572 return falseString; 573 } 574 } 575 576 583 public boolean allEmpty(List list) 584 { 585 int size = list.size(); 586 587 for (int i = 0; i < size; i++) 588 { 589 if (list.get(i) != null && list.get(i).toString().length() > 0) 590 { 591 return false; 592 } 593 } 594 return true; 595 } 596 } 597 | Popular Tags |