1 package org.roller.presentation.velocity.plugins.jspwiki; 2 import com.ecyrd.jspwiki.FileUtil; 3 import com.ecyrd.jspwiki.TranslatorReader; 4 import com.ecyrd.jspwiki.WikiContext; 5 import com.ecyrd.jspwiki.WikiEngine; 6 import com.ecyrd.jspwiki.WikiPage; 7 8 import org.apache.commons.lang.StringEscapeUtils; 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.apache.velocity.context.Context; 12 import org.roller.pojos.WeblogEntryData; 13 import org.roller.presentation.RollerRequest; 14 import org.roller.presentation.velocity.PagePlugin; 15 16 import java.io.StringReader ; 17 import java.util.StringTokenizer ; 18 19 30 public class WikiPlugin implements PagePlugin 31 { 32 protected String name = "JSPWiki Syntax"; 33 public String description = "Allows use of JSPWiki formatting to easily " + 34 "generate HTML. See the " + 35 "<a HREF='http://www.jspwiki.org/Wiki.jsp?page=TextFormattingRules' target='jspwiki'>JSPWiki</a> site."; 36 37 private static Log mLogger = 38 LogFactory.getFactory().getInstance(WikiPlugin.class); 39 40 static WikiEngine mWikiEngine = null; 41 static WikiContext mWikiContext = null; 42 static WikiPage mWikiPage = new WikiPage("dummyPage"); 43 44 public WikiPlugin() 45 { 46 mLogger.debug("JSPWiki WikiPlugin instantiated."); 47 } 48 49 public String toString() { return name; } 50 51 55 public void init(RollerRequest rreq, Context ctx) 56 { 57 try 58 { 59 if (WikiPlugin.mWikiEngine == null) 60 { 61 if (rreq != null && rreq.getPageContext() != null) 62 { 63 WikiPlugin.mWikiEngine = WikiEngine.getInstance( 64 rreq.getPageContext().getServletConfig()); 65 } 66 } 67 if (WikiPlugin.mWikiContext == null && WikiPlugin.mWikiEngine != null) 68 { 69 WikiPlugin.mWikiContext = new WikiContext( 70 WikiPlugin.mWikiEngine, WikiPlugin.mWikiPage ); 71 } 72 73 if (WikiPlugin.mWikiContext != null && WikiPlugin.mWikiEngine != null) 74 { 75 ctx.put("wikiPlugin",this); 76 } 77 } 78 catch (Exception e) 79 { 80 mLogger.error("ERROR initializing WikiPlugin",e); 81 } 82 } 83 84 90 public String render( String src ) 91 { 92 String ret = null; 93 try 94 { 95 StringReader reader = new StringReader (src); 96 TranslatorReader tr = new TranslatorReader( mWikiContext, reader ); 97 ret = FileUtil.readContents( tr ); 98 } 99 catch (Exception e) 100 { 101 mLogger.error("ERROR rendering Wiki text",e); 102 } 103 return ret; 104 } 105 106 public String render( WeblogEntryData entry, boolean skipFlag) 107 { 108 return render(entry.getText()); 109 } 110 111 public String makeCamelCaseWikiLink( WeblogEntryData wd, String prefix ) 112 { 113 StringBuffer sb = new StringBuffer (); 114 StringTokenizer toker = new StringTokenizer (wd.getAnchor(),"_"); 115 while ( toker.hasMoreTokens() ) 116 { 117 String token = toker.nextToken(); 118 sb.append( token.substring(0,1).toUpperCase() ); 119 sb.append( token.substring(1) ); 120 } 121 return mWikiEngine.getBaseURL()+"Wiki.jsp?page="+prefix+sb.toString(); 122 } 123 124 125 public String makeSpacedWikiLink( WeblogEntryData wd, String prefix ) 126 { 127 StringBuffer sb = new StringBuffer (); 128 StringTokenizer toker = new StringTokenizer (wd.getAnchor(),"_"); 129 while ( toker.hasMoreTokens() ) 130 { 131 sb.append( toker.nextToken() ); 132 if ( toker.hasMoreTokens() ) sb.append("%20"); 133 } 134 return mWikiEngine.getBaseURL()+"Wiki.jsp?page="+prefix+sb.toString(); 135 } 136 137 public String getName() { return name; } 138 public String getDescription() { return StringEscapeUtils.escapeJavaScript(description); } 139 } 140 | Popular Tags |