View Javadoc

1   /*
2    * $Id: BaseFieldTag.java 376841 2006-02-10 21:01:28Z husted $
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  package org.apache.struts.taglib.html;
19  
20  import org.apache.struts.taglib.TagUtils;
21  
22  import javax.servlet.jsp.JspException;
23  
24  /***
25   * Convenience base class for the various input tags for text fields.
26   *
27   * @version $Rev: 376841 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
28   *          $
29   */
30  public abstract class BaseFieldTag extends BaseInputTag {
31      // ----------------------------------------------------- Instance Variables
32  
33      /***
34       * Comma-delimited list of content types that a server processing this
35       * form will handle correctly.  This property is defined only for the
36       * <code>file</code> tag, but is implemented here because it affects the
37       * rendered HTML of the corresponding &lt;input&gt; tag.
38       */
39      protected String accept = null;
40  
41      /***
42       * The "redisplay contents" flag (used only on <code>password</code>).
43       */
44      protected boolean redisplay = true;
45  
46      /***
47       * The type of input field represented by this tag (text, password, or
48       * hidden).
49       */
50      protected String type = null;
51  
52      public String getAccept() {
53          return (this.accept);
54      }
55  
56      public void setAccept(String accept) {
57          this.accept = accept;
58      }
59  
60      public boolean getRedisplay() {
61          return (this.redisplay);
62      }
63  
64      public void setRedisplay(boolean redisplay) {
65          this.redisplay = redisplay;
66      }
67  
68      // --------------------------------------------------------- Public Methods
69  
70      /***
71       * Generate the required input tag. <p> Support for indexed property since
72       * Struts 1.1
73       *
74       * @throws JspException if a JSP exception has occurred
75       */
76      public int doStartTag() throws JspException {
77          TagUtils.getInstance().write(this.pageContext, this.renderInputElement());
78  
79          return (EVAL_BODY_TAG);
80      }
81  
82      /***
83       * Renders a fully formed &lt;input&gt; element.
84       *
85       * @throws JspException
86       * @since Struts 1.2
87       */
88      protected String renderInputElement()
89          throws JspException {
90          StringBuffer results = new StringBuffer("<input");
91  
92          prepareAttribute(results, "type", this.type);
93          prepareAttribute(results, "name", prepareName());
94          prepareAttribute(results, "accesskey", getAccesskey());
95          prepareAttribute(results, "accept", getAccept());
96          prepareAttribute(results, "maxlength", getMaxlength());
97          prepareAttribute(results, "size", getCols());
98          prepareAttribute(results, "tabindex", getTabindex());
99          prepareValue(results);
100         results.append(this.prepareEventHandlers());
101         results.append(this.prepareStyles());
102         prepareOtherAttributes(results);
103         results.append(this.getElementClose());
104 
105         return results.toString();
106     }
107 
108     /***
109      * Render the value element
110      *
111      * @param results The StringBuffer that output will be appended to.
112      */
113     protected void prepareValue(StringBuffer results)
114         throws JspException {
115         results.append(" value=\"");
116 
117         if (value != null) {
118             results.append(this.formatValue(value));
119         } else if (redisplay || !"password".equals(type)) {
120             Object value =
121                 TagUtils.getInstance().lookup(pageContext, name, property, null);
122 
123             results.append(this.formatValue(value));
124         }
125 
126         results.append('"');
127     }
128 
129     /***
130      * Return the given value as a formatted <code>String</code>.  This
131      * implementation escapes potentially harmful HTML characters.
132      *
133      * @param value The value to be formatted. <code>null</code> values will
134      *              be returned as the empty String "".
135      * @throws JspException if a JSP exception has occurred
136      * @since Struts 1.2
137      */
138     protected String formatValue(Object value)
139         throws JspException {
140         if (value == null) {
141             return "";
142         }
143 
144         return TagUtils.getInstance().filter(value.toString());
145     }
146 
147     /***
148      * Release any acquired resources.
149      */
150     public void release() {
151         super.release();
152         accept = null;
153         name = Constants.BEAN_KEY;
154         redisplay = true;
155     }
156 }