Source code: org/roller/presentation/velocity/plugins/jspwiki/WikiPlugin.java
1
2 package org.roller.presentation.velocity.plugins.jspwiki;
3
4 import com.ecyrd.jspwiki.FileUtil;
5 import com.ecyrd.jspwiki.TranslatorReader;
6 import com.ecyrd.jspwiki.WikiContext;
7 import com.ecyrd.jspwiki.WikiEngine;
8 import com.ecyrd.jspwiki.WikiPage;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.apache.velocity.context.Context;
13 import org.roller.pojos.WeblogEntryData;
14 import org.roller.presentation.RollerRequest;
15 import org.roller.presentation.velocity.PagePlugin;
16
17 import java.io.StringReader;
18 import java.util.StringTokenizer;
19
20 /**
21 * Wiki plugin using the JSPWiki WikiEngine. If you want Wiki links to point
22 * to your JSPWiki, then edit the jspwiki.properties fiel in Roller's WEB-INF
23 * directory and set the jspwiki.baseURL appropriately. For example, if your
24 * Wiki is on your localhost in the Servlet Context named 'wiki' then you'd
25 * set jspwiki.baseURL like so:<br />
26 * <br />
27 * jspwiki.baseURL=http://local:8080/wiki/<br />
28 *
29 * @author David M Johnson
30 */
31 public class WikiPlugin implements PagePlugin
32 {
33 private static Log mLogger =
34 LogFactory.getFactory().getInstance(WikiPlugin.class);
35
36 WikiEngine mWikiEngine = null;
37 WikiContext mWikiContext = null;
38 WikiPage mWikiPage = new WikiPage("dummyPage");
39
40 /**
41 * Put plugin into the page context so templates may access it.
42 */
43 public void init(RollerRequest rreq, Context ctx)
44 {
45 try
46 {
47 mWikiEngine = WikiEngine.getInstance(
48 rreq.getPageContext().getServletConfig());
49 mWikiContext = new WikiContext( mWikiEngine, mWikiPage );
50 ctx.put("wikiPlugin",this);
51 }
52 catch (Exception e)
53 {
54 mLogger.error("ERROR initializing WikiPlugin",e);
55 }
56 }
57
58 /**
59 * Convert an input string that contains text that uses JSPWiki
60 * syntax to an output string in HTML format.
61 * @param src Input string that uses JSPWiki syntax
62 * @return Output string in HTML format.
63 */
64 public String render( String src )
65 {
66 String ret = null;
67 try
68 {
69 StringReader reader = new StringReader(src);
70 TranslatorReader tr = new TranslatorReader( mWikiContext, reader );
71 ret = FileUtil.readContents( tr );
72 }
73 catch (Exception e)
74 {
75 mLogger.error("ERROR rendering Wiki text",e);
76 }
77 return ret;
78 }
79
80 /** Return URL to the Wiki page for a weblog entry, CamelCase style */
81 public String makeCamelCaseWikiLink( WeblogEntryData wd, String prefix )
82 {
83 StringBuffer sb = new StringBuffer();
84 StringTokenizer toker = new StringTokenizer(wd.getAnchor(),"_");
85 while ( toker.hasMoreTokens() )
86 {
87 String token = toker.nextToken();
88 sb.append( token.substring(0,1).toUpperCase() );
89 sb.append( token.substring(1) );
90 }
91 return mWikiEngine.getBaseURL()+"Wiki.jsp?page="+prefix+sb.toString();
92 }
93
94 /** Return URL to the Wiki page for a weblog entry, spacey style */
95 public String makeSpacedWikiLink( WeblogEntryData wd, String prefix )
96 {
97 StringBuffer sb = new StringBuffer();
98 StringTokenizer toker = new StringTokenizer(wd.getAnchor(),"_");
99 while ( toker.hasMoreTokens() )
100 {
101 sb.append( toker.nextToken() );
102 if ( toker.hasMoreTokens() ) sb.append("%20");
103 }
104 return mWikiEngine.getBaseURL()+"Wiki.jsp?page="+prefix+sb.toString();
105 }
106 }