01: /*
02: WikiForms - a WikiPage FORM handler for JSPWiki.
03:
04: Copyright (C) 2003 BaseN.
05:
06: JSPWiki Copyright (C) 2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)
07:
08: This program is free software; you can redistribute it and/or modify
09: it under the terms of the GNU Lesser General Public License as published
10: by the Free Software Foundation; either version 2.1 of the License, or
11: (at your option) any later version.
12:
13: This program is distributed in the hope that it will be useful,
14: but WITHOUT ANY WARRANTY; without even the implied warranty of
15: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: GNU Lesser General Public License for more details.
17:
18: You should have received a copy of the GNU Lesser General Public License
19: along with this program; if not, write to the Free Software
20: */
21: package com.ecyrd.jspwiki.forms;
22:
23: import com.ecyrd.jspwiki.*;
24: import com.ecyrd.jspwiki.plugin.*;
25: import java.util.*;
26:
27: /**
28: * FormSet is a companion WikiPlugin for Form.
29: *
30: * <p>The mandatory 'form' parameter specifies which form the variable
31: * applies to. Any other parameters are put directly into a FormInfo
32: * object that will be available to a Form plugin called 'form'
33: * (presumably invoked later on the same WikiPage).
34: *
35: * <p>If the name of a FormSet parameter is the same as the name of
36: * a Form plugin input element later on the same page, the Form will
37: * consider the given value the default for the form field. (However,
38: * the handler for the Form is free to use the value as it wishes, and
39: * even override it.)
40: *
41: * <p>If the name of a parameter is not present in Form input fields,
42: * the parameter is presumably meant for sending initial information
43: * to the Form handler. If this is the case, you may want to specify the
44: * <i>populate=''</i> in the Form <i>open</i> element, otherwise the
45: * form won't be displayed on the first invocation.
46: *
47: * <p>This object looks for a FormInfo object named
48: * FORM_VALUES_CARRIER in the WikiContext. If found, it checks that
49: * its name matches the 'form' parameter, and if it does, adds the
50: * plugin parameters to the FormInfo. If the names don't match, the
51: * old FormInfo is discarded and a new one is created. Only one
52: * FormInfo is supported at a time. A practical consequence of this is
53: * that a FormSet invocation only applies to the Form plugins that
54: * follow it; any further Forms need new FormSet calls.
55: *
56: * @see FormInfo
57: * @author ebu
58: */
59: public class FormSet implements WikiPlugin {
60: public String execute(WikiContext ctx, Map params)
61: throws PluginException {
62: String formName = (String) params.get(FormElement.PARAM_FORM);
63: if (formName == null || formName.trim().length() == 0) {
64: return "";
65: }
66:
67: FormInfo info = (FormInfo) ctx
68: .getVariable(FormElement.FORM_VALUES_CARRIER);
69:
70: if (info == null || formName.equals(info.getName()) == false) {
71: info = new FormInfo();
72: ctx.setVariable(FormElement.FORM_VALUES_CARRIER, info);
73: }
74:
75: //
76: // Create a copy for the context. Unfortunately we need to
77: // create slightly modified copy, because otherwise on next
78: // invocation this might be coming from a cache; so we can't
79: // modify the original param string.
80: //
81: info.setName(formName);
82: HashMap hm = new HashMap();
83: hm.putAll(params);
84:
85: hm.remove(FormElement.PARAM_FORM);
86: info.addSubmission(hm);
87:
88: return "";
89: }
90: }
|