1 18 19 package org.apache.roller.ui.rendering.model; 20 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.Map ; 27 import java.util.TimeZone ; 28 import java.util.regex.Matcher ; 29 import java.util.regex.Pattern ; 30 import javax.servlet.http.HttpServletRequest ; 31 import org.apache.commons.lang.StringEscapeUtils; 32 import org.apache.commons.lang.StringUtils; 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.apache.roller.RollerException; 36 import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper; 37 import org.apache.roller.pojos.wrapper.WebsiteDataWrapper; 38 import org.apache.roller.ui.core.RollerSession; 39 import org.apache.roller.ui.rendering.util.WeblogRequest; 40 import org.apache.roller.util.DateUtil; 41 import org.apache.roller.util.RegexUtil; 42 import org.apache.roller.util.Utilities; 43 44 47 public class UtilitiesModel implements Model { 48 49 private static Log log = LogFactory.getLog(UtilitiesModel.class); 50 51 private static Pattern mLinkPattern = 52 Pattern.compile("<a HREF=.*?>", Pattern.CASE_INSENSITIVE); 53 private static final Pattern OPENING_B_TAG_PATTERN = 54 Pattern.compile("<b>", Pattern.CASE_INSENSITIVE); 55 private static final Pattern CLOSING_B_TAG_PATTERN = 56 Pattern.compile("</b>", Pattern.CASE_INSENSITIVE); 57 private static final Pattern OPENING_I_TAG_PATTERN = 58 Pattern.compile("<i>", Pattern.CASE_INSENSITIVE); 59 private static final Pattern CLOSING_I_TAG_PATTERN = 60 Pattern.compile("</i>", Pattern.CASE_INSENSITIVE); 61 private static final Pattern OPENING_BLOCKQUOTE_TAG_PATTERN = 62 Pattern.compile("<blockquote>", Pattern.CASE_INSENSITIVE); 63 private static final Pattern CLOSING_BLOCKQUOTE_TAG_PATTERN = 64 Pattern.compile("</blockquote>", Pattern.CASE_INSENSITIVE); 65 private static final Pattern BR_TAG_PATTERN = 66 Pattern.compile("<br */*>", Pattern.CASE_INSENSITIVE); 67 private static final Pattern OPENING_P_TAG_PATTERN = 68 Pattern.compile("<p>", Pattern.CASE_INSENSITIVE); 69 private static final Pattern CLOSING_P_TAG_PATTERN = 70 Pattern.compile("</p>", Pattern.CASE_INSENSITIVE); 71 private static final Pattern OPENING_PRE_TAG_PATTERN = 72 Pattern.compile("<pre>", Pattern.CASE_INSENSITIVE); 73 private static final Pattern CLOSING_PRE_TAG_PATTERN = 74 Pattern.compile("</pre>", Pattern.CASE_INSENSITIVE); 75 private static final Pattern OPENING_UL_TAG_PATTERN = 76 Pattern.compile("<ul>", Pattern.CASE_INSENSITIVE); 77 private static final Pattern CLOSING_UL_TAG_PATTERN = 78 Pattern.compile("</ul>", Pattern.CASE_INSENSITIVE); 79 private static final Pattern OPENING_OL_TAG_PATTERN = 80 Pattern.compile("<ol>", Pattern.CASE_INSENSITIVE); 81 private static final Pattern CLOSING_OL_TAG_PATTERN = 82 Pattern.compile("</ol>", Pattern.CASE_INSENSITIVE); 83 private static final Pattern OPENING_LI_TAG_PATTERN = 84 Pattern.compile("<li>", Pattern.CASE_INSENSITIVE); 85 private static final Pattern CLOSING_LI_TAG_PATTERN = 86 Pattern.compile("</li>", Pattern.CASE_INSENSITIVE); 87 private static final Pattern CLOSING_A_TAG_PATTERN = 88 Pattern.compile("</a>", Pattern.CASE_INSENSITIVE); 89 private static final Pattern OPENING_A_TAG_PATTERN = 90 Pattern.compile("<a HREF=.*?>", Pattern.CASE_INSENSITIVE); 91 private static final Pattern QUOTE_PATTERN = 92 Pattern.compile(""", Pattern.CASE_INSENSITIVE); 93 94 private HttpServletRequest request = null; 95 private TimeZone tz = null; 96 97 98 99 public String getModelName() { 100 return "utils"; 101 } 102 103 104 105 public void init(Map initData) throws RollerException { 106 107 this.request = (HttpServletRequest ) initData.get("request"); 109 110 WeblogRequest weblogRequest = (WeblogRequest)initData.get("weblogRequest"); 112 if (weblogRequest != null && weblogRequest.getWeblog() != null) { 113 tz = weblogRequest.getWeblog().getTimeZoneInstance(); 114 } 115 } 116 117 118 120 public boolean isUserAuthorizedToAuthor(WebsiteDataWrapper weblog) { 121 try { 122 RollerSession rses = RollerSession.getRollerSession(request); 123 if (rses.getAuthenticatedUser() != null) { 124 return rses.isUserAuthorizedToAuthor(weblog.getPojo()); 125 } 126 } catch (Exception e) { 127 log.warn("ERROR: checking user authorization", e); 128 } 129 return false; 130 } 131 132 public boolean isUserAuthorizedToAdmin(WebsiteDataWrapper weblog) { 133 try { 134 RollerSession rses = RollerSession.getRollerSession(request); 135 if (rses.getAuthenticatedUser() != null) { 136 return rses.isUserAuthorizedToAdmin(weblog.getPojo()); 137 } 138 } catch (Exception e) { 139 log.warn("ERROR: checking user authorization", e); 140 } 141 return false; 142 } 143 144 public boolean isUserAuthenticated() { 145 return (request.getUserPrincipal() != null); 146 } 147 148 152 public static Date getNow() { 153 return new Date (); 154 } 155 156 159 public String formatDate(Date d, String fmt) { 160 if(d == null || fmt == null) 161 return fmt; 162 163 SimpleDateFormat format = new SimpleDateFormat (fmt); 164 if (tz != null) { 165 format.setTimeZone(tz); 166 } 167 return format.format(d); 168 } 169 170 173 public static String formatDate(Date d, String fmt, TimeZone tzOverride) { 174 if(d == null || fmt == null) 175 return fmt; 176 177 SimpleDateFormat format = new SimpleDateFormat (fmt); 178 format.setTimeZone(tzOverride); 179 return format.format(d); 180 } 181 182 185 public static String formatIso8601Date(Date d) { 186 return DateUtil.formatIso8601(d); 187 } 188 189 192 public static String formatIso8601Day(Date d) { 193 return DateUtil.formatIso8601Day(d); 194 } 195 196 199 public static String formatRfc822Date(Date date) { 200 return DateUtil.formatRfc822(date); 201 } 202 203 206 public static String format8charsDate(Date date) { 207 return DateUtil.format8chars(date); 208 } 209 210 212 public static boolean isEmpty(String str) { 213 if (str == null) return true; 214 return "".equals(str.trim()); 215 } 216 217 public static boolean isNotEmpty(String str) { 218 return !isEmpty(str); 219 } 220 221 public static String [] split(String str1, String str2) { 222 return StringUtils.split(str1, str2); 223 } 224 225 226 public static boolean equals(String str1, String str2) { 227 return StringUtils.equals(str1, str2); 228 } 229 230 public static boolean isAlphanumeric(String str) { 231 return StringUtils.isAlphanumeric(str); 232 } 233 234 public static String [] stripAll(String [] strs) { 235 return StringUtils.stripAll(strs); 236 } 237 238 public static String left(String str, int length) { 239 return StringUtils.left(str, length); 240 } 241 242 public static String escapeHTML(String str) { 243 return StringEscapeUtils.escapeHtml(str); 244 } 245 246 public static String unescapeHTML(String str) { 247 return StringEscapeUtils.unescapeHtml(str); 248 } 249 250 public static String escapeXML(String str) { 251 return StringEscapeUtils.escapeXml(str); 252 } 253 254 public static String unescapeXML(String str) { 255 return StringEscapeUtils.unescapeXml(str); 256 } 257 258 public static String replace(String src, String target, String rWith) { 259 return StringUtils.replace(src, target, rWith); 260 } 261 262 public static String replace(String src, String target, String rWith, int maxCount) { 263 return StringUtils.replace(src, target, rWith, maxCount); 264 } 265 266 private static String replace(String string, Pattern pattern, String replacement) { 267 Matcher m = pattern.matcher(string); 268 return m.replaceAll(replacement); 269 } 270 271 276 public static String removeHTML(String str) { 277 return removeHTML(str, true); 278 } 279 280 285 public static String removeHTML(String str, boolean addSpace) { 286 return Utilities.removeHTML(str, addSpace); 287 } 288 289 292 public static String autoformat(String s) { 293 String ret = StringUtils.replace(s, "\n", "<br />"); 294 return ret; 295 } 296 299 public static String truncate( 300 String str, int lower, int upper, String appendToEnd) { 301 String str2 = removeHTML(str, false); 303 304 if (upper < lower) { 306 upper = lower; 307 } 308 309 if(str2.length() > upper) { 312 int loc; 314 315 loc = str2.lastIndexOf(' ', upper); 317 318 if(loc >= lower) { 320 str2 = str2.substring(0, loc); 322 } else { 323 str2 = str2.substring(0, upper); 325 loc = upper; 326 } 327 328 str2 = str2 + appendToEnd; 330 } 331 332 return str2; 333 } 334 335 public static String truncateNicely(String str, int lower, int upper, String appendToEnd) { 336 return Utilities.truncateNicely(str, lower, upper, appendToEnd); 337 } 338 339 public static String truncateText(String str, int lower, int upper, String appendToEnd) { 340 String str2 = removeHTML(str, false); 342 boolean diff = (str2.length() < str.length()); 343 344 if(upper < lower) { 346 upper = lower; 347 } 348 349 if(str2.length() > upper) { 352 int loc; 354 355 loc = str2.lastIndexOf(' ', upper); 357 358 if(loc >= lower) { 360 str2 = str2.substring(0, loc); 362 } else { 363 str2 = str2.substring(0, upper); 365 loc = upper; 366 } 367 str = str2 + appendToEnd; 369 } 370 return str; 371 } 372 373 public static String hexEncode(String str) { 374 if (StringUtils.isEmpty(str)) return str; 375 376 return RegexUtil.encode(str); 377 } 378 379 public static String encodeEmail(String str) { 380 return str!=null ? RegexUtil.encodeEmail(str) : null; 381 } 382 383 388 public static final String encode(String s) { 389 try { 390 if (s != null) 391 return URLEncoder.encode(s, "UTF-8"); 392 else 393 return s; 394 } catch (UnsupportedEncodingException e) { 395 return s; 397 } 398 } 399 400 405 public static final String decode(String s) { 406 try { 407 if (s != null) 408 return URLDecoder.decode(s, "UTF-8"); 409 else 410 return s; 411 } catch (UnsupportedEncodingException e) { 412 return s; 414 } 415 } 416 417 420 public static String addNofollow(String html) { 421 if (html == null || html.length() == 0) { 422 return html; 423 } 424 Matcher m = mLinkPattern.matcher(html); 425 StringBuffer buf = new StringBuffer (); 426 while (m.find()) { 427 int start = m.start(); 428 int end = m.end(); 429 String link = html.substring(start, end); 430 buf.append(html.substring(0, start)); 431 if (link.indexOf("rel=\"nofollow\"") == -1) { 432 buf.append( 433 link.substring(0, link.length() - 1) + " rel=\"nofollow\">"); 434 } else { 435 buf.append(link); 436 } 437 html = html.substring(end, html.length()); 438 m = mLinkPattern.matcher(html); 439 } 440 buf.append(html); 441 return buf.toString(); 442 } 443 444 452 public static String transformToHTMLSubset(String s) { 453 454 if (s == null) { 455 return null; 456 } 457 458 s = replace(s, OPENING_B_TAG_PATTERN, "<b>"); 459 s = replace(s, CLOSING_B_TAG_PATTERN, "</b>"); 460 s = replace(s, OPENING_I_TAG_PATTERN, "<i>"); 461 s = replace(s, CLOSING_I_TAG_PATTERN, "</i>"); 462 s = replace(s, OPENING_BLOCKQUOTE_TAG_PATTERN, "<blockquote>"); 463 s = replace(s, CLOSING_BLOCKQUOTE_TAG_PATTERN, "</blockquote>"); 464 s = replace(s, BR_TAG_PATTERN, "<br />"); 465 s = replace(s, OPENING_P_TAG_PATTERN, "<p>"); 466 s = replace(s, CLOSING_P_TAG_PATTERN, "</p>"); 467 s = replace(s, OPENING_PRE_TAG_PATTERN, "<pre>"); 468 s = replace(s, CLOSING_PRE_TAG_PATTERN, "</pre>"); 469 s = replace(s, OPENING_UL_TAG_PATTERN, "<ul>"); 470 s = replace(s, CLOSING_UL_TAG_PATTERN, "</ul>"); 471 s = replace(s, OPENING_OL_TAG_PATTERN, "<ol>"); 472 s = replace(s, CLOSING_OL_TAG_PATTERN, "</ol>"); 473 s = replace(s, OPENING_LI_TAG_PATTERN, "<li>"); 474 s = replace(s, CLOSING_LI_TAG_PATTERN, "</li>"); 475 s = replace(s, QUOTE_PATTERN, "\""); 476 477 s = replace(s, CLOSING_A_TAG_PATTERN, "</a>"); 479 Matcher m = OPENING_A_TAG_PATTERN.matcher(s); 480 while (m.find()) { 481 int start = m.start(); 482 int end = m.end(); 483 String link = s.substring(start, end); 484 link = "<" + link.substring(4, link.length() - 4) + ">"; 485 s = s.substring(0, start) + link + s.substring(end, s.length()); 486 m = OPENING_A_TAG_PATTERN.matcher(s); 487 } 488 489 s = s.replaceAll("&lt;", "<"); 491 s = s.replaceAll("&gt;", ">"); 492 s = s.replaceAll("&#", "&#"); 493 494 return s; 495 } 496 497 500 public static String toBase64(byte[] aValue) { 501 502 final String m_strBase64Chars = 503 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 504 505 int byte1; 506 int byte2; 507 int byte3; 508 int iByteLen = aValue.length; 509 StringBuffer tt = new StringBuffer (); 510 511 for (int i = 0; i < iByteLen; i += 3) { 512 boolean bByte2 = (i + 1) < iByteLen; 513 boolean bByte3 = (i + 2) < iByteLen; 514 byte1 = aValue[i] & 0xFF; 515 byte2 = (bByte2) ? (aValue[i + 1] & 0xFF) : 0; 516 byte3 = (bByte3) ? (aValue[i + 2] & 0xFF) : 0; 517 518 tt.append(m_strBase64Chars.charAt(byte1 / 4)); 519 tt.append(m_strBase64Chars.charAt((byte2 / 16) + ((byte1 & 0x3) * 16))); 520 tt.append(((bByte2) ? m_strBase64Chars.charAt((byte3 / 64) + ((byte2 & 0xF) * 4)) : '=')); 521 tt.append(((bByte3) ? m_strBase64Chars.charAt(byte3 & 0x3F) : '=')); 522 } 523 524 return tt.toString(); 525 } 526 527 } 528 | Popular Tags |