KickJava   Java API By Example, From Geeks To Geeks.

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


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.BodyTagSupport JavaDoc;
25
26 /**
27  *
28  * This class implements the <input:textarea> tag, which presents a
29  * <textarea> 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 TextArea extends BodyTagSupport JavaDoc {
38
39     private String JavaDoc name; // name of the textarea
40

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

43     private Map JavaDoc attributes; // attributes of the <textarea> 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 cols, rows;
50
51     public void release() {
52         super.release();
53         name = null;
54         dVal = null;
55         attributes = null;
56         attributesText = null;
57         beanId = null;
58         cols = null;
59         rows = null;
60     }
61
62     public int doEndTag() throws JspException JavaDoc {
63         try {
64             // sanity check
65
if (name == null || name.equals(""))
66                 throw new JspTagException JavaDoc("invalid null or empty 'name'");
67
68             // Store beanId in a local variable because we change it
69
String JavaDoc beanId = this.beanId;
70
71             // Get default beanId
72
if (beanId == null) {
73                 beanId = Util.defaultFormBeanId(this);
74             } else if (beanId.length() == 0) {
75                 // An empty beanId means, do not use any bean - not even default
76
beanId = null;
77             }
78
79             // get what we need from the page
80
ServletRequest JavaDoc req = pageContext.getRequest();
81             JspWriter JavaDoc out = pageContext.getOut();
82
83             // start building up the tag
84
out.print("<textarea 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 (cols != null) {
93                 out.print("cols=\"" + Util.quote(cols) + "\" ");
94             }
95             if (rows != null) {
96                 out.print("rows=\"" + Util.quote(rows) + "\" ");
97             }
98
99             // end the starting tag
100
out.print(">");
101
102             String JavaDoc defaultValue = dVal;
103             if (getBodyContent() != null
104                     && getBodyContent().getString() != null) {
105                 defaultValue = getBodyContent().getString();
106             }
107
108             /*
109              * print out the value from the bean if it's there, or from the
110              * request if it's there, or use the default value if it's not
111              */

112             String JavaDoc beanValue = (beanId != null ? Util.beanPropertyValue(
113                     pageContext.findAttribute(beanId), name) : null);
114             if (beanValue != null) {
115                 out.print(Util.quote(beanValue));
116             } else if (req.getParameter(name) != null) {
117                 out.print(Util.quote(req.getParameter(name)));
118             } else {
119                 if (defaultValue != null)
120                     out.print(Util.quote(defaultValue));
121             }
122
123             // end the textarea
124
out.print("</textarea>");
125
126         } catch (Exception JavaDoc ex) {
127             throw new JspTagException JavaDoc(ex.getMessage());
128         }
129         return EVAL_PAGE;
130     }
131
132     public void setName(String JavaDoc x) {
133         name = x;
134     }
135
136     public void setAttributes(Map JavaDoc x) {
137         attributes = x;
138     }
139
140     public void setAttributesText(String JavaDoc x) {
141         attributesText = x;
142     }
143
144     public void setBean(String JavaDoc x) {
145         beanId = x;
146     }
147
148     public void setDefault(String JavaDoc x) {
149         dVal = x;
150     }
151
152     /**
153      * Getter for property name.
154      *
155      * @return Value of property name.
156      */

157     public String JavaDoc getName() {
158         return name;
159     }
160
161     /**
162      * Getter for property default.
163      *
164      * @return Value of property default.
165      */

166     public String JavaDoc getDefault() {
167         return dVal;
168     }
169
170     /**
171      * Getter for property bean.
172      *
173      * @return Value of property bean.
174      */

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

184     public String JavaDoc getAttributesText() {
185         return attributesText;
186     }
187
188     /**
189      * Getter for property attributes.
190      *
191      * @return Value of property attributes.
192      */

193     public Map JavaDoc getAttributes() {
194         return attributes;
195     }
196
197     public void setCols(String JavaDoc cols) {
198         this.cols = cols;
199     }
200
201     public void setRows(String JavaDoc rows) {
202         this.rows = rows;
203     }
204 }
Popular Tags