1 package fr.improve.struts.taglib.layout.el; 2 3 import java.lang.reflect.InvocationTargetException ; 4 import java.util.Iterator ; 5 6 import javax.servlet.jsp.JspException ; 7 import javax.servlet.jsp.PageContext ; 8 9 import org.apache.commons.beanutils.PropertyUtils; 10 11 import fr.improve.struts.taglib.layout.util.LayoutUtils; 12 import fr.improve.struts.taglib.layout.util.ParentFinder; 13 14 17 public class VarBlock implements Block { 18 19 private String name; 20 private String property; 21 22 public VarBlock(String in_name, String in_property) { 23 name = in_name; 24 property = in_property; 25 } 26 27 30 public String evaluate(PageContext in_pg) throws EvaluationException { 31 Object lc_bean = null; 32 String lc_name = Expression.evaluate(name, in_pg); 33 String lc_property = Expression.evaluate(property, in_pg); 34 35 if ("this".equals(lc_name)) { 36 lc_bean = ParentFinder.getLastTag(in_pg); 37 } else { 38 lc_bean = in_pg.findAttribute(lc_name); 39 } 40 41 if (lc_bean==null) { 43 int lc_indexStart = lc_name.indexOf('['); 44 int lc_indexEnd = lc_name.indexOf(']'); 45 if (lc_indexStart!=-1 && lc_indexEnd!=-1 && lc_indexStart < lc_indexEnd) { 46 String lc_beanName = lc_name.substring(0, lc_indexStart); 47 Iterator lc_it; 48 try { 49 lc_it = LayoutUtils.getIterator(in_pg, lc_beanName, null); 50 } catch (JspException e) { 51 throw new EvaluationException("Could not get iterator for bean " + lc_beanName); 52 } 53 int lc_pos = Integer.parseInt(lc_name.substring(lc_indexStart+1, lc_indexEnd)); 54 while (lc_it.hasNext() && lc_pos>=0) { 55 lc_bean = lc_it.next(); 56 lc_pos--; 57 } 58 } 59 } 60 61 if (lc_bean==null) { 62 return ""; 63 } 64 if (lc_property!=null) { 65 Object lc_value = null; 66 try { 67 lc_value = PropertyUtils.getProperty(lc_bean, lc_property); 68 } catch (NoSuchMethodException e) { 69 throw new EvaluationException("No method to get the property " + lc_property + " on a " + lc_bean.getClass().getName()); 70 } catch (InvocationTargetException e) { 71 throw new EvaluationException("Getter of property " + lc_property + " failed"); 72 } catch (IllegalAccessException e) { 73 throw new EvaluationException("Illegal access to property " + lc_property); 74 } 75 if (lc_value==null) { 76 return ""; 77 } else { 78 return lc_value.toString(); 79 } 80 } 81 return lc_bean.toString(); 82 } 83 84 public String toString() { 85 return new StringBuffer ("[V(").append(name).append(".").append(property).append(")]").toString(); 86 } 87 88 } 89 | Popular Tags |