1 16 17 package org.apache.taglibs.standard.tag.common.core; 18 19 import java.beans.IntrospectionException ; 20 import java.beans.Introspector ; 21 import java.beans.PropertyDescriptor ; 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.reflect.Method ; 24 import java.util.Map ; 25 26 import javax.servlet.jsp.JspException ; 27 import javax.servlet.jsp.JspTagException ; 28 import javax.servlet.jsp.PageContext ; 29 import javax.servlet.jsp.el.ELException ; 30 import javax.servlet.jsp.el.ExpressionEvaluator ; 31 import javax.servlet.jsp.el.VariableResolver ; 32 import javax.servlet.jsp.tagext.BodyTagSupport ; 33 34 import org.apache.taglibs.standard.resources.Resources; 35 36 41 public class SetSupport extends BodyTagSupport { 42 43 46 protected Object value; protected boolean valueSpecified; protected Object target; protected String property; private String var; private int scope; private boolean scopeSpecified; 54 57 62 public SetSupport() { 63 super(); 64 init(); 65 } 66 67 private void init() { 69 value = var = null; 70 scopeSpecified = valueSpecified = false; 71 scope = PageContext.PAGE_SCOPE; 72 } 73 74 public void release() { 76 super.release(); 77 init(); 78 } 79 80 81 84 public int doEndTag() throws JspException { 85 86 Object result; 88 if (value != null) { 90 result = value; 92 } else if (valueSpecified) { 93 result = null; 95 } else { 96 if (bodyContent == null || bodyContent.getString() == null) 98 result = ""; 99 else 100 result = bodyContent.getString().trim(); 101 } 102 103 if (var != null) { 105 106 112 if (result != null) { 113 pageContext.setAttribute(var, result, scope); 114 } else { 115 if (scopeSpecified) 116 pageContext.removeAttribute(var, scope); 117 else 118 pageContext.removeAttribute(var); 119 } 120 121 } else if (target != null) { 122 123 if (target instanceof Map ) { 125 if (result == null) 127 ((Map ) target).remove(property); 128 else 129 ((Map ) target).put(property, result); 130 } else { 131 try { 133 PropertyDescriptor pd[] = 134 Introspector.getBeanInfo(target.getClass()) 135 .getPropertyDescriptors(); 136 boolean succeeded = false; 137 for (int i = 0; i < pd.length; i++) { 138 if (pd[i].getName().equals(property)) { 139 Method m = pd[i].getWriteMethod(); 140 if (m == null) { 141 throw new JspException ( 142 Resources.getMessage("SET_NO_SETTER_METHOD", 143 property)); 144 } 145 if (result != null) { 146 try { 147 m.invoke(target, 148 new Object [] { 149 convertToExpectedType(result, m.getParameterTypes()[0])}); 150 } catch (javax.servlet.jsp.el.ELException ex) { 151 throw new JspTagException (ex); 152 } 153 } else { 154 m.invoke(target, new Object [] { null }); 155 } 156 succeeded = true; 157 } 158 } 159 if (!succeeded) { 160 throw new JspTagException ( 161 Resources.getMessage("SET_INVALID_PROPERTY", 162 property)); 163 } 164 } catch (IllegalAccessException ex) { 165 throw new JspException (ex); 166 } catch (IntrospectionException ex) { 167 throw new JspException (ex); 168 } catch (InvocationTargetException ex) { 169 throw new JspException (ex); 170 } 171 } 172 } else { 173 throw new JspTagException (); 175 } 176 177 return EVAL_PAGE; 178 } 179 180 184 private Object convertToExpectedType( final Object value, 185 Class expectedType ) 186 throws javax.servlet.jsp.el.ELException { 187 ExpressionEvaluator evaluator = pageContext.getExpressionEvaluator(); 188 return evaluator.evaluate( "${result}", expectedType, 189 new VariableResolver () { 190 public Object resolveVariable( String pName ) 191 throws ELException { 192 return value; 193 } 194 }, null ); 195 } 196 197 200 public void setVar(String var) { 202 this.var = var; 203 } 204 205 public void setScope(String scope) { 207 this.scope = Util.getScope(scope); 208 this.scopeSpecified = true; 209 } 210 } 211 | Popular Tags |