01: /*
02: JSPWiki - a JSP-based WikiWiki clone.
03:
04: Copyright (C) 2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)
05:
06: This program is free software; you can redistribute it and/or modify
07: it under the terms of the GNU Lesser General Public License as published by
08: the Free Software Foundation; either version 2.1 of the License, or
09: (at your option) any later version.
10:
11: This program is distributed in the hope that it will be useful,
12: but WITHOUT ANY WARRANTY; without even the implied warranty of
13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: GNU Lesser General Public License for more details.
15:
16: You should have received a copy of the GNU Lesser General Public License
17: along with this program; if not, write to the Free Software
18: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: */
20: package com.ecyrd.jspwiki.plugin;
21:
22: import com.ecyrd.jspwiki.*;
23: import java.util.*;
24:
25: /**
26: * Provides a page-specific counter.
27: * <P>Parameters
28: * <UL>
29: * <LI>name - Name of the counter. Optional.
30: * </UL>
31: *
32: * Stores a variable in the WikiContext called "counter", with the name of the
33: * optionally attached. For example:<BR>
34: * If name is "thispage", then the variable name is called "counter-thispage".
35: *
36: * @since 1.9.30
37: * @author Janne Jalkanen
38: */
39: public class Counter implements WikiPlugin {
40: // private static Logger log = Logger.getLogger( Counter.class );
41:
42: static final String VARIABLE_NAME = "counter";
43:
44: public String execute(WikiContext context, Map params)
45: throws PluginException {
46: //
47: // First, determine which kind of name we use to store in
48: // the WikiContext.
49: //
50: String countername = (String) params.get("name");
51:
52: if (countername == null) {
53: countername = VARIABLE_NAME;
54: } else {
55: countername = VARIABLE_NAME + "-" + countername;
56: }
57:
58: //
59: // Fetch, increment, and store back.
60: //
61: Integer val = (Integer) context.getVariable(countername);
62:
63: if (val == null) {
64: val = new Integer(0);
65: }
66:
67: val = new Integer(val.intValue() + 1);
68:
69: context.setVariable(countername, val);
70:
71: return val.toString();
72: }
73:
74: }
|