This is version . It is not the current version, and thus it cannot be edited.
[Back to current version]   [Restore this version]

This plugin will return a random single line from any Wiki page. I use this to implement 'fortune cookies' on my Wiki. The format for using this plugin is: Plugin insertion failed: Could not find plugin net.colbadhombre.JSPWiki.plugins.FortuneCookiePluginPlugin insertion failed: Could not find plugin net.colbadhombre.JSPWiki.plugins.FortuneCookiePlugin 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 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. Sorry, I don't have a JAR to attach. The JAR I do have has lots of other things in it that would just confuse matters here. I'll see about getting one here soon that includes both the source and the class.

The source has no copyright. Feel free to use it whereever and however you'd like.

Thanks to Janne for JSPWiki.

-- Bradford Holcombe


Back to CategoryThirdPartyPlugins.


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;

/** Returns a random line from a file, like a saying from a list of fortunes. If the first line of the file is an integer, that is interpreted as the number of fortunes in the file. Providing this allows this plugin to skip parsing the file each time to determine how many lines there are. The format of the fortune file need only be lines each delimited by newlines, with the optional count as the first line. Blank lines are ignored, as are any lines starting with the Java line comment characters '//' (with arbitrary white space in front). Usage: Plugin insertion failed: Could not find plugin net.colbadhombre.JSPWiki.plugins.FortuneCookiePluginPlugin insertion failed: Could not find plugin net.colbadhombre.JSPWiki.plugins.FortuneCookiePlugin @author bholcombe */

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) { // } } }

return line; } }

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 particular version was published on 19-Nov-2003 06:23 by 171.75.64.141.