View Javadoc

1   /*
2    * $Id: FormTag.java 130 2004-11-28 13:00:44Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.tarsis.taglib.form;
24  
25  import java.io.IOException;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.jsp.JspException;
29  
30  import org.talika.tarsis.Globals;
31  
32  /**
33   * Implementation of form tag.
34   *
35   * @author  Jose M. Palomar
36   * @version $Revision: 130 $
37   */
38  public final class FormTag extends BaseTag {
39  
40      // Fields
41      /**
42       * <code>action</code> attribute.
43       */
44      private String action;
45  
46      /**
47       * <code>method</code> attribute.
48       */
49      private String method = "POST";
50  
51      /**
52       * <code>name</code> attribute.
53       */
54      private String name;
55  
56      /**
57       * <code>enctype</code> attribute.
58       */
59      private String enctype;
60  
61      /**
62       * <code>target</code> attribute.
63       */
64      private String target;
65  
66      /**
67       * <code>onsubmit</code> attribute.
68       */
69      private String onsubmit;
70  
71      /**
72       * <code>onreset</code> attribute.
73       */
74      private String onreset;
75  
76      /**
77       * <code>command</code> attribute.
78       */
79      private String command;
80  
81      // Constructors
82      /**
83       * Creates a new <code>FormTag</code> object.
84       */
85      public FormTag() {
86      }
87  
88      // Methods
89      /**
90       * Process the start tag for this instance.
91       *
92       * @return int EVAL_BODY_INCLUDE.
93       * @throws JspException if somethign goes wrong during processing.
94       * @see javax.servlet.jsp.tagext.Tag#doStartTag()
95       */
96      public int doStartTag() throws JspException {
97  
98          try {
99              pageContext.getOut().print(renderFormStartElement());
100         }
101         catch (IOException ioe) {
102             // Bad luck
103         }
104 
105         return EVAL_BODY_INCLUDE;
106 
107     }
108 
109     /**
110      * Process the end tag for this instance.
111      *
112      * @return int EVAL_PAGE.
113      * @throws JspException if somethign goes wrong during processing.
114      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
115      */
116     public int doEndTag() throws JspException {
117 
118         try {
119             pageContext.getOut().print(renderFormEndElement());
120         }
121         catch (IOException ioe) {
122             // Bad luck
123         }
124 
125         return EVAL_PAGE;
126     }
127 
128     // Renderer
129     /**
130      * Renders form start tag.
131      *
132      * @return String a string with rendered form start tag.
133      */
134     public String renderFormStartElement() {
135 
136         StringBuffer element = new StringBuffer();
137 
138         element.append("<form");
139 
140         element.append(baseAttributes());
141 
142         if (name != null) {
143             element.append(" name=\"");
144             element.append(name);
145             element.append("\"");
146         }
147 
148         if (action == null && command != null) {
149             setCommandAction();
150         }
151 
152         if (action != null) {
153             element.append(" action=\"");
154             element.append(action);
155             element.append("\"");
156         }
157 
158         if (method != null) {
159             element.append(" method=\"");
160             element.append(method);
161             element.append("\"");
162         }
163 
164         if (enctype != null) {
165             element.append(" enctype=\"");
166             element.append(enctype);
167             element.append("\"");
168         }
169 
170         if (target != null) {
171             element.append(" target=\"");
172             element.append(target);
173             element.append("\"");
174         }
175 
176         if (onsubmit != null) {
177             element.append(" onsubmit=\"");
178             element.append(onsubmit);
179             element.append("\"");
180         }
181 
182         if (onreset != null) {
183             element.append(" onreset=\"");
184             element.append(onreset);
185             element.append("\"");
186         }
187 
188         element.append(">");
189 
190         return element.toString();
191 
192     }
193 
194     /**
195      * Renders form end tag.
196      *
197      * @return String a string with rendered form end tag.
198      */
199     public String renderFormEndElement() {
200         return "</form>";
201     }
202 
203     /**
204      * Sets <code>action</code> attribute according to controller servlet.
205      */
206     public void setCommandAction() {
207 
208         StringBuffer act = new StringBuffer();
209         HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
210 
211         act.append(request.getContextPath());
212         act.append(Globals.CONTROLLER_SERVLET);
213         if (!command.startsWith("/")) {
214             act.append('/');
215         }
216         act.append(command);
217 
218         setAction(act.toString());
219 
220     }
221 
222     // Attributes
223     /**
224      * Sets <code>action</code> attribute value.
225      *
226      * @param action String <code>action</code> attribute value.
227      */
228     public void setAction(String action) {
229         this.action = action;
230     }
231 
232     /**
233      * Sets <code>method</code> attribute value.
234      *
235      * @param method String <code>method</code> attribute value.
236      */
237     public void setMethod(String method) {
238         this.method = method;
239     }
240 
241     /**
242      * Sets <code>name</code> attribute value.
243      *
244      * @param name String <code>name</code> attribute value.
245      */
246     public void setName(String name) {
247         this.name = name;
248     }
249 
250     /**
251      * Sets <code>enctype</code> attribute value.
252      *
253      * @param enctype String <code>enctype</code> attribute value.
254      */
255     public void setEnctype(String enctype) {
256         this.enctype = enctype;
257     }
258 
259     /**
260      * Sets <code>target</code> attribute value.
261      *
262      * @param target String <code>target</code> attribute value.
263      */
264     public void setTarget(String target) {
265         this.target = target;
266     }
267 
268     /**
269      * Sets <code>onsubmit</code> attribute value.
270      *
271      * @param onsubmit String <code>onsubmit</code> attribute value.
272      */
273     public void setOnsubmit(String onsubmit) {
274         this.onsubmit = onsubmit;
275     }
276 
277     /**
278      * Sets <code>onreset</code> attribute value.
279      *
280      * @param onreset String <code>onreset</code> attribute value.
281      */
282     public void setOnreset(String onreset) {
283         this.onreset = onreset;
284     }
285 
286     /**
287      * Sets <code>command</code> attribute value.
288      *
289      * @param command String <code>command</code> attribute value.
290      */
291     public void setCommand(String command) {
292         this.command = command;
293     }
294 
295     /**
296      * Called on a Tag handler to release state.
297      *
298      * @see javax.servlet.jsp.tagext.Tag#release()
299      */
300     public void release() {
301 
302         super.release();
303         this.action = null;
304         this.method = "POST";
305         this.name = null;
306         this.enctype = null;
307         this.method = null;
308         this.target = null;
309         this.onsubmit = null;
310         this.onreset = null;
311         this.command = null;
312 
313     }
314 
315 }