KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > tags > form > FormTag


1 package jodd.servlet.tags.form;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpSession;
8 import javax.servlet.jsp.JspWriter;
9 import javax.servlet.jsp.tagext.BodyContent;
10 import javax.servlet.jsp.tagext.BodyTagSupport;
11
12 import jodd.bean.BeanUtil;
13 import jodd.servlet.HtmlEncoder;
14 import jodd.servlet.HtmlUtil;
15 import jodd.util.StringUtil;
16
17
18
19 /**
20  * Form tag populates a included form with values from the one or more beans.
21  * Beans are stored in various scopes. This tag is <i>smart</i> so everything
22  * happens automatically. However, there are several easy-to-work-with limits
23  * that must be fulfilled to make this tag working properly:
24  * <ul>
25  * <li>All values attributes must not exist.</li>
26  * <li>Textarea content must be empty.</li>
27  * <li>All<sup>*</sup> attributes and its names must be directly connected with the
28  * <code>="</code> or <code>='</code>.</li>
29  * <li>All<sup>*</sup> attributes values must be surrounded by quotes (" or ').</li>
30  * <li>of course, there should be no duplicated names, both across the form and beans.</li>
31  * </ul>
32  *
33  * <sup>*</sup>note: not all, just: name, value and type attributes, however, it is
34  * recommended to use the above style everywhere.
35  * <p>
36  *
37  * Availiable scopes are:
38  * <ul>
39  * <li>page,</li>
40  * <li>session, and</li>
41  * <li>request</li>
42  * </ul>
43  */

44 public class FormTag extends BodyTagSupport {
45
46     // ---------------------------------------------------------------- tag parameters
47

48     private String beanNames = null;
49     /**
50      * Sets bean names with value of the "bean" attribute.
51      *
52      * @param v bean names
53      */

54     public void setBeans(String v) {
55         beanNames = v;
56     }
57     /**
58      * Gets bean names.
59      *
60      * @return bean names
61      */

62     public String getBeans() {
63         return beanNames;
64     }
65
66     private String scopes = null;
67     /**
68      * Sets the value of "scope" attribute, that represent beans scope.
69      *
70      * @param v
71      */

72     public void setScopes(String v) {
73         scopes = v;
74     }
75     /**
76      * Return value of the "scope" attribute
77      *
78      * @return bean scopes
79      */

80     public String getScopes() {
81         return scopes;
82     }
83
84
85     // ---------------------------------------------------------------- tag methods
86

87     private HashMap beansValues = null;
88
89     /**
90      * Copies properties of all specified bean into one map.
91      *
92      * @return EVAL_BODY_AGAIN
93      */

94     public int doStartTag() {
95         beansValues = new HashMap();
96         String[] b = StringUtil.splitc(beanNames, ", ");
97         String[] s = StringUtil.splitc(scopes.toLowerCase(), ", ");
98
99         HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
100         HttpSession session = (HttpSession) pageContext.getSession();
101
102         for (int i = 0; i < b.length; i++) {
103             Object bean = null;
104             if ((s[i].length() == 0) || (s[i].equals("page"))) {
105                 bean = pageContext.getAttribute(b[i]);
106             } else if (s[i].equals("request")) {
107                 bean = request.getAttribute(b[i]);
108             } else if (s[i].equals("session")) {
109                 bean = session.getAttribute(b[i]);
110             }
111             if (bean != null) {
112                 beansValues.putAll((Map) BeanUtil.getAllProperties(bean));
113             }
114         }
115         return EVAL_BODY_AGAIN;
116     }
117
118     /**
119      * Performs smart form population.
120      *
121      * @return SKIP_BODY
122      */

123     public int doAfterBody() {
124         BodyContent body = getBodyContent();
125         try {
126             JspWriter out = body.getEnclosingWriter();
127             String bodytext = body.getString();
128             if ((beansValues != null) && (beansValues.size() > 0)) {
129                 bodytext = populateForm(bodytext, beansValues);
130             }
131             out.print(bodytext);
132         } catch (Exception ex) {
133             ex.printStackTrace();
134         }
135         return SKIP_BODY;
136     }
137
138
139     /**
140      * End of tag.
141      *
142      * @return EVAL_PAGE
143      */

144     public int doEndTag() {
145         return EVAL_PAGE;
146     }
147
148
149     // ---------------------------------------------------------------- populate
150

151     private String populateForm(String html, HashMap values) {
152
153         int i = 0, s = 0;
154         StringBuffer result = new StringBuffer(html.length());
155         String currentSelectName = null;
156         while (true) {
157             // find starting tag
158
i = html.indexOf('<', s);
159             if (i == -1) {
160                 result.append(html.substring(s));
161                 break; // input tag not found
162
}
163             result.append(html.substring(s, i)); // tag found, all before tag is stored
164
s = i;
165
166             // find closing tag
167
i = html.indexOf('>', i);
168             if (i == -1) {
169                 result.append(html.substring(s));
170                 break; // closing tag not found
171
}
172             i++;
173
174             // match tags
175
String tag = html.substring(s, i);
176             String tagName = HtmlUtil.getTagName(tag);
177             if (tagName.equalsIgnoreCase("input") == true) {
178                 String tagType = HtmlUtil.getAttribute(tag, "type");
179                 if (tagType != null) {
180                     String name = HtmlUtil.getAttribute(tag, "name");
181                     if (values.containsKey(name)) {
182                         String value = StringUtil.toString(values.get(name));
183                         tagType = tagType.toLowerCase();
184                         if (tagType.equals("text")) {
185                             tag = HtmlUtil.addAttribute(tag, "value", value);
186                         } if (tagType.equals("hidden")) {
187                             tag = HtmlUtil.addAttribute(tag, "value", value);
188                         } if (tagType.equals("image")) {
189                             tag = HtmlUtil.addAttribute(tag, "value", value);
190                         } if (tagType.equals("password")) {
191                             tag = HtmlUtil.addAttribute(tag, "value", value);
192                         } if (tagType.equals("checkbox")) {
193                             String tagValue = HtmlUtil.getAttribute(tag, "value");
194                             if (tagValue == null) {
195                                 tagValue = "true";
196                             }
197                             if (tagValue.equals(value)) {
198                                 tag = HtmlUtil.addAttribute(tag, "checked");
199                             }
200                         } if (tagType.equals("radio")) {
201                             String tagValue = HtmlUtil.getAttribute(tag, "value");
202                             if (tagValue != null) {
203                                 if (tagValue.equals(value)) {
204                                     tag = HtmlUtil.addAttribute(tag, "checked");
205                                 }
206                             }
207                         }
208                     }
209                 }
210             } else if (tagName.equalsIgnoreCase("textarea") == true) {
211                 String name = HtmlUtil.getAttribute(tag, "name");
212                 if (values.containsKey(name)) {
213                     Object value = values.get(name);
214                     if (value != null) {
215                         //tag += ServletUtil.encodeHtml(StringUtil.toString(value));
216
tag += HtmlEncoder.encode(StringUtil.toString(value));
217                     }
218                 }
219             } else if (tagName.equalsIgnoreCase("select") == true) {
220                 currentSelectName = HtmlUtil.getAttribute(tag, "name");
221             } else if (tagName.equalsIgnoreCase("/select") == true) {
222                 currentSelectName = null;
223             } else if (tagName.equalsIgnoreCase("option") == true) {
224                 if (currentSelectName != null) {
225                     String tagValue = HtmlUtil.getAttribute(tag, "value");
226                     if (tagValue != null) {
227                         if (values.containsKey(currentSelectName)) {
228                             Object vals = values.get(currentSelectName);
229                             if (vals != null) {
230                                 if (vals.getClass().isArray() == false) {
231                                     String value = StringUtil.toString(vals);
232                                     if (value.equals(tagValue)) {
233                                         tag = HtmlUtil.addAttribute(tag, "selected");
234                                     }
235                                 } else {
236                                     String vs[] = StringUtil.toStringArray(vals);
237                                     for (int k = 0; k < vs.length; k++) {
238                                         String vsk = vs[k];
239                                         if (vsk != null) {
240                                             if (vsk.equals(tagValue)) {
241                                                 tag = HtmlUtil.addAttribute(tag, "selected");
242                                             }
243                                         }
244                                     }
245                                 }
246                             }
247                         }
248                     }
249                 }
250             }
251             result.append(tag);
252             s = i;
253         }
254         return result.toString();
255     }
256
257 }
258
Popular Tags