KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > input > Text


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

16 package org.apache.taglibs.input;
17
18 import java.util.Map JavaDoc;
19
20 import javax.servlet.ServletRequest JavaDoc;
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.JspTagException JavaDoc;
23 import javax.servlet.jsp.JspWriter JavaDoc;
24 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
25
26 /**
27  *
28  * This class implements the <input:text> tag, which presents an <input
29  * type="text" ... /> form element.
30  *
31  * @version 0.90
32  * @author Shawn Bayern
33  * @author Lance Lavandowska
34  * @author Karl von Randow
35  */

36
37 public class Text extends TagSupport JavaDoc {
38
39     private String JavaDoc name; // name of the text field
40

41     private String JavaDoc dVal; // default value if none is found
42

43     private Map JavaDoc attributes; // attributes of the <input> element
44

45     private String JavaDoc attributesText; // attributes of the <input> element as text
46

47     private String JavaDoc beanId; // bean id to get default values from
48

49     private String JavaDoc size;
50
51     public void release() {
52         super.release();
53         name = null;
54         dVal = null;
55         attributes = null;
56         attributesText = null;
57         beanId = null;
58         size = null;
59     }
60
61     public int doStartTag() throws JspException JavaDoc {
62         try {
63             // sanity check
64
if (name == null || name.equals(""))
65                 throw new JspTagException JavaDoc("invalid null or empty 'name'");
66
67             // Store beanId in a local variable as we change it
68
String JavaDoc beanId = this.beanId;
69
70             // Get default beanId
71
if (beanId == null) {
72                 beanId = Util.defaultFormBeanId(this);
73             } else if (beanId.length() == 0) {
74                 // An empty beanId means, do not use any bean - not even default
75
beanId = null;
76             }
77
78             // get what we need from the page
79
ServletRequest JavaDoc req = pageContext.getRequest();
80             JspWriter JavaDoc out = pageContext.getOut();
81
82             // start building up the tag
83
out.print("<input type=\"text\" ");
84             out.print("name=\"" + Util.quote(name) + "\" ");
85
86             // include any attributes we've got here
87
Util.printAttributes(out, attributes);
88             if (attributesText != null) {
89                 out.print(attributesText + " ");
90             }
91
92             if (size != null) {
93                 out.print("size=\"" + Util.quote(size) + "\" ");
94             }
95
96             /*
97              * print out the value from the bean if it's there, or from the
98              * request if it's there, or use the default value if it's not
99              */

100             String JavaDoc beanValue = (beanId != null ? Util.beanPropertyValue(
101                     pageContext.findAttribute(beanId), name) : null);
102             if (beanValue != null) {
103                 out.print("value=\"" + Util.quote(beanValue) + "\" ");
104             } else if (req.getParameter(name) != null) {
105                 out.print("value=\"" + Util.quote(req.getParameter(name))
106                         + "\" ");
107             } else {
108                 if (dVal != null)
109                     out.print("value=\"" + Util.quote(dVal) + "\" ");
110                 else
111                     out.print("value=\"\" ");
112             }
113             // end the tag
114
out.print("/>");
115
116         } catch (Exception JavaDoc ex) {
117             throw new JspTagException JavaDoc(ex.getMessage());
118         }
119         return SKIP_BODY;
120     }
121
122     public void setName(String JavaDoc x) {
123         name = x;
124     }
125
126     public void setAttributes(Map JavaDoc x) {
127         attributes = x;
128     }
129
130     public void setAttributesText(String JavaDoc x) {
131         attributesText = x;
132     }
133
134     public void setBean(String JavaDoc x) {
135         beanId = x;
136     }
137
138     public void setDefault(String JavaDoc x) {
139         dVal = x;
140     }
141
142     /**
143      * Getter for property name.
144      *
145      * @return Value of property name.
146      */

147     public String JavaDoc getName() {
148         return name;
149     }
150
151     /**
152      * Getter for property default.
153      *
154      * @return Value of property default.
155      */

156     public String JavaDoc getDefault() {
157         return dVal;
158     }
159
160     /**
161      * Getter for property bean.
162      *
163      * @return Value of property bean.
164      */

165     public String JavaDoc getBean() {
166         return beanId;
167     }
168
169     /**
170      * Getter for property attributesText.
171      *
172      * @return Value of property attributesText.
173      */

174     public String JavaDoc getAttributesText() {
175         return attributesText;
176     }
177
178     /**
179      * Getter for property attributes.
180      *
181      * @return Value of property attributes.
182      */

183     public Map JavaDoc getAttributes() {
184         return attributes;
185     }
186
187     public void setSize(String JavaDoc size) {
188         this.size = size;
189     }
190 }
Popular Tags