1 package org.apache.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.URLDecoder ; 13 import java.net.URLEncoder ; 14 import java.security.MessageDigest ; 15 import java.util.NoSuchElementException ; 16 import java.util.StringTokenizer ; 17 18 import org.apache.commons.lang.StringEscapeUtils; 19 import org.apache.commons.lang.StringUtils; 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import sun.misc.BASE64Decoder; 24 import sun.misc.BASE64Encoder; 25 26 29 public class Utilities { 30 31 private static Log mLogger = LogFactory.getLog(Utilities.class); 32 33 35 public static String stripJsessionId( String url ) { 36 int startPos = url.indexOf(";jsessionid="); 38 if ( startPos != -1 ) { 39 int endPos = url.indexOf("?",startPos); 40 if ( endPos == -1 ) { 41 url = url.substring(0,startPos); 42 } else { 43 url = url.substring(0,startPos) 44 + url.substring(endPos,url.length()); 45 } 46 } 47 return url; 48 } 49 50 55 public static String escapeHTML(String s) { 56 return escapeHTML(s, true); 57 } 58 59 65 public static String escapeHTML(String s, boolean escapeAmpersand) { 66 if (escapeAmpersand) { 68 s = StringUtils.replace(s, "&", "&"); 69 } 70 s = StringUtils.replace(s, " ", " "); 71 s = StringUtils.replace(s, "\"", """); 72 s = StringUtils.replace(s, "<", "<"); 73 s = StringUtils.replace(s, ">", ">"); 74 return s; 75 } 76 77 public static String unescapeHTML(String str) { 78 return StringEscapeUtils.unescapeHtml(str); 79 } 80 81 87 public static String removeHTML(String str) { 88 return removeHTML(str, true); 89 } 90 91 100 public static String removeHTML(String str, boolean addSpace) { 101 if (str == null) return ""; 102 StringBuffer ret = new StringBuffer (str.length()); 103 int start = 0; 104 int beginTag = str.indexOf("<"); 105 int endTag = 0; 106 if (beginTag == -1) 107 return str; 108 109 while (beginTag >= start) { 110 if (beginTag > 0) { 111 ret.append(str.substring(start, beginTag)); 112 113 if (addSpace) ret.append(" "); 115 } 116 endTag = str.indexOf(">", beginTag); 117 118 if (endTag > -1) { 120 start = endTag + 1; 121 beginTag = str.indexOf("<", start); 122 } 123 else { 125 ret.append(str.substring(beginTag)); 126 break; 127 } 128 } 129 if (endTag > -1 && endTag + 1 < str.length()) { 131 ret.append(str.substring(endTag + 1)); 132 } 133 return ret.toString().trim(); 134 } 135 136 141 public static String removeAndEscapeHTML( String s ) { 142 if ( s==null ) return ""; 143 else return Utilities.escapeHTML( Utilities.removeHTML(s) ); 144 } 145 146 150 public static String autoformat(String s) { 151 String ret = StringUtils.replace(s, "\n", "<br />"); 152 return ret; 153 } 154 155 159 public static String replaceNonAlphanumeric(String str) { 160 return replaceNonAlphanumeric(str, '_'); 161 } 162 163 168 public static String replaceNonAlphanumeric(String str, char subst) { 169 StringBuffer ret = new StringBuffer (str.length()); 170 char[] testChars = str.toCharArray(); 171 for (int i = 0; i < testChars.length; i++) { 172 if (Character.isLetterOrDigit(testChars[i])) { 173 ret.append(testChars[i]); 174 } else { 175 ret.append( subst ); 176 } 177 } 178 return ret.toString(); 179 } 180 181 185 public static String removeNonAlphanumeric(String str) { 186 StringBuffer ret = new StringBuffer (str.length()); 187 char[] testChars = str.toCharArray(); 188 for (int i = 0; i < testChars.length; i++) { 189 if (Character.isLetterOrDigit(testChars[i]) || 191 testChars[i] == '.') { 192 ret.append(testChars[i]); 193 } 194 } 195 return ret.toString(); 196 } 197 198 204 public static String stringArrayToString(String [] stringArray, String delim) { 205 String ret = ""; 206 for (int i = 0; i < stringArray.length; i++) { 207 if (ret.length() > 0) 208 ret = ret + delim + stringArray[i]; 209 else 210 ret = stringArray[i]; 211 } 212 return ret; 213 } 214 215 217 public static String [] stringToStringArray(String instr, String delim) 218 throws NoSuchElementException , NumberFormatException { 219 StringTokenizer toker = new StringTokenizer (instr, delim); 220 String stringArray[] = new String [toker.countTokens()]; 221 int i = 0; 222 223 while (toker.hasMoreTokens()) { 224 stringArray[i++] = toker.nextToken(); 225 } 226 return stringArray; 227 } 228 229 231 public static int[] stringToIntArray(String instr, String delim) 232 throws NoSuchElementException , NumberFormatException { 233 StringTokenizer toker = new StringTokenizer (instr, delim); 234 int intArray[] = new int[toker.countTokens()]; 235 int i = 0; 236 237 while (toker.hasMoreTokens()) { 238 String sInt = toker.nextToken(); 239 int nInt = Integer.parseInt(sInt); 240 intArray[i++] = new Integer (nInt).intValue(); 241 } 242 return intArray; 243 } 244 245 247 public static String intArrayToString(int[] intArray) { 248 String ret = ""; 249 for (int i = 0; i < intArray.length; i++) { 250 if (ret.length() > 0) 251 ret = ret + "," + Integer.toString(intArray[i]); 252 else 253 ret = Integer.toString(intArray[i]); 254 } 255 return ret; 256 } 257 258 public static void copyFile(File from, File to) throws IOException { 260 InputStream in = null; 261 OutputStream out = null; 262 263 try { 264 in = new FileInputStream (from); 265 } catch (IOException ex) { 266 throw new IOException ( 267 "Utilities.copyFile: opening input stream '" 268 + from.getPath() 269 + "', " 270 + ex.getMessage()); 271 } 272 273 try { 274 out = new FileOutputStream (to); 275 } catch (Exception ex) { 276 try { 277 in.close(); 278 } catch (IOException ex1) { 279 } 280 throw new IOException ( 281 "Utilities.copyFile: opening output stream '" 282 + to.getPath() 283 + "', " 284 + ex.getMessage()); 285 } 286 287 copyInputToOutput(in, out, from.length()); 288 } 289 290 295 public static void copyInputToOutput( 296 InputStream input, 297 OutputStream output, 298 long byteCount) 299 throws IOException { 300 int bytes; 301 long length; 302 303 BufferedInputStream in = new BufferedInputStream (input); 304 BufferedOutputStream out = new BufferedOutputStream (output); 305 306 byte[] buffer; 307 buffer = new byte[8192]; 308 309 for (length = byteCount; length > 0;) { 310 bytes = (int) (length > 8192 ? 8192 : length); 311 312 try { 313 bytes = in.read(buffer, 0, bytes); 314 } catch (IOException ex) { 315 try { 316 in.close(); 317 out.close(); 318 } catch (IOException ex1) { 319 } 320 throw new IOException ( 321 "Reading input stream, " + ex.getMessage()); 322 } 323 324 if (bytes < 0) 325 break; 326 327 length -= bytes; 328 329 try { 330 out.write(buffer, 0, bytes); 331 } catch (IOException ex) { 332 try { 333 in.close(); 334 out.close(); 335 } catch (IOException ex1) { 336 } 337 throw new IOException ( 338 "Writing output stream, " + ex.getMessage()); 339 } 340 } 341 342 try { 343 in.close(); 344 out.close(); 345 } catch (IOException ex) { 346 throw new IOException ("Closing file streams, " + ex.getMessage()); 347 } 348 } 349 350 public static void copyInputToOutput( 352 InputStream input, 353 OutputStream output) 354 throws IOException { 355 BufferedInputStream in = new BufferedInputStream (input); 356 BufferedOutputStream out = new BufferedOutputStream (output); 357 byte buffer[] = new byte[8192]; 358 for (int count = 0; count != -1;) { 359 count = in.read(buffer, 0, 8192); 360 if (count != -1) 361 out.write(buffer, 0, count); 362 } 363 364 try { 365 in.close(); 366 out.close(); 367 } catch (IOException ex) { 368 throw new IOException ("Closing file streams, " + ex.getMessage()); 369 } 370 } 371 372 383 public static String encodePassword(String password, String algorithm) { 384 byte[] unencodedPassword = password.getBytes(); 385 386 MessageDigest md = null; 387 388 try { 389 md = MessageDigest.getInstance(algorithm); 391 } catch (Exception e) { 392 mLogger.error("Exception: " + e); 393 return password; 394 } 395 396 md.reset(); 397 398 md.update(unencodedPassword); 401 402 byte[] encodedPassword = md.digest(); 404 405 StringBuffer buf = new StringBuffer (); 406 407 for (int i = 0; i < encodedPassword.length; i++) { 408 if ((encodedPassword[i] & 0xff) < 0x10) { 409 buf.append("0"); 410 } 411 412 buf.append(Long.toString(encodedPassword[i] & 0xff, 16)); 413 } 414 415 return buf.toString(); 416 } 417 418 429 public static String encodeString(String str) throws IOException { 430 BASE64Encoder encoder = new BASE64Encoder(); 431 String encodedStr = encoder.encodeBuffer(str.getBytes()); 432 433 return (encodedStr.trim()); 434 } 435 436 443 public static String decodeString(String str) throws IOException { 444 BASE64Decoder dec = new BASE64Decoder(); 445 String value = new String (dec.decodeBuffer(str)); 446 447 return (value); 448 } 449 450 453 public static String truncate( 454 String str, int lower, int upper, String appendToEnd) { 455 String str2 = removeHTML(str, false); 457 458 if (upper < lower) { 460 upper = lower; 461 } 462 463 if(str2.length() > upper) { 466 int loc; 468 469 loc = str2.lastIndexOf(' ', upper); 471 472 if(loc >= lower) { 474 str2 = str2.substring(0, loc); 476 } else { 477 str2 = str2.substring(0, upper); 479 loc = upper; 480 } 481 482 str2 = str2 + appendToEnd; 484 } 485 486 return str2; 487 } 488 489 501 public static String truncateNicely(String str, int lower, int upper, String appendToEnd) { 502 String str2 = removeHTML(str, false); 504 boolean diff = (str2.length() < str.length()); 505 506 if(upper < lower) { 508 upper = lower; 509 } 510 511 if(str2.length() > upper) { 514 int loc; 516 517 loc = str2.lastIndexOf(' ', upper); 519 520 if(loc >= lower) { 522 str2 = str2.substring(0, loc); 524 } else { 525 str2 = str2.substring(0, upper); 527 loc = upper; 528 } 529 530 if (diff) { 532 533 loc = str2.lastIndexOf(' ', loc); 535 536 String str3 = str2.substring(loc+1); 538 539 loc = str.indexOf(str3, loc) + str3.length(); 541 542 str2 = str.substring(0, loc); 544 545 str3 = extractHTML(str.substring(loc)); 547 548 552 str = str2 + appendToEnd + str3; 555 } else { 556 str = str2 + appendToEnd; 558 } 559 560 } 561 562 return str; 563 } 564 565 public static String truncateText(String str, int lower, int upper, String appendToEnd) { 566 String str2 = removeHTML(str, false); 568 boolean diff = (str2.length() < str.length()); 569 570 if(upper < lower) { 572 upper = lower; 573 } 574 575 if(str2.length() > upper) { 578 int loc; 580 581 loc = str2.lastIndexOf(' ', upper); 583 584 if(loc >= lower) { 586 str2 = str2.substring(0, loc); 588 } else { 589 str2 = str2.substring(0, upper); 591 loc = upper; 592 } 593 str = str2 + appendToEnd; 595 } 596 return str; 597 } 598 599 603 private static String stripLineBreaks(String str) { 604 str = str.replaceAll("<br>", ""); 606 str = str.replaceAll("<br/>", ""); 607 str = str.replaceAll("<br />", ""); 608 str = str.replaceAll("<p></p>", ""); 609 str = str.replaceAll("<p/>",""); 610 str = str.replaceAll("<p />",""); 611 return str; 612 } 613 614 626 private static String removeVisibleHTMLTags(String str) { 627 str = stripLineBreaks(str); 628 StringBuffer result = new StringBuffer (str); 629 StringBuffer lcresult = new StringBuffer (str.toLowerCase()); 630 631 String [] visibleTags = {"<img"}; int stringIndex; 634 for ( int j = 0 ; j < visibleTags.length ; j++ ) { 635 while ( (stringIndex = lcresult.indexOf(visibleTags[j])) != -1 ) { 636 if ( visibleTags[j].endsWith(">") ) { 637 result.delete(stringIndex, stringIndex+visibleTags[j].length() ); 638 lcresult.delete(stringIndex, stringIndex+visibleTags[j].length() ); 639 } else { 640 int endIndex = result.indexOf(">", stringIndex); 642 if (endIndex > -1) { 643 result.delete(stringIndex, endIndex + 1 ); 646 lcresult.delete(stringIndex, endIndex + 1 ); 647 } 648 } 649 } 650 } 651 652 String [] openCloseTags = {"li", "a", "div", "h1", "h2", "h3", "h4"}; for (int j = 0; j < openCloseTags.length; j++) { 656 String closeTag = "</"+openCloseTags[j]+">"; 658 int lastStringIndex = 0; 659 while ( (stringIndex = lcresult.indexOf( "<"+openCloseTags[j], lastStringIndex)) > -1) { 660 lastStringIndex = stringIndex; 661 int endIndex = lcresult.indexOf(closeTag, stringIndex); 663 if (endIndex > -1) { 664 result.delete(stringIndex, endIndex+closeTag.length()); 666 lcresult.delete(stringIndex, endIndex+closeTag.length()); 667 } else { 668 endIndex = lcresult.indexOf(">", stringIndex); 670 int nextStart = lcresult.indexOf("<", stringIndex+1); 671 if (endIndex > stringIndex && lcresult.charAt(endIndex-1) == '/' && (endIndex < nextStart || nextStart == -1)) { 672 result.delete(stringIndex, endIndex + 1); 674 lcresult.delete(stringIndex, endIndex + 1); 675 676 } 677 } 678 } 679 } 680 681 return result.toString(); 682 } 683 684 689 public static String extractHTML(String str) { 690 if (str == null) return ""; 691 StringBuffer ret = new StringBuffer (str.length()); 692 int start = 0; 693 int beginTag = str.indexOf("<"); 694 int endTag = 0; 695 if (beginTag == -1) 696 return str; 697 698 while (beginTag >= start) { 699 endTag = str.indexOf(">", beginTag); 700 701 if (endTag > -1) { 703 ret.append( str.substring(beginTag, endTag+1) ); 704 705 start = endTag + 1; 707 beginTag = str.indexOf("<", start); 708 } 709 else { 711 break; 712 } 713 } 714 return ret.toString(); 715 } 716 717 718 public static String hexEncode(String str) { 719 if (StringUtils.isEmpty(str)) return str; 720 721 return RegexUtil.encode(str); 722 } 723 724 public static String encodeEmail(String str) { 725 return str!=null ? RegexUtil.encodeEmail(str) : null; 726 } 727 728 733 public static final String encode(String s) { 734 try { 735 if (s != null) 736 return URLEncoder.encode(s, "UTF-8"); 737 else 738 return s; 739 } catch (UnsupportedEncodingException e) { 740 return s; 742 } 743 } 744 745 750 public static final String decode(String s) { 751 try { 752 if (s != null) 753 return URLDecoder.decode(s, "UTF-8"); 754 else 755 return s; 756 } catch (UnsupportedEncodingException e) { 757 return s; 759 } 760 } 761 762 766 public static int stringToInt(String string) { 767 try { 768 return Integer.valueOf(string).intValue(); 769 } catch (NumberFormatException e) { 770 mLogger.debug("Invalid Integer:" + string); 771 } 772 return 0; 773 } 774 775 778 public static String toBase64(byte[] aValue) { 779 780 final String m_strBase64Chars = 781 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 782 783 int byte1; 784 int byte2; 785 int byte3; 786 int iByteLen = aValue.length; 787 StringBuffer tt = new StringBuffer (); 788 789 for (int i = 0; i < iByteLen; i += 3) { 790 boolean bByte2 = (i + 1) < iByteLen; 791 boolean bByte3 = (i + 2) < iByteLen; 792 byte1 = aValue[i] & 0xFF; 793 byte2 = (bByte2) ? (aValue[i + 1] & 0xFF) : 0; 794 byte3 = (bByte3) ? (aValue[i + 2] & 0xFF) : 0; 795 796 tt.append(m_strBase64Chars.charAt(byte1 / 4)); 797 tt.append(m_strBase64Chars.charAt((byte2 / 16) + ((byte1 & 0x3) * 16))); 798 tt.append(((bByte2) ? m_strBase64Chars.charAt((byte3 / 64) + ((byte2 & 0xF) * 4)) : '=')); 799 tt.append(((bByte3) ? m_strBase64Chars.charAt(byte3 & 0x3F) : '=')); 800 } 801 802 return tt.toString(); 803 } 804 } 805 | Popular Tags |