001: /*
002: * $Id: BaseFieldTag.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.taglib.html;
022:
023: import org.apache.struts.taglib.TagUtils;
024:
025: import javax.servlet.jsp.JspException;
026:
027: /**
028: * Convenience base class for the various input tags for text fields.
029: *
030: * @version $Rev: 471754 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
031: * $
032: */
033: public abstract class BaseFieldTag extends BaseInputTag {
034: // ----------------------------------------------------- Instance Variables
035:
036: /**
037: * Comma-delimited list of content types that a server processing this
038: * form will handle correctly. This property is defined only for the
039: * <code>file</code> tag, but is implemented here because it affects the
040: * rendered HTML of the corresponding <input> tag.
041: */
042: protected String accept = null;
043:
044: /**
045: * The "redisplay contents" flag (used only on <code>password</code>).
046: */
047: protected boolean redisplay = true;
048:
049: /**
050: * The type of input field represented by this tag (text, password, or
051: * hidden).
052: */
053: protected String type = null;
054:
055: public String getAccept() {
056: return (this .accept);
057: }
058:
059: public void setAccept(String accept) {
060: this .accept = accept;
061: }
062:
063: public boolean getRedisplay() {
064: return (this .redisplay);
065: }
066:
067: public void setRedisplay(boolean redisplay) {
068: this .redisplay = redisplay;
069: }
070:
071: // --------------------------------------------------------- Public Methods
072:
073: /**
074: * Generate the required input tag. <p> Support for indexed property since
075: * Struts 1.1
076: *
077: * @throws JspException if a JSP exception has occurred
078: */
079: public int doStartTag() throws JspException {
080: TagUtils.getInstance().write(this .pageContext,
081: this .renderInputElement());
082:
083: return (EVAL_BODY_TAG);
084: }
085:
086: /**
087: * Renders a fully formed <input> element.
088: *
089: * @throws JspException
090: * @since Struts 1.2
091: */
092: protected String renderInputElement() throws JspException {
093: StringBuffer results = new StringBuffer("<input");
094:
095: prepareAttribute(results, "type", this .type);
096: prepareAttribute(results, "name", prepareName());
097: prepareAttribute(results, "accesskey", getAccesskey());
098: prepareAttribute(results, "accept", getAccept());
099: 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:
108: return results.toString();
109: }
110:
111: /**
112: * Render the value element
113: *
114: * @param results The StringBuffer that output will be appended to.
115: */
116: protected void prepareValue(StringBuffer results)
117: throws JspException {
118: results.append(" value=\"");
119:
120: if (value != null) {
121: results.append(this .formatValue(value));
122: } else if (redisplay || !"password".equals(type)) {
123: Object value = TagUtils.getInstance().lookup(pageContext,
124: name, property, null);
125:
126: results.append(this .formatValue(value));
127: }
128:
129: results.append('"');
130: }
131:
132: /**
133: * Return the given value as a formatted <code>String</code>. This
134: * implementation escapes potentially harmful HTML characters.
135: *
136: * @param value The value to be formatted. <code>null</code> values will
137: * be returned as the empty String "".
138: * @throws JspException if a JSP exception has occurred
139: * @since Struts 1.2
140: */
141: protected String formatValue(Object value) throws JspException {
142: if (value == null) {
143: return "";
144: }
145:
146: return TagUtils.getInstance().filter(value.toString());
147: }
148:
149: /**
150: * Release any acquired resources.
151: */
152: public void release() {
153: super .release();
154: accept = null;
155: name = Constants.BEAN_KEY;
156: redisplay = true;
157: }
158: }
|