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 44 public class FormTag extends BodyTagSupport { 45 46 48 private String beanNames = null; 49 54 public void setBeans(String v) { 55 beanNames = v; 56 } 57 62 public String getBeans() { 63 return beanNames; 64 } 65 66 private String scopes = null; 67 72 public void setScopes(String v) { 73 scopes = v; 74 } 75 80 public String getScopes() { 81 return scopes; 82 } 83 84 85 87 private HashMap beansValues = null; 88 89 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 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 144 public int doEndTag() { 145 return EVAL_PAGE; 146 } 147 148 149 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 i = html.indexOf('<', s); 159 if (i == -1) { 160 result.append(html.substring(s)); 161 break; } 163 result.append(html.substring(s, i)); s = i; 165 166 i = html.indexOf('>', i); 168 if (i == -1) { 169 result.append(html.substring(s)); 170 break; } 172 i++; 173 174 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 += 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 |