1 18 package org.apache.roller.ui.rendering.velocity.deprecated; 19 20 import java.io.IOException ; 21 import java.io.UnsupportedEncodingException ; 22 import java.net.URLDecoder ; 23 import java.net.URLEncoder ; 24 import java.text.SimpleDateFormat ; 25 import java.util.Date ; 26 import java.util.regex.Matcher ; 27 import java.util.regex.Pattern ; 28 import org.apache.commons.lang.StringEscapeUtils; 29 import org.apache.commons.lang.StringUtils; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.roller.util.DateUtil; 33 import org.apache.roller.util.RegexUtil; 34 import org.apache.roller.util.Utilities; 35 36 37 41 public class OldUtilities { 42 43 44 private static Log mLogger = LogFactory.getLog(OldUtilities.class); 45 46 private static Pattern mLinkPattern = 47 Pattern.compile("<a HREF=.*?>", Pattern.CASE_INSENSITIVE); 48 private static final Pattern OPENING_B_TAG_PATTERN = 49 Pattern.compile("<b>", Pattern.CASE_INSENSITIVE); 50 private static final Pattern CLOSING_B_TAG_PATTERN = 51 Pattern.compile("</b>", Pattern.CASE_INSENSITIVE); 52 private static final Pattern OPENING_I_TAG_PATTERN = 53 Pattern.compile("<i>", Pattern.CASE_INSENSITIVE); 54 private static final Pattern CLOSING_I_TAG_PATTERN = 55 Pattern.compile("</i>", Pattern.CASE_INSENSITIVE); 56 private static final Pattern OPENING_BLOCKQUOTE_TAG_PATTERN = 57 Pattern.compile("<blockquote>", Pattern.CASE_INSENSITIVE); 58 private static final Pattern CLOSING_BLOCKQUOTE_TAG_PATTERN = 59 Pattern.compile("</blockquote>", Pattern.CASE_INSENSITIVE); 60 private static final Pattern BR_TAG_PATTERN = 61 Pattern.compile("<br */*>", Pattern.CASE_INSENSITIVE); 62 private static final Pattern OPENING_P_TAG_PATTERN = 63 Pattern.compile("<p>", Pattern.CASE_INSENSITIVE); 64 private static final Pattern CLOSING_P_TAG_PATTERN = 65 Pattern.compile("</p>", Pattern.CASE_INSENSITIVE); 66 private static final Pattern OPENING_PRE_TAG_PATTERN = 67 Pattern.compile("<pre>", Pattern.CASE_INSENSITIVE); 68 private static final Pattern CLOSING_PRE_TAG_PATTERN = 69 Pattern.compile("</pre>", Pattern.CASE_INSENSITIVE); 70 private static final Pattern OPENING_UL_TAG_PATTERN = 71 Pattern.compile("<ul>", Pattern.CASE_INSENSITIVE); 72 private static final Pattern CLOSING_UL_TAG_PATTERN = 73 Pattern.compile("</ul>", Pattern.CASE_INSENSITIVE); 74 private static final Pattern OPENING_OL_TAG_PATTERN = 75 Pattern.compile("<ol>", Pattern.CASE_INSENSITIVE); 76 private static final Pattern CLOSING_OL_TAG_PATTERN = 77 Pattern.compile("</ol>", Pattern.CASE_INSENSITIVE); 78 private static final Pattern OPENING_LI_TAG_PATTERN = 79 Pattern.compile("<li>", Pattern.CASE_INSENSITIVE); 80 private static final Pattern CLOSING_LI_TAG_PATTERN = 81 Pattern.compile("</li>", Pattern.CASE_INSENSITIVE); 82 private static final Pattern CLOSING_A_TAG_PATTERN = 83 Pattern.compile("</a>", Pattern.CASE_INSENSITIVE); 84 private static final Pattern OPENING_A_TAG_PATTERN = 85 Pattern.compile("<a HREF=.*?>", Pattern.CASE_INSENSITIVE); 86 private static final Pattern QUOTE_PATTERN = 87 Pattern.compile(""", Pattern.CASE_INSENSITIVE); 88 89 public static boolean isEmpty(String str) { 90 if (str == null) return true; 91 return "".equals(str.trim()); 92 } 93 94 public static boolean isNotEmpty(String str) { 95 return !isEmpty(str); 96 } 97 98 public static String [] split(String str1, String str2) { 99 return StringUtils.split(str1, str2); 100 } 101 102 public static String replace(String src, String target, String rWith) { 103 return StringUtils.replace(src, target, rWith); 104 } 105 106 public static String replace(String src, String target, String rWith, int maxCount) { 107 return StringUtils.replace(src, target, rWith, maxCount); 108 } 109 110 public static boolean equals(String str1, String str2) { 111 return StringUtils.equals(str1, str2); 112 } 113 114 public static boolean isAlphanumeric(String str) { 115 return StringUtils.isAlphanumeric(str); 116 } 117 118 public static String [] stripAll(String [] strs) { 119 return StringUtils.stripAll(strs); 120 } 121 122 public static String left(String str, int length) { 123 return StringUtils.left(str, length); 124 } 125 126 public static String escapeHTML(String str) { 127 return StringEscapeUtils.escapeHtml(str); 128 } 129 130 public static String unescapeHTML(String str) { 131 return StringEscapeUtils.unescapeHtml(str); 132 } 133 134 139 public static String removeHTML(String str) { 140 return removeHTML(str, true); 141 } 142 143 148 public static String removeHTML(String str, boolean addSpace) { 149 return Utilities.removeHTML(str, addSpace); 150 } 151 152 155 public static String autoformat(String s) { 156 String ret = StringUtils.replace(s, "\n", "<br />"); 157 return ret; 158 } 159 160 163 public static Date getNow() { 164 return new Date (); 165 } 166 167 170 public static String formatDate(Date d, String fmt) { 171 SimpleDateFormat format = new SimpleDateFormat (fmt); 172 return format.format(d); 173 } 174 175 178 public static String formatIso8601Date(Date d) { 179 return DateUtil.formatIso8601(d); 180 } 181 182 185 public static String formatIso8601Day(Date d) { 186 return DateUtil.formatIso8601Day(d); 187 } 188 189 192 public static String formatRfc822Date(Date date) { 193 return DateUtil.formatRfc822(date); 194 } 195 196 199 public static String format8charsDate(Date date) { 200 return DateUtil.format8chars(date); 201 } 202 203 206 public static String truncate( 207 String str, int lower, int upper, String appendToEnd) { 208 String str2 = removeHTML(str, false); 210 211 if (upper < lower) { 213 upper = lower; 214 } 215 216 if(str2.length() > upper) { 219 int loc; 221 222 loc = str2.lastIndexOf(' ', upper); 224 225 if(loc >= lower) { 227 str2 = str2.substring(0, loc); 229 } else { 230 str2 = str2.substring(0, upper); 232 loc = upper; 233 } 234 235 str2 = str2 + appendToEnd; 237 } 238 239 return str2; 240 } 241 242 public static String truncateNicely(String str, int lower, int upper, String appendToEnd) { 243 return Utilities.truncateNicely(str, lower, upper, appendToEnd); 244 } 245 246 public static String truncateText(String str, int lower, int upper, String appendToEnd) { 247 String str2 = removeHTML(str, false); 249 boolean diff = (str2.length() < str.length()); 250 251 if(upper < lower) { 253 upper = lower; 254 } 255 256 if(str2.length() > upper) { 259 int loc; 261 262 loc = str2.lastIndexOf(' ', upper); 264 265 if(loc >= lower) { 267 str2 = str2.substring(0, loc); 269 } else { 270 str2 = str2.substring(0, upper); 272 loc = upper; 273 } 274 str = str2 + appendToEnd; 276 } 277 return str; 278 } 279 280 public static String hexEncode(String str) { 281 if (StringUtils.isEmpty(str)) return str; 282 283 return RegexUtil.encode(str); 284 } 285 286 public static String encodeEmail(String str) { 287 return str!=null ? RegexUtil.encodeEmail(str) : null; 288 } 289 290 295 public static final String encode(String s) { 296 try { 297 if (s != null) 298 return URLEncoder.encode(s, "UTF-8"); 299 else 300 return s; 301 } catch (UnsupportedEncodingException e) { 302 return s; 304 } 305 } 306 307 312 public static final String decode(String s) { 313 try { 314 if (s != null) 315 return URLDecoder.decode(s, "UTF-8"); 316 else 317 return s; 318 } catch (UnsupportedEncodingException e) { 319 return s; 321 } 322 } 323 324 327 public static String addNofollow(String html) { 328 if (html == null || html.length() == 0) { 329 return html; 330 } 331 Matcher m = mLinkPattern.matcher(html); 332 StringBuffer buf = new StringBuffer (); 333 while (m.find()) { 334 int start = m.start(); 335 int end = m.end(); 336 String link = html.substring(start, end); 337 buf.append(html.substring(0, start)); 338 if (link.indexOf("rel=\"nofollow\"") == -1) { 339 buf.append( 340 link.substring(0, link.length() - 1) + " rel=\"nofollow\">"); 341 } else { 342 buf.append(link); 343 } 344 html = html.substring(end, html.length()); 345 m = mLinkPattern.matcher(html); 346 } 347 buf.append(html); 348 return buf.toString(); 349 } 350 351 359 public static String transformToHTMLSubset(String s) { 360 361 if (s == null) { 362 return null; 363 } 364 365 s = replace(s, OPENING_B_TAG_PATTERN, "<b>"); 366 s = replace(s, CLOSING_B_TAG_PATTERN, "</b>"); 367 s = replace(s, OPENING_I_TAG_PATTERN, "<i>"); 368 s = replace(s, CLOSING_I_TAG_PATTERN, "</i>"); 369 s = replace(s, OPENING_BLOCKQUOTE_TAG_PATTERN, "<blockquote>"); 370 s = replace(s, CLOSING_BLOCKQUOTE_TAG_PATTERN, "</blockquote>"); 371 s = replace(s, BR_TAG_PATTERN, "<br />"); 372 s = replace(s, OPENING_P_TAG_PATTERN, "<p>"); 373 s = replace(s, CLOSING_P_TAG_PATTERN, "</p>"); 374 s = replace(s, OPENING_PRE_TAG_PATTERN, "<pre>"); 375 s = replace(s, CLOSING_PRE_TAG_PATTERN, "</pre>"); 376 s = replace(s, OPENING_UL_TAG_PATTERN, "<ul>"); 377 s = replace(s, CLOSING_UL_TAG_PATTERN, "</ul>"); 378 s = replace(s, OPENING_OL_TAG_PATTERN, "<ol>"); 379 s = replace(s, CLOSING_OL_TAG_PATTERN, "</ol>"); 380 s = replace(s, OPENING_LI_TAG_PATTERN, "<li>"); 381 s = replace(s, CLOSING_LI_TAG_PATTERN, "</li>"); 382 s = replace(s, QUOTE_PATTERN, "\""); 383 384 s = replace(s, CLOSING_A_TAG_PATTERN, "</a>"); 386 Matcher m = OPENING_A_TAG_PATTERN.matcher(s); 387 while (m.find()) { 388 int start = m.start(); 389 int end = m.end(); 390 String link = s.substring(start, end); 391 link = "<" + link.substring(4, link.length() - 4) + ">"; 392 s = s.substring(0, start) + link + s.substring(end, s.length()); 393 m = OPENING_A_TAG_PATTERN.matcher(s); 394 } 395 396 s = s.replaceAll("&lt;", "<"); 398 s = s.replaceAll("&gt;", ">"); 399 s = s.replaceAll("&#", "&#"); 400 401 return s; 402 } 403 404 private static String replace(String string, Pattern pattern, String replacement) { 405 Matcher m = pattern.matcher(string); 406 return m.replaceAll(replacement); 407 } 408 409 412 public static String toBase64(byte[] aValue) { 413 414 final String m_strBase64Chars = 415 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 416 417 int byte1; 418 int byte2; 419 int byte3; 420 int iByteLen = aValue.length; 421 StringBuffer tt = new StringBuffer (); 422 423 for (int i = 0; i < iByteLen; i += 3) { 424 boolean bByte2 = (i + 1) < iByteLen; 425 boolean bByte3 = (i + 2) < iByteLen; 426 byte1 = aValue[i] & 0xFF; 427 byte2 = (bByte2) ? (aValue[i + 1] & 0xFF) : 0; 428 byte3 = (bByte3) ? (aValue[i + 2] & 0xFF) : 0; 429 430 tt.append(m_strBase64Chars.charAt(byte1 / 4)); 431 tt.append(m_strBase64Chars.charAt((byte2 / 16) + ((byte1 & 0x3) * 16))); 432 tt.append(((bByte2) ? m_strBase64Chars.charAt((byte3 / 64) + ((byte2 & 0xF) * 4)) : '=')); 433 tt.append(((bByte3) ? m_strBase64Chars.charAt(byte3 & 0x3F) : '=')); 434 } 435 436 return tt.toString(); 437 } 438 439 440 446 public static String escapeHTML(String s, boolean escapeAmpersand) { 447 return Utilities.escapeHTML(s, escapeAmpersand); 448 } 449 450 454 public static String stringReplace(String str, String str1, String str2) { 455 String ret = StringUtils.replace(str,str1,str2); 456 return ret; 457 } 458 459 467 public static String stringReplace( 468 String str, 469 String str1, 470 String str2, 471 int maxCount) { 472 String ret = StringUtils.replace(str,str1,str2,maxCount); 473 return ret; 474 } 475 476 477 478 489 public static String encodeString(String str) throws IOException { 490 sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder(); 491 String encodedStr = encoder.encodeBuffer(str.getBytes()); 492 493 return (encodedStr.trim()); 494 } 495 496 503 public static String decodeString(String str) throws IOException { 504 sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder(); 505 String value = new String (dec.decodeBuffer(str)); 506 507 return (value); 508 } 509 510 514 private static String stripLineBreaks(String str) { 515 str = str.replaceAll("<br>", ""); 517 str = str.replaceAll("<br/>", ""); 518 str = str.replaceAll("<br />", ""); 519 str = str.replaceAll("<p></p>", ""); 520 str = str.replaceAll("<p/>",""); 521 str = str.replaceAll("<p />",""); 522 return str; 523 } 524 525 537 private static String removeVisibleHTMLTags(String str) { 538 str = stripLineBreaks(str); 539 StringBuffer result = new StringBuffer (str); 540 StringBuffer lcresult = new StringBuffer (str.toLowerCase()); 541 542 String [] visibleTags = {"<img"}; int stringIndex; 545 for ( int j = 0 ; j < visibleTags.length ; j++ ) { 546 while ( (stringIndex = lcresult.indexOf(visibleTags[j])) != -1 ) { 547 if ( visibleTags[j].endsWith(">") ) { 548 result.delete(stringIndex, stringIndex+visibleTags[j].length() ); 549 lcresult.delete(stringIndex, stringIndex+visibleTags[j].length() ); 550 } else { 551 int endIndex = result.indexOf(">", stringIndex); 553 if (endIndex > -1) { 554 result.delete(stringIndex, endIndex + 1 ); 557 lcresult.delete(stringIndex, endIndex + 1 ); 558 } 559 } 560 } 561 } 562 563 String [] openCloseTags = {"li", "a", "div", "h1", "h2", "h3", "h4"}; for (int j = 0; j < openCloseTags.length; j++) { 567 String closeTag = "</"+openCloseTags[j]+">"; 569 int lastStringIndex = 0; 570 while ( (stringIndex = lcresult.indexOf( "<"+openCloseTags[j], lastStringIndex)) > -1) { 571 lastStringIndex = stringIndex; 572 int endIndex = lcresult.indexOf(closeTag, stringIndex); 574 if (endIndex > -1) { 575 result.delete(stringIndex, endIndex+closeTag.length()); 577 lcresult.delete(stringIndex, endIndex+closeTag.length()); 578 } else { 579 endIndex = lcresult.indexOf(">", stringIndex); 581 int nextStart = lcresult.indexOf("<", stringIndex+1); 582 if (endIndex > stringIndex && lcresult.charAt(endIndex-1) == '/' && (endIndex < nextStart || nextStart == -1)) { 583 result.delete(stringIndex, endIndex + 1); 585 lcresult.delete(stringIndex, endIndex + 1); 586 587 } 588 } 589 } 590 } 591 592 return result.toString(); 593 } 594 595 596 605 public static final String charToHTML(char ch, boolean xml) { 606 int c; 607 608 if (ch == '<') { 610 return ("<"); 611 } 612 613 else if (ch == '>') { 615 return (">"); 616 } 617 618 else if (ch == '&') { 620 return ("&"); 621 } 622 623 631 else if (xml && (ch == '"')) { 633 return ("""); 634 } 635 636 else if (xml && (ch == '\'')) { 638 return ("'"); 639 } 640 641 else { 643 return (String.valueOf(ch)); 645 } 646 } 647 648 657 public static final String textToHTML(String text, boolean xml) { 658 if (text == null) return "null"; 659 final StringBuffer html = new StringBuffer (); 660 661 for (int i = 0; i < text.length(); i++) { 663 html.append(charToHTML(text.charAt(i), xml)); 665 } 666 667 return html.toString(); 669 } 670 671 678 public static final String textToHTML(String text) { 679 return textToHTML(text, false); 680 } 681 682 689 public static final String textToXML(String text) { 690 return textToHTML(text, true); 691 } 692 693 698 public static final String textToCDATA(String text) { 699 if (text == null) return "null"; 700 final StringBuffer html = new StringBuffer (); 701 702 for (int i = 0; i < text.length(); i++) { 704 html.append(charToCDATA(text.charAt(i))); 706 } 707 708 return html.toString(); 710 } 711 712 717 public static final String charToCDATA(char ch) { 718 int c; 719 720 if (ch >= 128) { 721 c = ch; 722 723 return ("&#" + c + ';'); 724 } 725 726 else { 728 return (String.valueOf(ch)); 730 } 731 } 732 733 } 734 | Popular Tags |