KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > velocity > plugins > jspwiki > WikiPlugin


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 JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
18
19 /**
20  * Wiki plugin using the JSPWiki WikiEngine. If you want Wiki links to point
21  * to your JSPWiki, then edit the jspwiki.properties fiel in Roller's WEB-INF
22  * directory and set the jspwiki.baseURL appropriately. For example, if your
23  * Wiki is on your localhost in the Servlet Context named 'wiki' then you'd
24  * set jspwiki.baseURL like so:<br />
25  * <br />
26  * jspwiki.baseURL=http://local:8080/wiki/<br />
27  *
28  * @author David M Johnson
29  */

30 public class WikiPlugin implements PagePlugin
31 {
32     protected String JavaDoc name = "JSPWiki Syntax";
33     public String JavaDoc 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 JavaDoc toString() { return name; }
50     
51     /**
52      * Initialize the JSPWiki Engine if not done so already.
53      * Put the Plugin into the VelocityContex (is this still necessary?-Lance).
54      */

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 JavaDoc e)
79         {
80             mLogger.error("ERROR initializing WikiPlugin",e);
81         }
82     }
83     
84     /**
85      * Convert an input string that contains text that uses JSPWiki
86      * syntax to an output string in HTML format.
87      * @param src Input string that uses JSPWiki syntax
88      * @return Output string in HTML format.
89      */

90     public String JavaDoc render( String JavaDoc src )
91     {
92         String JavaDoc ret = null;
93         try
94         {
95             StringReader JavaDoc reader = new StringReader JavaDoc(src);
96             TranslatorReader tr = new TranslatorReader( mWikiContext, reader );
97             ret = FileUtil.readContents( tr );
98         }
99         catch (Exception JavaDoc e)
100         {
101             mLogger.error("ERROR rendering Wiki text",e);
102         }
103         return ret;
104     }
105     
106     public String JavaDoc render( WeblogEntryData entry, boolean skipFlag)
107     {
108         return render(entry.getText());
109     }
110     /** Return URL to the Wiki page for a weblog entry, CamelCase style */
111     public String JavaDoc makeCamelCaseWikiLink( WeblogEntryData wd, String JavaDoc prefix )
112     {
113         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
114         StringTokenizer JavaDoc toker = new StringTokenizer JavaDoc(wd.getAnchor(),"_");
115         while ( toker.hasMoreTokens() )
116         {
117             String JavaDoc 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     /** Return URL to the Wiki page for a weblog entry, spacey style */
125     public String JavaDoc makeSpacedWikiLink( WeblogEntryData wd, String JavaDoc prefix )
126     {
127         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
128         StringTokenizer JavaDoc toker = new StringTokenizer JavaDoc(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 JavaDoc getName() { return name; }
138     public String JavaDoc getDescription() { return StringEscapeUtils.escapeJavaScript(description); }
139 }
140
Popular Tags