001: /*
002: WikiForms - a WikiPage FORM handler for JSPWiki.
003:
004: Copyright (C) 2003 BaseN.
005:
006: JSPWiki Copyright (C) 2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)
007:
008: This program is free software; you can redistribute it and/or modify
009: it under the terms of the GNU Lesser General Public License as published
010: by the Free Software Foundation; either version 2.1 of the License, or
011: (at your option) any later version.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU Lesser General Public License for more details.
017:
018: You should have received a copy of the GNU Lesser General Public License
019: along with this program; if not, write to the Free Software
020: */
021: package com.ecyrd.jspwiki.forms;
022:
023: import java.text.MessageFormat;
024: import java.util.Map;
025: import java.util.ResourceBundle;
026:
027: import com.ecyrd.jspwiki.WikiContext;
028: import com.ecyrd.jspwiki.plugin.PluginException;
029: import com.ecyrd.jspwiki.plugin.WikiPlugin;
030:
031: /**
032: * Opens a WikiForm.
033: *
034: * Builds the HTML code for opening a FORM.
035: *
036: * <p>Since we're only providing an opening FORM tag, we can't use
037: * the ECS utilities.
038: *
039: * A Form plugin line that produces one looks like this:
040: * <p><pre>
041: * [{FormOpen name='formname' handler='pluginname'
042: * submit='submitservlet'
043: * show='always'
044: * }]
045: * </pre>
046: *
047: * <p>Mandatory parameters:
048: * <br>The <i>name</i> field identifies this particular form to the
049: * Form plugin across pages.
050: * <br>The <i>handler</i> field is a WikiPlugin name; it will be
051: * invoked with the form field values.
052: *
053: * <p>Optional parameters:
054: * <p>The submitservlet is the name of a JSP/servlet capable of
055: * handling the input from this form. It is optional; the default
056: * value is the current page (which can handle the input by using
057: * this Plugin.)
058: *
059: * <p>The <i>hide</i> parameter affects the visibility of this
060: * form. If left out, the form is always shown. If set to
061: * 'onsuccess', the form is not shown if it was submitted
062: * successfully. (Note that a reload of the page would cause the
063: * context to reset, and the form would be shown again. This may
064: * be a useless option.)
065: *
066: * @author ebu
067: */
068: public class FormOpen extends FormElement {
069: private static org.apache.log4j.Logger log = org.apache.log4j.Logger
070: .getLogger(FormOpen.class);
071:
072: public static final String PARAM_METHOD = "method";
073:
074: /**
075: */
076: public String execute(WikiContext ctx, Map params)
077: throws PluginException {
078: ResourceBundle rb = ctx
079: .getBundle(WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
080: String formName = (String) params.get(PARAM_FORM);
081: if (formName == null) {
082: Object[] args = { PARAM_FORM };
083: throw new PluginException(MessageFormat.format(rb
084: .getString("formopen.missingparam"), args));
085: }
086: String hide = (String) params.get(PARAM_HIDEFORM);
087: String sourcePage = ctx.getPage().getName();
088: String submitServlet = (String) params.get(PARAM_SUBMITHANDLER);
089: if (submitServlet == null)
090: submitServlet = ctx.getURL(WikiContext.VIEW, sourcePage);
091:
092: String method = (String) params.get(PARAM_METHOD);
093: if (method == null)
094: method = "post";
095:
096: if (!(method.equalsIgnoreCase("get") || method
097: .equalsIgnoreCase("post"))) {
098: throw new PluginException(rb
099: .getString("formopen.postorgetonly"));
100: }
101:
102: FormInfo info = getFormInfo(ctx);
103: if (info != null) {
104: // Previous information may be the result of submitting
105: // this form, or of a FormSet plugin, or both. If it
106: // exists and is for this form, fine.
107: if (formName.equals(info.getName())) {
108: log
109: .debug("Previous FormInfo for this form was found in context.");
110: // If the FormInfo exists, and if we're supposed to display on
111: // error only, we need to exit now.
112: if (hide != null && HIDE_SUCCESS.equals(hide)
113: && info.getStatus() == FormInfo.EXECUTED) {
114: info.setHide(true);
115: return ("<p>"
116: + rb.getString("formopen.noneedtoshow") + "</p>");
117: }
118: } else {
119: // This would mean that a new form was started without
120: // closing an old one. Get rid of the garbage.
121: info = new FormInfo();
122: }
123: } else {
124: // No previous FormInfo available; store now, so it'll be
125: // available for upcoming Form input elements.
126: info = new FormInfo();
127: storeFormInfo(ctx, info);
128: }
129:
130: info.setName(formName);
131: info.setAction(submitServlet);
132:
133: StringBuffer tag = new StringBuffer(40);
134: tag.append("<div class=\"wikiform\">\n");
135: tag.append("<form action=\"" + submitServlet);
136: tag.append("\" name=\"" + formName);
137: tag.append("\" accept-charset=\""
138: + ctx.getEngine().getContentEncoding());
139: tag
140: .append("\" method=\""
141: + method
142: + "\" enctype=\"application/x-www-form-urlencoded\">\n");
143: tag.append(" <input type=\"hidden\" name=\""
144: + PARAM_FORMNAMEHIDDEN);
145: tag.append("\" value=\"" + formName + "\"/>\n");
146:
147: return tag.toString();
148: }
149:
150: }
|