package net.libo.web.taglib; import java.util.ArrayList; import java.util.HashMap; import java.util.Collection; import java.util.Iterator; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.BodyContent; import javax.servlet.jsp.tagext.BodyTagSupport; import org.apache.commons.beanutils.PropertyUtils; import net.libo.web.util.HtmlEncoder;import net.libo.web.util.HtmlUtils; /** * @author Boyce * */public class FieldBindTag extends BodyTagSupport { private Object[] beans = null; private String beanNames = null; /** * Sets bean names with value of the "bean" attribute. * * @param v bean names */ public void setBeans(String v) { beanNames = v; } /** * Gets bean names. * * @return bean names */ public String getBeans() { return beanNames; } public int doStartTag() { String[] b = beanNames.split(","); ArrayList beanList = new ArrayList(); for (int i=0;i String name = b[i].trim(); if (name.length() > 0) { Object bean = pageContext.findAttribute(name); if (bean != null)beanList.add(bean); } } beans = beanList.toArray(); return EVAL_BODY_AGAIN; } /** * Performs smart form population. * * @return SKIP_BODY */ public int doAfterBody() { BodyContent body = getBodyContent(); try { JspWriter out = body.getEnclosingWriter(); String bodytext = body.getString(); bodytext = populateForm(bodytext); out.print(bodytext); } catch (Exception ex) { ex.printStackTrace(); } return SKIP_BODY; } /** * End of tag. * * @return EVAL_PAGE * @throws JspException */ public int doEndTag() throws JspException { beans = null; return EVAL_PAGE; } // ---------------------------------------------------------------- populate private String populateForm(String html) throws Exception { int i = 0, s = 0; StringBuffer result = new StringBuffer(html.length()); String currentSelectName = null; while (true) { // find starting tag i = html.indexOf('<', s); if (i == -1) { result.append(html.substring(s)); break; // input tag not found } result.append(html.substring(s, i)); // tag found, all before tag is stored s = i; // find closing tag i = html.indexOf('>', i); if (i == -1) { result.append(html.substring(s)); break; // closing tag not found } i++; // match tags String tag = html.substring(s, i); //System.out.println(tag); String tagName = HtmlUtils.getTagName(tag); if (tagName.equalsIgnoreCase("input") == true) { String tagType = HtmlUtils.getAttribute(tag, "type"); if (tagType != null) { String name = HtmlUtils.getAttribute(tag, "name"); HashMap params = new HashMap(); params.put("name", name); Object vobj = findValue(name); if (vobj != null) { String value = vobj.toString(); tagType = tagType.toLowerCase(); if (tagType.equals("text")) { tag = HtmlUtils.addAttribute(tag, "value", value); } if (tagType.equals("hidden")) { tag = HtmlUtils.addAttribute(tag, "value", value); } if (tagType.equals("image")) { tag = HtmlUtils.addAttribute(tag, "value", value); } if (tagType.equals("password")) { tag = HtmlUtils.addAttribute(tag, "value", value); } if (tagType.equals("checkbox")) {
String tagValue = HtmlUtils.getAttribute(tag, "value"); if (tagValue == null) { tagValue = "true"; } if (tagValue.equals(value)) { tag = HtmlUtils.addAttribute(tag, "checked"); } } if (tagType.equals("radio")) {
String tagValue = HtmlUtils.getAttribute(tag, "value"); if (tagValue != null) { if (tagValue.equals(value)) { tag = HtmlUtils.addAttribute(tag, "checked"); } } } } } } else if (tagName.equalsIgnoreCase("textarea") == true) { String name = HtmlUtils.getAttribute(tag, "name"); Object value = findValue(name); if (value != null) { if (value != null) {
tag += HtmlEncoder.encode(value.toString()); } } } else if (tagName.equalsIgnoreCase("select") == true) { currentSelectName = HtmlUtils.getAttribute(tag, "name"); } else if (tagName.equalsIgnoreCase("/select") == true) { currentSelectName = null; } else if (tagName.equalsIgnoreCase("option") == true) { if (currentSelectName != null) { String tagValue = HtmlUtils.getAttribute(tag, "value"); if (tagValue != null) { Object vals = findValue(currentSelectName); if (vals != null) {
if (vals.getClass().isArray()) { Object[] arr = (Object[])vals;
if (arr.length > 0) {
String[] vs = new String[arr.length]; for (int j=0;j vs[j] = arr[j].toString(); } for (int k = 0; k < vs.length; k++) { String vsk = vs[k]; if (vsk != null) { if (vsk.equals(tagValue)) { tag = HtmlUtils.addAttribute(tag, "selected"); } } } } } else if (vals instanceof Collection) { Collection arr = (Collection)vals; int arrsize = arr.size(); if (arrsize > 0) { String[] vs = new String[arrsize]; int j=0; for (Iterator it=arr.iterator();it.hasNext();) { vs[j++] = it.next().toString(); } for (int k = 0; k < vs.length; k++) { String vsk = vs[k]; if (vsk != null) { if (vsk.equals(tagValue)) { tag = HtmlUtils.addAttribute(tag, "selected");
}
} } } } else { String value = vals.toString(); if (value.equals(tagValue)) { tag = HtmlUtils.addAttribute(tag, "selected"); } } } } } } result.append(tag); s = i; } return result.toString(); } protected Object findValue(String name) { Object value = null; for (int i=0;i if (beans[i] != null) { try { value = PropertyUtils.getProperty(beans[i], name); } catch (Exception e) { } if (value != null) break; } } return value; }}