1 2 package org.roller.presentation.velocity; 3 4 import org.apache.commons.logging.Log; 5 import org.apache.commons.logging.LogFactory; 6 import org.apache.velocity.Template; 7 import org.apache.velocity.VelocityContext; 8 import org.apache.velocity.runtime.RuntimeSingleton; 9 import org.roller.model.RefererManager; 10 import org.roller.model.UserManager; 11 import org.roller.pojos.PageData; 12 import org.roller.pojos.RefererData; 13 import org.roller.pojos.UserData; 14 import org.roller.pojos.WeblogCategoryData; 15 import org.roller.pojos.WebsiteData; 16 import org.roller.presentation.RollerContext; 17 import org.roller.presentation.RollerRequest; 18 import org.roller.presentation.bookmarks.tags.ViewBookmarksTag; 19 import org.roller.presentation.tags.menu.EditorNavigationBarTag; 20 import org.roller.presentation.tags.menu.NavigationBarTag; 21 import org.roller.presentation.weblog.tags.RssBadgeTag; 22 import org.roller.presentation.weblog.tags.ViewWeblogEntriesTag; 23 import org.roller.presentation.weblog.tags.WeblogCategoryChooserTag; 24 import org.roller.util.Utilities; 25 26 import java.io.PrintWriter ; 27 import java.io.StringWriter ; 28 import java.text.SimpleDateFormat ; 29 import java.util.ArrayList ; 30 import java.util.Date ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 import javax.servlet.ServletContext ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 import javax.servlet.jsp.PageContext ; 38 import org.roller.config.RollerRuntimeConfig; 39 import org.roller.model.RollerFactory; 40 41 45 public class Macros 46 { 47 private static Log mLogger = 48 LogFactory.getFactory().getInstance(Macros.class); 49 50 protected PageContext mPageContext = null; 51 52 protected PageHelper mPageHelper = null; 53 54 55 public static final int MAX_RECURSION_DEPTH = 6; 56 57 58 protected static final ThreadLocal mIncludePageTLS = new ThreadLocal (); 59 60 61 protected static final ThreadLocal mEntriesTLS = new ThreadLocal (); 62 63 64 protected static final ThreadLocal mEntriesDayTLS = new ThreadLocal (); 65 66 68 public Macros( PageContext ctx, PageHelper helper ) 69 { 70 mPageContext = ctx; 71 mPageHelper = helper; 72 } 73 74 76 protected RollerRequest getRollerRequest() 77 { 78 return RollerRequest.getRollerRequest(mPageContext); 79 } 80 81 87 public String formatCurrentDate( String pattern ) 88 { 89 String date = null; 90 try 91 { 92 SimpleDateFormat format = new SimpleDateFormat ( pattern ); 93 date = format.format( new Date () ); 94 } 95 catch (RuntimeException e) 96 { 97 date = "ERROR: formatting date"; 98 } 99 return date; 100 } 101 102 110 public String formatUpdateTime( ArrayList weblogEntries, String pattern ) 111 { 112 String date = null; 113 Date updateTime = getUpdateTime(weblogEntries); 114 try 115 { 116 SimpleDateFormat format = new SimpleDateFormat ( pattern ); 117 date = format.format( updateTime ); 118 } 119 catch (RuntimeException e) 120 { 121 date = "ERROR: formatting date"; 122 } 123 return date; 124 } 125 126 132 public Date getUpdateTime( ArrayList weblogEntries ) 133 { 134 return PageModel.getUpdateTime( weblogEntries ); 135 } 136 137 142 public String showVersion() 143 { 144 ServletContext ctx = mPageContext.getServletContext(); 145 return ctx.getInitParameter( RollerContext.VERSION_KEY ); 146 } 147 152 public String showWebsiteTitle() 153 { 154 WebsiteData wd = null; 155 RollerRequest rreq = getRollerRequest(); 156 try { 157 wd = rreq.getWebsite(); 158 } 159 catch (Exception e) { 160 return "ERROR finding website in request: " + e.toString(); 161 } 162 return wd.getName(); 163 } 164 169 public String showWebsiteDescription() 170 { 171 WebsiteData wd = null; 172 RollerRequest rreq = getRollerRequest(); 173 try { 174 wd = rreq.getWebsite(); 175 } 176 catch (Exception e) { 177 return "ERROR finding website in request: " + e.toString(); 178 } 179 return wd.getDescription(); 180 } 181 187 public String showNavBar( boolean vertical ) 188 { 189 NavigationBarTag navTag = new NavigationBarTag(); 190 navTag.setPageContext(mPageContext); 191 navTag.setVertical(vertical); 192 return navTag.emit(); 193 } 194 202 public String showNavBar( boolean vertical, String delimiter ) 203 { 204 NavigationBarTag navTag = new NavigationBarTag(); 205 navTag.setPageContext(mPageContext); 206 navTag.setVertical(vertical); 207 navTag.setDelimiter(delimiter); 208 return navTag.emit(); 209 } 210 217 public String showEditorNavBar( boolean vertical ) 218 { 219 EditorNavigationBarTag editorTag = new EditorNavigationBarTag(); 220 editorTag.setPageContext(mPageContext); 221 if ( vertical ) 222 { 223 editorTag.setView("/navbar-vertical.vm"); 224 } 225 else 226 { 227 editorTag.setView("/navbar-horizontal.vm"); 228 } 229 editorTag.setModel("editor-menu.xml"); 230 return editorTag.emit(); 231 } 232 233 235 241 public String showBookmarks( String folderName, String title ) 242 { 243 ViewBookmarksTag booksTag = new ViewBookmarksTag(); 244 booksTag.setPageContext(mPageContext); 245 return booksTag.view( folderName, title ); 246 } 247 248 254 public String showBookmarks( String folderName, boolean showFolderName ) 255 { 256 ViewBookmarksTag booksTag = new ViewBookmarksTag(); 257 booksTag.setPageContext(mPageContext); 258 return booksTag.view( folderName, showFolderName ); 259 } 260 261 268 public String showBookmarks( 269 String folderName, boolean showFolderName, boolean expandingFolder ) 270 { 271 ViewBookmarksTag booksTag = new ViewBookmarksTag(); 272 booksTag.setPageContext(mPageContext); 273 return booksTag.view( folderName, showFolderName, expandingFolder ); 274 } 275 276 282 public UserData getUser() 283 { 284 try 285 { 286 return getRollerRequest().getUser(); 287 } 288 catch (Exception e) 289 { 290 mLogger.error("Getting user",e); 291 } 292 return null; 293 } 294 295 303 public WebsiteData getWebsite() 304 { 305 try 306 { 307 return getRollerRequest().getWebsite(); 308 } 309 catch (Exception e) 310 { 311 mLogger.error("Getting website",e); 312 } 313 return null; 314 } 315 316 322 public String showWeblogEntries() 323 { 324 return showWeblogEntries(15); 325 } 326 327 334 public String showWeblogEntries(int maxEntries) 335 { 336 String ret = null; 337 try 338 { 339 if ( mEntriesTLS.get() == null ) 341 { 342 mEntriesTLS.set( new Integer (0) ); 343 } 344 else 345 { 346 Integer countObj = (Integer )mEntriesTLS.get(); 347 int count = countObj.intValue(); 348 if ( count++ > MAX_RECURSION_DEPTH ) 349 { 350 return "ERROR: recursion level too deep"; 351 } 352 mEntriesTLS.set( new Integer (count) ); 353 } 354 355 ViewWeblogEntriesTag entriesTag = new ViewWeblogEntriesTag(); 356 entriesTag.setPageContext(mPageContext); 357 entriesTag.setMaxEntries(maxEntries); 358 ret = entriesTag.emit(); 359 } 360 finally 361 { 362 mEntriesTLS.set( null ); 363 } 364 365 return ret; 366 } 367 368 374 public String showWeblogEntries( String dayTemplate ) 375 { 376 return showWeblogEntries(dayTemplate,15); 377 } 378 379 386 public String showWeblogEntries( String dayTemplate, int maxEntries ) 387 { 388 String ret = null; 389 try 390 { 391 if ( mEntriesDayTLS.get() == null ) 393 { 394 mEntriesDayTLS.set( new Integer (0) ); 395 } 396 else 397 { 398 Integer countObj = (Integer )mEntriesDayTLS.get(); 399 int count = countObj.intValue(); 400 if ( count++ > MAX_RECURSION_DEPTH ) 401 { 402 return "ERROR: recursion level too deep"; 403 } 404 mEntriesDayTLS.set( new Integer (count) ); 405 } 406 407 ViewWeblogEntriesTag entriesTag = new ViewWeblogEntriesTag(); 408 entriesTag.setPageContext(mPageContext); 409 entriesTag.setMaxEntries(maxEntries); 410 entriesTag.setDayTemplate( dayTemplate ); 411 ret = entriesTag.emit(); 412 } 413 finally 414 { 415 mEntriesDayTLS.set( null ); 416 } 417 return ret; 418 } 419 420 425 public String showWeblogCalendar() 426 { 427 return showWeblogCalendar(false); 428 } 429 430 435 public String showBigWeblogCalendar() 436 { 437 return showWeblogCalendar(true); 438 } 439 440 446 public String showWeblogCalendar( boolean big ) 447 { 448 return mPageHelper.showWeblogCalendar(big, null); 449 } 450 451 456 public String showWeblogCategoryChooser() 457 { 458 WeblogCategoryChooserTag catTag = new WeblogCategoryChooserTag(); 459 catTag.setPageContext(mPageContext); 460 return catTag.emit(); 461 } 462 463 468 public String showRSSLinks() 469 { 470 String links = "ERROR: creating RSS links"; 471 try 472 { 473 RollerRequest rreq = getRollerRequest(); 474 RollerContext rctx = RollerContext.getRollerContext( 475 rreq.getServletContext()); 476 477 UserData ud = rreq.getUser(); 478 479 String baseUrl = rctx.getContextUrl( 480 (HttpServletRequest )mPageContext.getRequest()); 481 StringBuffer sb = new StringBuffer (); 482 483 484 sb.append("<a HREF=\""); 485 sb.append( baseUrl ); 486 sb.append("/rss/"); 487 sb.append( ud.getUserName() ); 488 sb.append("\">All</a> "); 489 sb.append("[<a HREF=\""); 490 sb.append( baseUrl ); 491 sb.append("/rss/"); 492 sb.append( ud.getUserName() ); 493 sb.append("?excerpts=true\">"); 494 sb.append("excerpts</a>]"); 495 sb.append("<br />"); 496 497 List cats = rreq.getRoller().getWeblogManager() 498 .getWeblogCategories(rreq.getWebsite(), false); 499 for (Iterator wbcItr = cats.iterator(); wbcItr.hasNext();) { 500 WeblogCategoryData category = (WeblogCategoryData) wbcItr.next(); 501 String catName = category.getName(); 502 sb.append("<a HREF=\""); 503 sb.append( baseUrl ); 504 sb.append("/rss/"); 505 sb.append( ud.getUserName() ); 506 sb.append("?catname="); 507 sb.append( catName ); 508 sb.append("\">"); 509 sb.append( catName ); 510 sb.append("</a> "); 511 sb.append("[<a HREF=\""); 512 sb.append( baseUrl ); 513 sb.append("/rss/"); 514 sb.append( ud.getUserName() ); 515 sb.append("?catname="); 516 sb.append( catName ); 517 sb.append("&excerpts=true\">"); 518 sb.append("excerpts</a>]"); 519 sb.append("<br />"); 520 } 521 522 links = sb.toString(); 523 } 524 catch (Exception e) 525 { 526 mLogger.error("Unexpected exception",e); 527 } 528 return links; 529 } 530 531 536 public String showRSSAutodiscoveryLink() 537 { 538 String links = "ERROR: error generating RSS autodiscovery link"; 539 try 540 { 541 RollerRequest rreq = RollerRequest.getRollerRequest( 542 (HttpServletRequest )mPageContext.getRequest()); 543 RollerContext rctx = RollerContext.getRollerContext( 544 rreq.getServletContext()); 545 546 StringBuffer sb = new StringBuffer (); 547 sb.append("<link rel=\"alternate\" type=\"application/rss+xml\" "); 548 sb.append("title=\"RSS\" HREF=\""); 549 sb.append( rctx.getContextUrl( 550 (HttpServletRequest )mPageContext.getRequest()) ); 551 sb.append( "/rss/" ); 552 sb.append( getUser().getUserName() ); 553 sb.append( "\">" ); 554 links = sb.toString(); 555 } 556 catch (Throwable e) 557 { 558 mLogger.error("Showing RSS link",e); 559 } 560 return links; 561 } 562 563 568 public String showRSSBadge() 569 { 570 RssBadgeTag rssTag = new RssBadgeTag(); 571 rssTag.setPageContext(mPageContext); 572 return rssTag.emit(); 573 } 574 575 581 public String includePage( String pageName ) 582 { 583 String ret = null; 584 try 585 { 586 if ( mIncludePageTLS.get() == null ) 588 { 589 mIncludePageTLS.set( new Integer (0) ); 590 } 591 else 592 { 593 Integer countObj = (Integer )mIncludePageTLS.get(); 594 int count = countObj.intValue(); 595 if ( count++ > MAX_RECURSION_DEPTH ) 596 { 597 return "ERROR: recursion level too deep"; 598 } 599 mIncludePageTLS.set( new Integer (count) ); 600 } 601 602 RollerRequest rreq = RollerRequest.getRollerRequest( 604 (HttpServletRequest )mPageContext.getRequest()); 605 UserManager userMgr = rreq.getRoller().getUserManager(); 606 607 PageData pd = userMgr.getPageByName( 608 rreq.getWebsite(), pageName ); 609 Template vtemplate = null; 610 if (pd != null) 611 { 612 vtemplate = RuntimeSingleton.getTemplate( pd.getId() ); 613 } 614 else 615 { 616 vtemplate = RuntimeSingleton.getTemplate( pageName ); 618 } 619 620 if (vtemplate == null) 621 { 622 ret = pageName + " : No Page or Template found."; 623 } 624 else 625 { 626 StringWriter sw = new StringWriter (); 628 PrintWriter pw = new PrintWriter (sw); 629 VelocityContext vcontext = new VelocityContext(); 630 ContextLoader.setupContext(vcontext,rreq, 631 (HttpServletResponse )mPageContext.getResponse()); 632 633 vtemplate.merge( vcontext, pw ); 634 ret = sw.toString(); 635 } 636 } 637 catch (Exception e) 638 { 639 ret = "ERROR: including page " + pageName; 640 } 641 finally 642 { 643 mIncludePageTLS.set( null ); 644 } 645 return ret; 646 } 647 648 659 public String showResourceImage( String fileName, String linkUrl, 660 String alt, int border, String halign, String valign ) 661 { 662 HttpServletRequest request = 663 (HttpServletRequest ) mPageContext.getRequest(); 664 String username = getRollerRequest().getUser().getUserName(); 665 ServletContext app = mPageContext.getServletContext(); 666 667 StringBuffer sb = new StringBuffer (); 668 String uploadPath = null; 669 try { 670 uploadPath = RollerFactory.getRoller().getFileManager().getUploadUrl(); 671 } catch(Exception e) {} 672 673 if ( uploadPath != null && uploadPath.trim().length() > 0 ) 674 { 675 sb.append( uploadPath ); 676 sb.append( "/"); 677 sb.append( username ); 678 sb.append( "/"); 679 sb.append( fileName); 680 } 681 else 682 { 683 sb.append( request.getContextPath() ); 684 sb.append( RollerContext.USER_RESOURCES ); 685 sb.append( "/"); 686 sb.append( username ); 687 sb.append( "/"); 688 sb.append( fileName); 689 } 690 return showImage( sb.toString(),linkUrl,alt,border,halign,valign ); 691 } 692 693 704 public String showImage( String imageUrl, String linkUrl, 705 String alt, int border, String halign, String valign ) 706 { 707 StringBuffer sb = new StringBuffer (); 708 if ( linkUrl!=null && linkUrl.trim().length()>0 ) 709 { 710 sb.append( "<a HREF=\""); 711 sb.append( linkUrl ); 712 sb.append( "\" >"); 713 } 714 715 sb.append("<img SRC=\""); 716 sb.append( imageUrl ); 717 sb.append( "\" alt=\""); 718 sb.append( alt); 719 sb.append( "\" border=\""); 720 sb.append( border); 721 sb.append( "\" halign=\""); 722 sb.append( halign); 723 sb.append( "\" valign=\""); 724 sb.append( valign); 725 sb.append( "\" />"); 726 727 if ( linkUrl!=null && linkUrl.trim().length()>0 ) 728 { 729 sb.append( "</a>"); 730 } 731 732 return sb.toString(); 733 } 734 735 740 public String showImagePath() 741 { 742 HttpServletRequest request = 743 (HttpServletRequest ) mPageContext.getRequest(); 744 745 StringBuffer sb = new StringBuffer (); 746 sb.append( request.getContextPath() ); 747 sb.append( "/images" ); 748 return sb.toString(); 749 } 750 751 757 public String showResourcePath() 758 { 759 HttpServletRequest request = 760 (HttpServletRequest ) mPageContext.getRequest(); 761 762 String username = getRollerRequest().getUser().getUserName(); 763 764 ServletContext app = mPageContext.getServletContext(); 765 766 StringBuffer sb = new StringBuffer (); 767 String uploadPath = null; 768 try { 769 uploadPath = RollerFactory.getRoller().getFileManager().getUploadUrl(); 770 } catch(Exception e) {} 771 if ( uploadPath != null && uploadPath.trim().length() > 0 ) 772 { 773 sb.append( uploadPath ); 774 } 775 else 776 { 777 sb.append( request.getContextPath() ); 778 sb.append( RollerContext.USER_RESOURCES ); 779 } 780 sb.append( "/" ); 781 sb.append( username ); 782 return sb.toString(); 783 } 784 785 792 public String showThemePath( String theme ) 793 { 794 String themesdir = RollerRuntimeConfig.getProperty("users.themes.path"); 795 796 HttpServletRequest request = 797 (HttpServletRequest )mPageContext.getRequest(); 798 StringBuffer sb = new StringBuffer (); 799 sb.append( request.getContextPath()); 800 sb.append(themesdir); 801 sb.append( "/" ); 802 sb.append( theme ); 803 return sb.toString(); 804 } 805 806 813 public String showThemeImagePath( String theme, String imageName ) 814 { 815 StringBuffer sb = new StringBuffer (); 816 sb.append( showThemePath(theme)); 817 sb.append( "/images/"); 818 sb.append( imageName); 819 return sb.toString(); 820 } 821 822 829 public String showThemeImage( String theme, String imageName ) 830 { 831 StringBuffer sb = new StringBuffer (); 832 sb.append( "<img alt=\""+imageName+"\" SRC=\""); 833 sb.append( showThemeImagePath(theme, imageName)); 834 sb.append( "\"/>"); 835 return sb.toString(); 836 } 837 838 845 public String showThemeStylePath( String theme, String stylesheet ) 846 { 847 StringBuffer sb = new StringBuffer (); 848 sb.append( showThemePath(theme)); 849 sb.append( "/styles/"); 850 sb.append( stylesheet); 851 return sb.toString(); 852 } 853 854 862 public String showThemeStyle( 863 String theme, String stylesheet, boolean useImport ) 864 { 865 StringBuffer sb = new StringBuffer (); 866 if (useImport) { 867 sb.append( "<style type=\"text/css\">"); 868 sb.append( "@import url("); 869 sb.append( showThemeStylePath(theme, stylesheet)); 870 sb.append( ");"); 871 sb.append( "</style>"); 872 } else { 873 sb.append( "<link rel=\"stylesheet\" type=\"text/css\" HREF=\""); 874 sb.append( showThemeStylePath(theme, stylesheet)); 875 sb.append( "\" />"); 876 } 877 return sb.toString(); 878 } 879 880 887 public String showThemeStyleImport( String theme, String stylesheet ) 888 { 889 return showThemeStyle(theme, stylesheet, true); 890 } 891 892 899 public String showThemeScriptPath( String theme, String scriptFile ) 900 { 901 StringBuffer sb = new StringBuffer (); 902 sb.append( showThemePath(theme)); 903 sb.append( "/scripts/"); 904 sb.append( scriptFile); 905 return sb.toString(); 906 } 907 908 919 public String showThemeScript( String theme, String scriptFile) 920 { 921 StringBuffer sb = new StringBuffer (); 922 sb.append( "<script type=\"text/javascript\" SRC=\""); 923 sb.append( showThemeScriptPath(theme, scriptFile)); 924 sb.append( "\"></script>"); return sb.toString(); 926 } 927 928 933 public String showPageName() 934 { 935 PageData pd = null; 936 RollerRequest rreq = getRollerRequest(); 937 try 938 { 939 pd = rreq.getPage(); 940 } 941 catch (Exception e) { 942 return "ERROR finding page in request: " + e.toString(); 943 } 944 return pd.getName(); 945 } 946 947 952 public String showPageDescription() 953 { 954 PageData pd = null; 955 RollerRequest rreq = getRollerRequest(); 956 try 957 { 958 pd = rreq.getPage(); 959 } 960 catch (Exception e) 961 { 962 return "ERROR finding page in request: " + e.toString(); 963 } 964 return pd.getDescription(); 965 } 966 967 972 public String showPageUpdateTime() 973 { 974 PageData pd = null; 975 RollerRequest rreq = getRollerRequest(); 976 try 977 { 978 pd = rreq.getPage(); 979 } 980 catch (Exception e) 981 { 982 return "ERROR finding page in request: " + e.toString(); 983 } 984 if (pd.getUpdateTime() == null) return ""; 985 return pd.getUpdateTime().toString(); 986 } 987 988 994 public String showReferers() 995 { 996 return showReferers(15,20); 997 } 998 999 1006 public String showReferers( int max ) 1007 { 1008 return showReferers(max,20); 1009 } 1010 1011 1019 public String showReferers( int max, int maxWidth ) 1020 { 1021 try 1022 { 1023 RollerRequest rreq = getRollerRequest(); 1024 RefererManager refmgr = rreq.getRoller().getRefererManager(); 1025 1026 StringBuffer sb = new StringBuffer (); 1027 1028 sb.append("Today's Page Hits:"); 1029 sb.append( refmgr.getDayHits(rreq.getWebsite()) ); 1030 sb.append("<br/>"); 1031 sb.append("Total Page Hits:"); 1032 sb.append( refmgr.getTotalHits(rreq.getWebsite()) ); 1033 sb.append("<br/>"); 1034 1035 List refs = refmgr.getTodaysReferers(rreq.getWebsite()); 1036 sb.append(""); 1037 sb.append("<ul class=\"rReferersList\">"); 1038 int count = refs.size()>max ? max : refs.size(); 1039 for (int i = 0; i < count; i++) 1040 { 1041 RefererData data = (RefererData)refs.get(i); 1042 sb.append("<li class=\"rReferersListItem\">"); 1043 sb.append( data.getDisplayUrl( maxWidth, true ) ); 1044 sb.append("</li>"); 1045 } 1046 sb.append("</ul>"); 1047 1048 return sb.toString(); 1049 } 1050 catch (Exception e) 1051 { 1052 mLogger.error("Displaying referers list",e); 1053 return "ERROR: displaying referers list"; 1054 } 1055 } 1056 1057 1064 public static String removeHTML(String s) 1065 { 1066 if ( s==null ) return ""; 1067 else return Utilities.removeHTML(s); 1068 } 1069 1070 1075 public static String escapeHTML( String s ) 1076 { 1077 if ( s==null ) return ""; 1078 else return Utilities.escapeHTML(s); 1079 } 1080 1081 1085 public static String removeAndEscapeHTML( String s ) 1086 { 1087 if ( s==null ) return ""; 1088 else return Utilities.escapeHTML( removeHTML(s) ); 1089 } 1090} 1091 1092 1093 | Popular Tags |