View Javadoc

1   /*
2    * $Id: CommandTag.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.jsp.JspException;
28  
29  import org.talika.tarsis.Globals;
30  
31  /**
32   * Implementation of command tag.
33   *
34   * @author  Jose M. Palomar
35   * @version $Revision: 130 $
36   */
37  public class CommandTag extends BaseTag {
38  
39      // Fields
40      /**
41       * <code>name</code> attribute. <code>name</code> attribute represents command
42       * name.
43       */
44      private String name;
45  
46      // Constructors
47      /**
48       * Creates a new <code>CommandTag</code> object.
49       */
50      public CommandTag() {
51      }
52  
53      // Methods
54      /**
55       * Process the start tag for this instance.
56       *
57       * @return int SKIP_BODY.
58       * @throws JspException if somethign goes wrong during processing.
59       * @see javax.servlet.jsp.tagext.Tag#doStartTag()
60       */
61      public int doStartTag() throws JspException {
62  
63          try {
64              pageContext.getOut().print(renderInputElement());
65          }
66          catch (IOException ioe) {
67              // Bad luck
68          }
69  
70          return SKIP_BODY;
71  
72      }
73  
74      // Renderer
75      /**
76       * Renders command's input tag.
77       *
78       * @return String a string with rendered input tag.
79       */
80      public final String renderInputElement() {
81  
82          StringBuffer element = new StringBuffer();
83  
84          element.append("<input");
85  
86          element.append(baseAttributes());
87  
88          element.append(" type=\"hidden\"");
89  
90          element.append(controlName());
91          element.append(controlValue());
92  
93          element.append(">");
94  
95          return element.toString();
96  
97      }
98  
99      /**
100      * Renders a string with the value of name attribute.
101      *
102      * @return String a string with the value of name attribute
103      */
104     public final String controlName() {
105 
106         StringBuffer atts = new StringBuffer();
107 
108         if (name != null) {
109             atts.append(" name=\"");
110             atts.append(Globals.COMMAND_NAME_PARAMETER);
111             atts.append("\"");
112         }
113 
114         return atts.toString();
115     }
116 
117     /**
118      * Renders a string with the value of value attribute.
119      *
120      * @return String a string with the value of value attribute
121      */
122     public final String controlValue() {
123 
124         StringBuffer atts = new StringBuffer();
125 
126         if (name != null) {
127             atts.append(" value=\"");
128             atts.append(name);
129             atts.append("\"");
130         }
131 
132         return atts.toString();
133 
134     }
135 
136     /**
137      * Renders a string with values of input attributes.<br>
138      * There is no input attributes for this tag.
139      *
140      * @return String an empty string.
141      */
142     public String inputAttributes() {
143         return "";
144     }
145 
146     // Attributes
147     /**
148      * Sets <code>name</code> attribute value.
149      *
150      * @param name String <code>name</code> attribute value.
151      */
152     public void setName(String name) {
153         this.name = name;
154     }
155 
156     /**
157      * Called on a Tag handler to release state.
158      *
159      * @see javax.servlet.jsp.tagext.Tag#release()
160      */
161     public void release() {
162 
163         super.release();
164 
165         this.name = null;
166 
167     }
168 
169 }