Add new attachment

Only authorized users are allowed to upload new attachments.

List of attachments

Kind Attachment Name Size Version Date Modified Author Change note
java
FortuneCookie.java 4.0 kB 1 25-Sep-2008 04:35 Maik Scheibler
java
FortuneCookiePlugin.java 3.2 kB 1 25-Sep-2008 14:02 Maik Scheibler moved source code from page to attachment
jar
FortuneCookiePlugin_v2.6.4.jar 3.3 kB 1 25-Sep-2008 04:14 Maik Scheibler
jar
FortuneCookiePluginv2.0.52.jar 2.3 kB 1 18-Mar-2005 22:41 Mbarcia
jar
FortuneCookies.jar 3.1 kB 2 05-Oct-2004 18:27 BradfordHolcombe

This page (revision-28) was last changed on 25-Sep-2008 16:02 by Maik Scheibler  

This page was created on 19-Nov-2003 06:23 by 171.75.64.141

Only authorized users are allowed to rename pages.

Only authorized users are allowed to delete pages.

Difference between version and

At line 1 changed 17 lines
This plugin will return a random single line from any Wiki page. I use this to implement 'fortune cookies' on my [wiki|http://colbadhombre.net/SecularWiki/] (See the bottom of the page).
The format for using this plugin is
* for {{~FortuneCookies.jar}}:
{{{
[{INSERT com.bradfordholcombe.JSPWiki.plugins.FortuneCookiePlugin WHERE page='Sayings'}]
}}}
* for the other version, {{~FortuneCookiePluginv2.0.52.jar}}, use:
{{{
[{INSERT net.colbadhombre.JSPWiki.plugins.FortuneCookiePlugin WHERE page='Sayings'}]
}}}
* for the other version, {{[FortuneCookiePlugin_v2.6.4.jar]}}, use:
{{{
[{de.ldj.jspwiki.plugin.FortuneCookie page='FortuneCookies'}]
}}}
[{TableOfContents }]
At line 19 changed one line
If you intend to use the [FortuneCookiePlugin_v2.6.4.jar], then please see the explanations at the end of this page.
__Versions :__\\
-----
[Latest|FortuneCookiePlugin#Latest] (most up to date )\\
[Archive|FortuneCookiePlugin#Archive] (previous versions)
At line 22 changed one line
The source page should be a list of newline delimited sayings. Optionally, the first line can be a number that represents the number of sayings on the page. Specifying this number allows the plugin to skip parsing the whole page once to count the number of lines. Also, lines starting with '//' or whitespace followed by '//' will be ignored.
!! Description
This plugin will return a random single line from any designated Wiki page on each call. Thus each line of this page becomes a "[fortune|Wikipedia:Fortune_cookie]".
At line 24 removed one line
This plugin was developed under 2.1.82 CVS/JDK1.4.2, and I haven't tried it anywhere else. Let me know if I should make any changes for compatability reasons.
At line 26 changed one line
I use JSPWiki for my home site, so I have a lot of these plugins doing things like reporting the local weather, what is playing on my [SliMP3|http://www.slimdevices.com/], and even the level of the river that runs in front of my house! Let me know if any of that sounds fun, and I'll post it.
!! Download
Please choose your plugin version:
At line 28 changed one line
The source code is below and a JAR is attached at the bottom. The source has no copyright. Feel free to use it whereever and however you'd like.
! Latest
This is the most up to date version of the plugin.
At line 30 changed one line
Thanks to Janne for JSPWiki.
25-Sep-2008: [FortuneCookiePlugin_v2.6.4.jar] package. [FortuneCookie.java] source.\\
''(Tested with JSPWiki 2.6.4 and Sun JDK 1.6.0)''
At line 32 changed 2 lines
__05OCT2004 11:28am UPDATE__\\
I have added code to remove list wiki markup from each line. This allows the [original page|http://colbadhombre.net/SecularWiki/Wiki.jsp?page=Sayings] to use the wiki list syntax, so the fortune line looks better when rendered in its own page. Thanks to [suggestions|EmbeddingJSPWiki] from Janne, I have added a call to the wiki tranlation engine so that each line (minus its original list markup) renders any wiki markup in the returned fortune. The new source is above, and the JAR reflects these changes.
Here are the changes:
* Changed the plugin package and classname, it is now {{{de.ldj.jspwiki.plugin.FortuneCookie}}}
* The fortunes will be cached and only reloaded if the fortunes page becomes modified. A count of fortunes as first line is no longer required/used.
* Removed the usage of deprecated API. ''(I tested with JSPWiki v2.6.4 & JDK 1.6.0, but it should also work with later versions.)''
* Overall source cleanup. ''(see [FortuneCookie.java])''
At line 35 changed one line
-- [Bradford Holcombe]
! Archive
Here you can find more recent versions of the plugin.
At line 37 changed 4 lines
----
Source:
{{{
package net.colbadhombre.JSPWiki.plugins;
18-Mar-2005: [FortuneCookiePluginv2.0.52.jar] package.\\
--[MBarcia]: ''(This plugin was not working for the 2.0.52 stable version. Also attached to this page is the JAR for this version. HTH)''
At line 42 changed 5 lines
import com.ecyrd.jspwiki.WikiContext;
import com.ecyrd.jspwiki.WikiEngine;
import com.ecyrd.jspwiki.WikiPage;
import com.ecyrd.jspwiki.plugin.PluginException;
import com.ecyrd.jspwiki.plugin.WikiPlugin;
05-Oct-2004: [FortuneCookies.jar] package. [FortuneCookiePlugin.java] source.\\
-- [Bradford Holcombe]: ''(I have added code to remove list wiki markup from each line. This allows the [original page|http://colbadhombre.net/SecularWiki/Wiki.jsp?page=Sayings] to use the wiki list syntax, so the fortune line looks better when rendered in its own page. Thanks to [suggestions|EmbeddingJSPWiki] from Janne, I have added a call to the wiki tranlation engine so that each line (minus its original list markup) renders any wiki markup in the returned fortune. The new source is above, and the JAR reflects these changes.)''
At line 48 removed 4 lines
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;
At line 53 changed 8 lines
public class FortuneCookiePlugin implements WikiPlugin
{
public String execute(WikiContext context, Map params) throws PluginException
{
String pageName = (String)params.get("page");
WikiEngine engine = context.getEngine();
WikiPage page = engine.getPage( pageName );
String pageData = engine.getPureText( page );
!! Usage
In order to use this plugin you need to:
# [Download|FortuneCookiePlugin#Download] the plugin JAR
# Add the JAR to your JSPWiki classpath. (consult your ServletContainer manual how to do this)
# Prepare a WikiPage with fortunes. (see [Fortune Format|FortuneCookiePlugin#Fortune Format])
# Call the plugin at any WikiPage (except the one with the fortunes, this would cause an enless recursion)\\ Depending on the version you choose, the call must be different:
** 25-Sep-2008 [FortuneCookiePlugin_v2.6.4.jar]: {{{[{de.ldj.jspwiki.plugin.FortuneCookie page='FortuneCookies'}]}}}
** 18-Mar-2005 [FortuneCookiePluginv2.0.52.jar]: {{{[{net.colbadhombre.JSPWiki.plugins.FortuneCookiePlugin page='Sayings'}]}}}
** 05-Oct-2004 [FortuneCookies.jar]: {{{[{com.bradfordholcombe.JSPWiki.plugins.FortuneCookiePlugin page='Sayings'}]}}}
At line 62 changed 5 lines
String line = "";
BufferedReader br = null;
try
{
br = new BufferedReader(new StringReader(pageData));
The plugin requires the parameter '{{page}}' to work. The value of {{page}} must be the name of an existing WikiPage. This page will be used as fortune page and must not contain a call to the FortuneCookiePlugin, because it might cause an endless recursion. Please find the format specification for the fortune page below.
At line 68 changed 5 lines
line = br.readLine();
if (line == null)
{
return "Fortune file empty (" + pageName + ")";
}
%%warning Each caller must use the same WikiPage as fortune page!\\
The plugin behaviour is undefined if there are calls to different fortune pages.%%
At line 74 removed 17 lines
int lineCount = -1;
try
{
// first line is a count of the number of fortunes in the file, if it is a number.
lineCount = Integer.parseInt(line);
}
catch (NumberFormatException nfe)
{
// not a number, gotta count.
lineCount = 0;
while (line != null)
{
if (line.length() > 0 && !line.trim().startsWith("//"))
{
lineCount++;
}
line = br.readLine();
At line 54 added 2 lines
!! Fortune Format
The fortune cookie plugin uses one WikiPage as fortune source. Each line on this page will become considered a fortune.
At line 93 changed one line
}
The following formatting rules apply to the fortune page:
* The fortune lines must be seperated by new-lines. Forced line breaks within the lines ({{{\\}}}) are not used as fortune delimeter. Thus it is possible to write multi-line fortunes.
* The fortunes can be written as bulleted list (*), numbered list (#), with forced line breaks ('{{{\\}}}' followed by new-line) or as paragraphs (empty lines between each fortune). Even a mix is possible.
* '*' or '#' at the begining of fortune lines will be removed before rendering the fortunes.
* Fortunes with a forced line break at the end of their lines ('{{{\\}}}' followed by whitespace and a new-line) will be rendered without the trailing '{{{\\}}}'.
At line 95 changed 88 lines
br.close();
br = new BufferedReader(new StringReader(pageData));
}
// pick the line to insert
int chosenLine = (int)Math.floor(lineCount * Math.random());
lineCount = 0;
line = br.readLine();
while (lineCount != chosenLine && line != null)
{
line = br.readLine();
if (line.length() > 0 && !line.trim().startsWith("//"))
{
lineCount++;
}
}
br.close();
}
catch (IOException e)
{
return "IO exception with fortune file.";
}
finally
{
if (br != null)
{
try
{
br.close();
}
catch (IOException e)
{
//
}
}
}
if( line.startsWith( "*" ) )
{
line = line.substring( 1 );
}
if( line.endsWith( "\\" ) )
{
line = line.substring( 0, line.length() - 2 );
}
// invoke the wiki markup translator
TranslatorReader reader = new TranslatorReader( context, new StringReader( line ) );
int c;
StringBuffer wikiLine = new StringBuffer();
try
{
while( ( c = reader.read() ) != -1 )
{
wikiLine.append( (char)c );
}
}
catch( IOException ioe )
{
// nothing
}
return wikiLine.toString();
}
}
}}}
----
This plugin was not working for the 2.0.52 stable version. Also attached to this page is the JAR for this version.\\
HTH\\
--[MBarcia]
----
I did some optimizing work on the plugin. Download the [FortuneCookiePlugin_v2.6.4.jar].
Here are the changes:
* Changed the plugin package, the class is now {{{de.ldj.jspwiki.plugin.FortuneCookie}}}
* The sayings will be cached and only reloaded if the sayings page becomes modified. A count of sayings as first line is no longer required/used.
* Removed the usage of deprecated API. ''(I tested with JSPWiki v2.6.4 & JDK 1.6.0, but it should also work with later versions.)''
* Overall source cleanup. ''(see [FortuneCookie.java])''
These things are still the same:
* The plugin still uses the same one parameter to designate the sayings page\\{{{e.g. [{de.ldj.jspwiki.plugin.FortuneCookie page='FortuneCookies'}]}}}
* The sayings can be written as bulleted list (*), numbered list (#), separated by line breaks ({{{\\}}}) or as paragraphs. Even a mix is possible.
* Regular Wiki markup can be used to style the fortunes.
At line 184 removed one line
* Wiki markup can be used within the sayings.
At line 66 added one line
----
Version Date Modified Size Author Changes ... Change note
28 25-Sep-2008 16:02 4.608 kB Maik Scheibler to previous
27 25-Sep-2008 15:41 4.606 kB Maik Scheibler to previous | to last
26 25-Sep-2008 14:00 7.717 kB Maik Scheibler to previous | to last
25 25-Sep-2008 12:45 7.025 kB Maik Scheibler to previous | to last
24 25-Sep-2008 04:58 7.031 kB Maik Scheibler to previous | to last
23 25-Sep-2008 04:52 6.75 kB Maik Scheibler to previous | to last
22 26-Sep-2007 23:21 5.456 kB JanneJalkanen to previous | to last
21 26-Sep-2007 02:18 5.478 kB 221.233.134.87 to previous | to last
« This page (revision-28) was last changed on 25-Sep-2008 16:02 by Maik Scheibler