01: // DirectoryResource.java
02: // $Id: CounterCommand.java,v 1.4 2000/08/16 21:37:47 ylafon Exp $
03: // (c) COPYRIGHT MIT and INRIA, 1996.
04: // Please first read the full copyright statement in file COPYRIGHT.html
05:
06: package org.w3c.jigsaw.ssi.commands;
07:
08: import java.util.Dictionary;
09:
10: import org.w3c.www.http.HTTP;
11:
12: import org.w3c.jigsaw.http.Reply;
13: import org.w3c.jigsaw.http.Request;
14:
15: import org.w3c.util.ArrayDictionary;
16:
17: import org.w3c.jigsaw.ssi.SSIFrame;
18:
19: /**
20: * Implementation of the SSI <code>counter</code> command.
21: * Used to do things like cpt = cpt + 1.
22: * @author Benoit Mahe <bmahe@sophia.inria.fr>
23: */
24: public class CounterCommand implements Command {
25: private final static String NAME = "cpt";
26: private final static boolean debug = true;
27:
28: private static final String keys[] = { "name", "init", "incr",
29: "value" };
30:
31: protected final int defaultinit = 0;
32:
33: public String getName() {
34: return NAME;
35: }
36:
37: public String getValue(Dictionary variables, String var,
38: Request request) {
39: return String.valueOf(getCounterValue(variables, var));
40: }
41:
42: protected void initCounterValue(Dictionary d, String name,
43: String value) {
44: d.put(getClass().getName() + "." + name, new Integer(value));
45: }
46:
47: protected void changeCounterValue(Dictionary d, String name,
48: String incr) {
49: int change = (Integer.valueOf(incr)).intValue();
50: int value = getCounterValue(d, name) + change;
51: d.put(getClass().getName() + "." + name, new Integer(value));
52: }
53:
54: protected int getCounterValue(Dictionary d, String name) {
55: Integer value = (Integer) d.get(getClass().getName() + "."
56: + name);
57: if (value != null)
58: return value.intValue();
59: else
60: return defaultinit;
61: }
62:
63: /**
64: * return true if reply can be cached.
65: * @return a boolean.
66: */
67: public boolean acceptCaching() {
68: return true;
69: }
70:
71: public Reply execute(SSIFrame ssiframe, Request request,
72: ArrayDictionary parameters, Dictionary variables) {
73: Object values[] = parameters.getMany(keys);
74: String name = (String) values[0];
75: String init = (String) values[1];
76: String incr = (String) values[2];
77: String value = (String) values[3];
78: String text = null;
79: if (name != null) {
80: if (init != null)
81: initCounterValue(variables, name, init);
82: if (incr != null) {
83: changeCounterValue(variables, name, incr);
84: }
85: if (value != null) {
86: text = String.valueOf(getCounterValue(variables, name));
87: }
88: }
89: Reply reply = ssiframe.createCommandReply(request, HTTP.OK);
90: if (text != null)
91: reply.setContent(text);
92: return reply;
93: }
94:
95: }
|