1 57 58 package com.Yasna.util; 59 60 import java.security.*; 61 import java.text.*; 62 import java.util.*; 63 64 67 public class StringUtils { 68 69 73 private static Object initLock = new Object (); 74 75 84 public static final String replace( String line, String oldString, String newString ) 85 { 86 if (line == null) { 87 return null; 88 } 89 int i=0; 90 if ( ( i=line.indexOf( oldString, i ) ) >= 0 ) { 91 char [] line2 = line.toCharArray(); 92 char [] newString2 = newString.toCharArray(); 93 int oLength = oldString.length(); 94 StringBuffer buf = new StringBuffer (line2.length); 95 buf.append(line2, 0, i).append(newString2); 96 i += oLength; 97 int j = i; 98 while( ( i=line.indexOf( oldString, i ) ) > 0 ) { 99 buf.append(line2, j, i-j).append(newString2); 100 i += oLength; 101 j = i; 102 } 103 buf.append(line2, j, line2.length - j); 104 return buf.toString(); 105 } 106 return line; 107 } 108 109 119 public static final String replaceIgnoreCase(String line, String oldString, 120 String newString) 121 { 122 if (line == null) { 123 return null; 124 } 125 String lcLine = line.toLowerCase(); 126 String lcOldString = oldString.toLowerCase(); 127 int i=0; 128 if ( ( i=lcLine.indexOf( lcOldString, i ) ) >= 0 ) { 129 char [] line2 = line.toCharArray(); 130 char [] newString2 = newString.toCharArray(); 131 int oLength = oldString.length(); 132 StringBuffer buf = new StringBuffer (line2.length); 133 buf.append(line2, 0, i).append(newString2); 134 i += oLength; 135 int j = i; 136 while( ( i=lcLine.indexOf( lcOldString, i ) ) > 0 ) { 137 buf.append(line2, j, i-j).append(newString2); 138 i += oLength; 139 j = i; 140 } 141 buf.append(line2, j, line2.length - j); 142 return buf.toString(); 143 } 144 return line; 145 } 146 147 157 public static final String replace( String line, String oldString, 158 String newString, int[] count) 159 { 160 if (line == null) { 161 return null; 162 } 163 int i=0; 164 if ( ( i=line.indexOf( oldString, i ) ) >= 0 ) { 165 int counter = 0; 166 counter++; 167 char [] line2 = line.toCharArray(); 168 char [] newString2 = newString.toCharArray(); 169 int oLength = oldString.length(); 170 StringBuffer buf = new StringBuffer (line2.length); 171 buf.append(line2, 0, i).append(newString2); 172 i += oLength; 173 int j = i; 174 while( ( i=line.indexOf( oldString, i ) ) > 0 ) { 175 counter++; 176 buf.append(line2, j, i-j).append(newString2); 177 i += oLength; 178 j = i; 179 } 180 buf.append(line2, j, line2.length - j); 181 count[0] = counter; 182 return buf.toString(); 183 } 184 return line; 185 } 186 187 196 public static final String escapeHTMLTags( String input ) { 197 if( input == null || input.length() == 0 ) { 200 return input; 201 } 202 StringBuffer buf = new StringBuffer (input.length()); 205 char ch = ' '; 206 for( int i=0; i<input.length(); i++ ) { 207 ch = input.charAt(i); 208 if( ch == '<' ) { 209 buf.append("<"); 210 } 211 else if( ch == '>' ) { 212 buf.append(">"); 213 } 214 else { 215 buf.append( ch ); 216 } 217 } 218 return buf.toString(); 219 } 220 221 224 private static MessageDigest digest = null; 225 226 250 public synchronized static final String hash(String data) { 251 if (digest == null) { 252 try { 253 digest = MessageDigest.getInstance("MD5"); 254 } 255 catch (NoSuchAlgorithmException nsae) { 256 System.err.println("Failed to load the MD5 MessageDigest. " + 257 "Jive will be unable to function normally."); 258 nsae.printStackTrace(); 259 } 260 } 261 digest.update(data.getBytes()); 263 return toHex(digest.digest()); 264 } 265 266 277 public static final String toHex (byte hash[]) { 278 StringBuffer buf = new StringBuffer (hash.length * 2); 279 int i; 280 281 for (i = 0; i < hash.length; i++) { 282 if (((int) hash[i] & 0xff) < 0x10) { 283 buf.append("0"); 284 } 285 buf.append(Long.toString((int) hash[i] & 0xff, 16)); 286 } 287 return buf.toString(); 288 } 289 290 300 public static final String [] toLowerCaseWordArray(String text) { 301 if (text == null || text.length() == 0) { 302 return new String [0]; 303 } 304 StringTokenizer tokens = new StringTokenizer(text, " ,\r\n.:/\\+"); 305 String [] words = new String [tokens.countTokens()]; 306 for (int i=0; i<words.length; i++) { 307 words[i] = tokens.nextToken().toLowerCase(); 308 } 309 return words; 310 } 311 312 317 private static final String [] commonWords = new String [] { 318 "a", "and", "as", "at", "be", "do", "i", "if", "in", "is", "it", "so", 319 "the", "to" 320 }; 321 private static Map commonWordsMap = null; 322 323 328 public static final String [] removeCommonWords(String [] words) { 329 if (commonWordsMap == null) { 333 synchronized(initLock) { 334 if (commonWordsMap == null) { 335 commonWordsMap = new HashMap(); 336 for (int i=0; i<commonWords.length; i++) { 337 commonWordsMap.put(commonWords[i], commonWords[i]); 338 } 339 } 340 } 341 } 342 ArrayList results = new ArrayList(words.length); 344 for (int i=0; i<words.length; i++) { 345 if (!commonWordsMap.containsKey(words[i])) { 346 results.add(words[i]); 347 } 348 } 349 return (String [])results.toArray(new String [results.size()]); 350 } 351 352 357 private static Random randGen = null; 358 359 365 private static char[] numbersAndLetters = null; 366 367 384 public static final String randomString(int length) { 385 if (length < 1) { 386 return null; 387 } 388 if (randGen == null) { 390 synchronized (initLock) { 391 if (randGen == null) { 392 randGen = new Random(); 393 numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" + 395 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray(); 396 } 397 } 398 } 399 char [] randBuffer = new char[length]; 401 for (int i=0; i<randBuffer.length; i++) { 402 randBuffer[i] = numbersAndLetters[randGen.nextInt(71)]; 403 } 404 return new String (randBuffer); 405 } 406 407 424 public static final String chopAtWord(String string, int length) { 425 if (string == null) { 426 return string; 427 } 428 429 char [] charArray = string.toCharArray(); 430 int sLength = string.length(); 431 if (length < sLength) { 432 sLength = length; 433 } 434 435 for (int i=0; i<sLength-1; i++) { 438 if (charArray[i] == '\r' && charArray[i+1] == '\n') { 440 return string.substring(0, i); 441 } 442 else if (charArray[i] == '\n') { 444 return string.substring(0, i); 445 } 446 } 447 if (charArray[sLength-1] == '\n') { 449 return string.substring(0, sLength-1); 450 } 451 452 if (string.length() < length) { 455 return string; 456 } 457 458 for (int i = length-1; i > 0; i--) { 460 if (charArray[i] == ' ') { 461 return string.substring(0, i).trim(); 462 } 463 } 464 465 return string.substring(0, length); 468 } 469 470 481 public static final String highlightWords(String string, String [] words, 482 String startHighlight, String endHighlight) 483 { 484 if (string == null || words == null || 485 startHighlight == null || endHighlight == null) 486 { 487 return null; 488 } 489 490 for (int x=0; x<words.length; x++) { 492 String lcString = string.toLowerCase(); 494 char [] string2 = string.toCharArray(); 496 String word = words[x].toLowerCase(); 497 498 int i=0; 500 if ( ( i=lcString.indexOf( word, i ) ) >= 0 ) { 501 int oLength = word.length(); 502 StringBuffer buf = new StringBuffer (string2.length); 503 504 boolean startSpace = false; 508 char startChar = ' '; 509 if (i-1 > 0) { 510 startChar = string2[i-1]; 511 if (!Character.isLetter(startChar)) { 512 startSpace = true; 513 } 514 } 515 boolean endSpace = false; 516 char endChar = ' '; 517 if (i+oLength<string2.length) { 518 endChar = string2[i+oLength]; 519 if (!Character.isLetter(endChar)) { 520 endSpace = true; 521 } 522 } 523 if ((startSpace && endSpace) || (i==0 && endSpace)) { 524 buf.append(string2, 0, i); 525 if (startSpace && startChar==' ') { buf.append(startChar); } 526 buf.append(startHighlight); 527 buf.append(string2, i, oLength).append(endHighlight); 528 if (endSpace && endChar==' ') { buf.append(endChar); } 529 } 530 else { 531 buf.append(string2, 0, i); 532 buf.append(string2, i, oLength); 533 } 534 535 i += oLength; 536 int j = i; 537 while( ( i=lcString.indexOf( word, i ) ) > 0 ) { 538 startSpace = false; 539 startChar = string2[i-1]; 540 if (!Character.isLetter(startChar)) { 541 startSpace = true; 542 } 543 544 endSpace = false; 545 if (i+oLength<string2.length) { 546 endChar = string2[i+oLength]; 547 if (!Character.isLetter(endChar)) { 548 endSpace = true; 549 } 550 } 551 if ((startSpace && endSpace) || i+oLength==string2.length) { 552 buf.append(string2, j, i-j); 553 if (startSpace && startChar==' ') { buf.append(startChar); } 554 buf.append(startHighlight); 555 buf.append(string2, i, oLength).append(endHighlight); 556 if (endSpace && endChar==' ') { buf.append(endChar); } 557 } 558 else { 559 buf.append(string2, j, i-j); 560 buf.append(string2, i, oLength); 561 } 562 i += oLength; 563 j = i; 564 } 565 buf.append(string2, j, string2.length - j); 566 string = buf.toString(); 567 } 568 } 569 return string; 570 } 571 572 579 public static final String escapeForXML(String string) { 580 if (string == null || string.length() == 0 ) { 583 return string; 584 } 585 char [] sArray = string.toCharArray(); 586 StringBuffer buf = new StringBuffer (sArray.length); 587 char ch; 588 for (int i=0; i<sArray.length; i++) { 589 ch = sArray[i]; 590 if(ch == '<') { 591 buf.append("<"); 592 } 593 else if (ch == '&') { 594 buf.append("&"); 595 } 596 else if (ch == '"') { 597 buf.append("""); 598 } 599 else { 600 buf.append(ch); 601 } 602 } 603 return buf.toString(); 604 } 605 606 } 607 | Popular Tags |