KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > html > BaseFieldTag


1 /*
2  * $Id: BaseFieldTag.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.taglib.html;
20
21 import javax.servlet.jsp.JspException JavaDoc;
22
23 import org.apache.struts.taglib.TagUtils;
24
25 /**
26  * Convenience base class for the various input tags for text fields.
27  *
28  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
29  */

30
31 public abstract class BaseFieldTag extends BaseInputTag {
32
33     // ----------------------------------------------------- Instance Variables
34

35     /**
36      * Comma-delimited list of content types that a server processing this form
37      * will handle correctly. This property is defined only for the
38      * <code>file</code> tag, but is implemented here because it affects the
39      * rendered HTML of the corresponding &lt;input&gt; tag.
40      */

41     protected String JavaDoc accept = null;
42
43     public String JavaDoc getAccept() {
44         return (this.accept);
45     }
46
47     public void setAccept(String JavaDoc accept) {
48         this.accept = accept;
49     }
50
51     /**
52      * The "redisplay contents" flag (used only on <code>password</code>).
53      */

54     protected boolean redisplay = true;
55
56     public boolean getRedisplay() {
57         return (this.redisplay);
58     }
59
60     public void setRedisplay(boolean redisplay) {
61         this.redisplay = redisplay;
62     }
63
64     /**
65      * The type of input field represented by this tag (text, password, or
66      * hidden).
67      */

68     protected String JavaDoc type = null;
69
70     // --------------------------------------------------------- Public Methods
71

72     /**
73      * Generate the required input tag.
74      * <p>
75      * Support for indexed property since Struts 1.1
76      *
77      * @exception JspException if a JSP exception has occurred
78      */

79     public int doStartTag() throws JspException JavaDoc {
80         
81         TagUtils.getInstance().write(this.pageContext, this.renderInputElement());
82
83         return (EVAL_BODY_TAG);
84
85     }
86
87     /**
88      * Renders a fully formed &lt;input&gt; element.
89      * @throws JspException
90      * @since Struts 1.2
91      */

92     protected String JavaDoc renderInputElement() throws JspException JavaDoc {
93         StringBuffer JavaDoc results = new StringBuffer JavaDoc("<input");
94
95         prepareAttribute(results, "type", this.type);
96         prepareAttribute(results, "name", prepareName());
97         prepareAttribute(results, "accesskey", getAccesskey());
98         prepareAttribute(results, "accept", getAccept());
99         prepareAttribute(results, "maxlength", getMaxlength());
100         prepareAttribute(results, "size", getCols());
101         prepareAttribute(results, "tabindex", getTabindex());
102         prepareValue(results);
103         results.append(this.prepareEventHandlers());
104         results.append(this.prepareStyles());
105         prepareOtherAttributes(results);
106         results.append(this.getElementClose());
107         return results.toString();
108     }
109
110     /**
111      * Render the value element
112      * @param results The StringBuffer that output will be appended to.
113      */

114     protected void prepareValue(StringBuffer JavaDoc results) throws JspException JavaDoc {
115
116         results.append(" value=\"");
117         if (value != null) {
118             results.append(this.formatValue(value));
119
120         } else if (redisplay || !"password".equals(type)) {
121             Object JavaDoc value =
122                 TagUtils.getInstance().lookup(pageContext, name, property, null);
123
124             results.append(this.formatValue(value));
125         }
126
127         results.append('"');
128
129     }
130     
131     /**
132      * Return the given value as a formatted <code>String</code>. This
133      * implementation escapes potentially harmful HTML characters.
134      *
135      * @param value The value to be formatted. <code>null</code> values will
136      * be returned as the empty String "".
137      *
138      * @throws JspException if a JSP exception has occurred
139      *
140      * @since Struts 1.2
141      */

142     protected String JavaDoc formatValue(Object JavaDoc value) throws JspException JavaDoc {
143         if (value == null) {
144             return "";
145         }
146
147         return TagUtils.getInstance().filter(value.toString());
148     }
149
150     /**
151      * Release any acquired resources.
152      */

153     public void release() {
154
155         super.release();
156         accept = null;
157         name = Constants.BEAN_KEY;
158         redisplay = true;
159
160     }
161
162 }
163
Popular Tags