KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > el > VarBlock


1 package fr.improve.struts.taglib.layout.el;
2
3 import java.lang.reflect.InvocationTargetException JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import javax.servlet.jsp.JspException JavaDoc;
7 import javax.servlet.jsp.PageContext JavaDoc;
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 /**
15  * @author jer80876
16  */

17 public class VarBlock implements Block {
18     
19     private String JavaDoc name;
20     private String JavaDoc property;
21     
22     public VarBlock(String JavaDoc in_name, String JavaDoc in_property) {
23         name = in_name;
24         property = in_property;
25     }
26
27     /**
28      * @see com.beaufouripsen.seas.presentation.taglib.el.Block#evaluate(PageContext)
29      */

30     public String JavaDoc evaluate(PageContext JavaDoc in_pg) throws EvaluationException {
31         Object JavaDoc lc_bean = null;
32         String JavaDoc lc_name = Expression.evaluate(name, in_pg);
33         String JavaDoc 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         // Check if there is an array, not efficient.
42
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 JavaDoc lc_beanName = lc_name.substring(0, lc_indexStart);
47                 Iterator JavaDoc lc_it;
48                 try {
49                     lc_it = LayoutUtils.getIterator(in_pg, lc_beanName, null);
50                 } catch (JspException JavaDoc 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 JavaDoc lc_value = null;
66             try {
67                 lc_value = PropertyUtils.getProperty(lc_bean, lc_property);
68             } catch (NoSuchMethodException JavaDoc e) {
69                 throw new EvaluationException("No method to get the property " + lc_property + " on a " + lc_bean.getClass().getName());
70             } catch (InvocationTargetException JavaDoc e) {
71                 throw new EvaluationException("Getter of property " + lc_property + " failed");
72             } catch (IllegalAccessException JavaDoc 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 JavaDoc toString() {
85         return new StringBuffer JavaDoc("[V(").append(name).append(".").append(property).append(")]").toString();
86     }
87
88 }
89
Popular Tags