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 5 changed 2 lines
[Latest|1] (most up to date )\\
[Archive|2] (previous versions)
[Latest|FortuneCookiePlugin#Latest] (most up to date )\\
[Archive|FortuneCookiePlugin#Archive] (previous versions)
At line 31 changed one line
18-Mar-2005: [FortuneCookiePluginv2.0.52.jar] package. [FortuneCookiePlugin.java] source.\\
18-Mar-2005: [FortuneCookiePluginv2.0.52.jar] package.\\
At line 34 changed 2 lines
05-Oct-2004: [FortuneCookies.jar] package.\\
-- user: ''(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.)''
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 39 changed one line
The fortunes may be styled like any regular
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 48 added one line
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 50 added 2 lines
%%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 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 57 added 5 lines
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 46 changed 171 lines
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'}]
}}}
%%commentbox
If you intend to use the [FortuneCookiePlugin_v2.6.4.jar], then please see the explanations at the end of this page.
%%
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.
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.
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.
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.
Thanks to Janne for JSPWiki.
__05OCT2004 11:28am UPDATE__\\
-- [Bradford Holcombe]
----
Source:
{{{
package net.colbadhombre.JSPWiki.plugins;
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;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;
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 );
String line = "";
BufferedReader br = null;
try
{
br = new BufferedReader(new StringReader(pageData));
line = br.readLine();
if (line == null)
{
return "Fortune file empty (" + pageName + ")";
}
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();
}
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();
}
}
}}}
----
----
I did some optimizing work on the plugin. Download the [FortuneCookiePlugin_v2.6.4.jar].
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 218 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