| 1 package org.roller.util; 2 3 import java.io.BufferedInputStream ; 4 import java.io.BufferedOutputStream ; 5 import java.io.File ; 6 import java.io.FileInputStream ; 7 import java.io.FileOutputStream ; 8 import java.io.IOException ; 9 import java.io.InputStream ; 10 import java.io.OutputStream ; 11 import java.io.UnsupportedEncodingException ; 12 import java.net.URLEncoder ; 13 import java.security.MessageDigest ; 14 import java.util.Date ; 15 import java.util.NoSuchElementException ; 16 import java.util.StringTokenizer ; 17 import java.util.regex.Matcher ; 18 import java.util.regex.Pattern ; 19 20 import org.apache.commons.lang.StringEscapeUtils; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 39 public class Utilities 40 { 41 42 private static Log mLogger = LogFactory.getLog(Utilities.class); 43 44 45 private static Pattern mLinkPattern = 46 Pattern.compile("<a HREF=.*?>", Pattern.CASE_INSENSITIVE); 47 48 52 public static boolean isNotEmpty(String str) 53 { 54 return StringUtils.isNotEmpty(str); 55 } 56 57 59 public static String stripJsessionId( String url ) 60 { 61 int startPos = url.indexOf(";jsessionid="); 63 if ( startPos != -1 ) 64 { 65 int endPos = url.indexOf("?",startPos); 66 if ( endPos == -1 ) 67 { 68 url = url.substring(0,startPos); 69 } 70 else 71 { 72 url = url.substring(0,startPos) 73 + url.substring(endPos,url.length()); 74 } 75 } 76 return url; 77 } 78 79 84 public static String escapeHTML(String s) 85 { 86 return escapeHTML(s, true); 87 } 88 89 95 public static String escapeHTML(String s, boolean escapeAmpersand) 96 { 97 if (escapeAmpersand) 99 { 100 s = stringReplace(s, "&", "&"); 101 } 102 s = stringReplace(s, " ", " "); 103 s = stringReplace(s, "\"", """); 104 s = stringReplace(s, "<", "<"); 105 s = stringReplace(s, ">", ">"); 106 return s; 107 } 108 109 115 public static String removeHTML(String str) 116 { 117 return removeHTML(str, true); 118 } 119 120 129 public static String removeHTML(String str, boolean addSpace) 130 { 131 if (str == null) return ""; 132 StringBuffer ret = new StringBuffer (str.length()); 133 int start = 0; 134 int beginTag = str.indexOf("<"); 135 int endTag = 0; 136 if (beginTag == -1) 137 return str; 138 139 while (beginTag >= start) 140 { 141 if (beginTag > 0) 142 { 143 ret.append(str.substring(start, beginTag)); 144 145 if (addSpace) ret.append(" "); 147 } 148 endTag = str.indexOf(">", beginTag); 149 150 if (endTag > -1) 152 { 153 start = endTag + 1; 154 beginTag = str.indexOf("<", start); 155 } 156 else 158 { 159 ret.append(str.substring(beginTag)); 160 break; 161 } 162 } 163 if (endTag > -1 && endTag + 1 < str.length()) 165 { 166 ret.append(str.substring(endTag + 1)); 167 } 168 return ret.toString().trim(); 169 } 170 171 176 public static String removeAndEscapeHTML( String s ) 177 { 178 if ( s==null ) return ""; 179 else return Utilities.escapeHTML( Utilities.removeHTML(s) ); 180 } 181 182 186 public static String autoformat(String s) 187 { 188 String ret = StringUtils.replace(s, "\n", "<br />"); 189 return ret; 190 } 191 192 196 public static String formatIso8601Date(Date d) 197 { 198 return DateUtil.formatIso8601(d); 199 } 200 201 205 public static String formatIso8601Day(Date d) 206 { 207 return DateUtil.formatIso8601Day(d); 208 } 209 210 214 public static String formatRfc822Date(Date date) 215 { 216 return DateUtil.formatRfc822(date); 217 } 218 219 223 public static String format8charsDate(Date date) 224 { 225 return DateUtil.format8chars(date); 226 } 227 228 232 public static String replaceNonAlphanumeric(String str) 233 { 234 return replaceNonAlphanumeric(str, '_'); 235 } 236 237 242 public static String replaceNonAlphanumeric(String str, char subst) 243 { 244 StringBuffer ret = new StringBuffer (str.length()); 245 char[] testChars = str.toCharArray(); 246 for (int i = 0; i < testChars.length; i++) 247 { 248 if (Character.isLetterOrDigit(testChars[i])) 249 { 250 ret.append(testChars[i]); 251 } 252 else 253 { 254 ret.append( subst ); 255 } 256 } 257 return ret.toString(); 258 } 259 260 264 public static String removeNonAlphanumeric(String str) 265 { 266 StringBuffer ret = new StringBuffer (str.length()); 267 char[] testChars = str.toCharArray(); 268 for (int i = 0; i < testChars.length; i++) 269 { 270 if (Character.isLetterOrDigit(testChars[i]) || 272 testChars[i] == '.') 273 { 274 ret.append(testChars[i]); 275 } 276 } 277 return ret.toString(); 278 } 279 280 285 public static String stringArrayToString(String [] stringArray, String delim) 286 { 287 String ret = ""; 288 for (int i = 0; i < stringArray.length; i++) 289 { 290 if (ret.length() > 0) 291 ret = ret + delim + stringArray[i]; 292 else 293 ret = stringArray[i]; 294 } 295 return ret; 296 } 297 298 302 public static String stringReplace(String str, String str1, String str2) 303 { 304 String ret = StringUtils.replace(str,str1,str2); 305 return ret; 306 } 307 308 316 public static String stringReplace( 317 String str, 318 String str1, 319 String str2, 320 int maxCount) 321 { 322 String ret = StringUtils.replace(str,str1,str2,maxCount); 323 return ret; 324 } 325 326 328 public static String [] stringToStringArray(String instr, String delim) 329 throws NoSuchElementException , NumberFormatException  330 { 331 StringTokenizer toker = new StringTokenizer (instr, delim); 332 String stringArray[] = new String [toker.countTokens()]; 333 int i = 0; 334 335 while (toker.hasMoreTokens()) 336 { 337 stringArray[i++] = toker.nextToken(); 338 } 339 return stringArray; 340 } 341 342 344 public static int[] stringToIntArray(String instr, String delim) 345 throws NoSuchElementException , NumberFormatException  346 { 347 StringTokenizer toker = new StringTokenizer (instr, delim); 348 int intArray[] = new int[toker.countTokens()]; 349 int i = 0; 350 351 while (toker.hasMoreTokens()) 352 { 353 String sInt = toker.nextToken(); 354 int nInt = Integer.parseInt(sInt); 355 intArray[i++] = new Integer (nInt).intValue(); 356 } 357 return intArray; 358 } 359 360 362 public static String intArrayToString(int[] intArray) 363 { 364 String ret = ""; 365 for (int i = 0; i < intArray.length; i++) 366 { 367 if (ret.length() > 0) 368 ret = ret + "," + Integer.toString(intArray[i]); 369 else 370 ret = Integer.toString(intArray[i]); 371 } 372 return ret; 373 } 374 375 public static void copyFile(File from, File to) throws IOException  377 { 378 InputStream in = null; 379 OutputStream out = null; 380 381 try 382 { 383 in = new FileInputStream (from); 384 } 385 catch (IOException ex) 386 { 387 throw new IOException ( 388 "Utilities.copyFile: opening input stream '" 389 + from.getPath() 390 + "', " 391 + ex.getMessage()); 392 } 393 394 try 395 { 396 out = new FileOutputStream (to); 397 } 398 catch (Exception ex) 399 { 400 try 401 { 402 in.close(); 403 } 404 catch (IOException ex1) 405 { 406 } 407 throw new IOException ( 408 "Utilities.copyFile: opening output stream '" 409 + to.getPath() 410 + "', " 411 + ex.getMessage()); 412 } 413 414 copyInputToOutput(in, out, from.length()); 415 } 416 417 422 public static void copyInputToOutput( 423 InputStream input, 424 OutputStream output, 425 long byteCount) 426 throws IOException  427 { 428 int bytes; 429 long length; 430 431 BufferedInputStream in = new BufferedInputStream (input); 432 BufferedOutputStream out = new BufferedOutputStream (output); 433 434 byte[] buffer; 435 buffer = new byte[8192]; 436 437 for (length = byteCount; length > 0;) 438 { 439 bytes = (int) (length > 8192 ? 8192 : length); 440 441 try 442 { 443 bytes = in.read(buffer, 0, bytes); 444 } 445 catch (IOException ex) 446 { 447 try 448 { 449 in.close(); 450 out.close(); 451 } 452 catch (IOException ex1) 453 { 454 } 455 throw new IOException ( 456 "Reading input stream, " + ex.getMessage()); 457 } 458 459 if (bytes < 0) 460 break; 461 462 length -= bytes; 463 464 try 465 { 466 out.write(buffer, 0, bytes); 467 } 468 catch (IOException ex) 469 { 470 try 471 { 472 in.close(); 473 out.close(); 474 } 475 catch (IOException ex1) 476 { 477 } 478 throw new IOException ( 479 "Writing output stream, " + ex.getMessage()); 480 } 481 } 482 483 try 484 { 485 in.close(); 486 out.close(); 487 } 488 catch (IOException ex) 489 { 490 throw new IOException ("Closing file streams, " + ex.getMessage()); 491 } 492 } 493 494 public static void copyInputToOutput( 496 InputStream input, 497 OutputStream output) 498 throws IOException  499 { 500 BufferedInputStream in = new BufferedInputStream (input); 501 BufferedOutputStream out = new BufferedOutputStream (output); 502 byte buffer[] = new byte[8192]; 503 for (int count = 0; count != -1;) 504 { 505 count = in.read(buffer, 0, 8192); 506 if (count != -1) 507 out.write(buffer, 0, count); 508 } 509 510 try 511 { 512 in.close(); 513 out.close(); 514 } 515 catch (IOException ex) 516 { 517 throw new IOException ("Closing file streams, " + ex.getMessage()); 518 } 519 } 520 521 532 public static String encodePassword(String password, String algorithm) 533 { 534 byte[] unencodedPassword = password.getBytes(); 535 536 MessageDigest md = null; 537 538 try 539 { 540 md = MessageDigest.getInstance(algorithm); 542 } 543 catch (Exception e) 544 { 545 mLogger.error("Exception: " + e); 546 return password; 547 } 548 549 md.reset(); 550 551 md.update(unencodedPassword); 554 555 byte[] encodedPassword = md.digest(); 557 558 StringBuffer buf = new StringBuffer (); 559 560 for (int i = 0; i < encodedPassword.length; i++) 561 { 562 if ((encodedPassword[i] & 0xff) < 0x10) 563 { 564 buf.append("0"); 565 } 566 567 buf.append(Long.toString(encodedPassword[i] & 0xff, 16)); 568 } 569 570 return buf.toString(); 571 } 572 573 584 public static String encodeString(String str) throws IOException 585 { 586 sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder(); 587 String encodedStr = encoder.encodeBuffer(str.getBytes()); 588 589 return (encodedStr.trim()); 590 } 591 592 599 public static String decodeString(String str) throws IOException 600 { 601 sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder(); 602 String value = new String (dec.decodeBuffer(str)); 603 604 return (value); 605 } 606 607 610 public static String truncate( 611 String str, int lower, int upper, String appendToEnd) 612 { 613 String str2 = removeHTML(str, false); 615 616 if (upper < lower) 618 { 619 upper = lower; 620 } 621 622 if(str2.length() > upper) 625 { 626 int loc; 628 629 loc = str2.lastIndexOf(' ', upper); 631 632 if(loc >= lower) 634 { 635 str2 = str2.substring(0, loc); 637 } 638 else 639 { 640 str2 = str2.substring(0, upper); 642 loc = upper; 643 } 644 645 str2 = str2 + appendToEnd; 647 } 648 649 return str2; 650 } 651 652 664 public static String truncateNicely(String str, int lower, int upper, String appendToEnd) 665 { 666 String str2 = removeHTML(str, false); 668 boolean diff = (str2.length() < str.length()); 669 670 if(upper < lower) { 672 upper = lower; 673 } 674 675 if(str2.length() > upper) { 678 int loc; 680 681 loc = str2.lastIndexOf(' ', upper); 683 684 if(loc >= lower) { 686 str2 = str2.substring(0, loc); 688 } else { 689 str2 = str2.substring(0, upper); 691 loc = upper; 692 } 693 694 if (diff) 696 { 697 698 loc = str2.lastIndexOf(' ', loc); 700 701 String str3 = str2.substring(loc+1); 703 704 loc = str.indexOf(str3, loc) + str3.length(); 706 707 str2 = str.substring(0, loc); 709 710 str3 = extractHTML(str.substring(loc)); 712 713 717 str = str2 + appendToEnd + str3; 720 } 721 else 722 { 723 str = str2 + appendToEnd; 725 } 726 727 } 728 729 return str; 730 } 731 732 public static String truncateText(String str, int lower, int upper, String appendToEnd) 733 { 734 String str2 = removeHTML(str, false); 736 boolean diff = (str2.length() < str.length()); 737 738 if(upper < lower) { 740 upper = lower; 741 } 742 743 if(str2.length() > upper) { 746 int loc; 748 749 loc = str2.lastIndexOf(' ', upper); 751 752 if(loc >= lower) { 754 str2 = str2.substring(0, loc); 756 } else { 757 str2 = str2.substring(0, upper); 759 loc = upper; 760 } 761 str = str2 + appendToEnd; 763 } 764 return str; 765 } 766 767 771 private static String stripLineBreaks(String str) 772 { 773 str = str.replaceAll("<br>", ""); 775 str = str.replaceAll("<br/>", ""); 776 str = str.replaceAll("<br />", ""); 777 str = str.replaceAll("<p></p>", ""); 778 str = str.replaceAll("<p/>",""); 779 str = str.replaceAll("<p />",""); 780 return str; 781 } 782 783 795 private static String removeVisibleHTMLTags(String str) 796 { 797 str = stripLineBreaks(str); 798 StringBuffer result = new StringBuffer (str); 799 StringBuffer lcresult = new StringBuffer (str.toLowerCase()); 800 801 String [] visibleTags = {"<img"}; int stringIndex; 804 for ( int j = 0 ; j < visibleTags.length ; j++ ) { 805 while ( (stringIndex = lcresult.indexOf(visibleTags[j])) != -1 ) { 806  
|