KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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

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

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