KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > expr > ExprUtils


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.expr;
14
15 import java.beans.PropertyDescriptor JavaDoc;
16 import java.lang.reflect.InvocationTargetException JavaDoc;
17
18 import javax.servlet.jsp.PageContext JavaDoc;
19
20 import org.apache.commons.beanutils.PropertyUtils;
21 import org.apache.log4j.Logger;
22
23 import com.tonbeller.wcf.utils.SoftException;
24
25 /**
26  * evaluates EL expressions, while we are waiting for servlet 2 / tomcat 5
27  * @author av
28  */

29 public class ExprUtils {
30   private static Logger logger = Logger.getLogger(ExprUtils.class);
31
32   private ExprUtils() {
33     super();
34   }
35
36   public static void checkExpr(String JavaDoc expr) {
37     if (expr == null)
38       throw new IllegalArgumentException JavaDoc("expr is null");
39     if (isExpression(expr) && !expr.endsWith("}"))
40       throw new IllegalArgumentException JavaDoc("expr must end with \"}\"");
41   }
42
43   public static Object JavaDoc getModelReference(PageContext JavaDoc pageContext, String JavaDoc expr) {
44     ExprContext ec = getExprContextAdapter(pageContext);
45     return getModelReference(ec, expr);
46
47   }
48
49   public static ExprContext getExprContextAdapter(final PageContext JavaDoc pageContext) {
50     return new ExprContext() {
51       public Object JavaDoc findBean(String JavaDoc name) {
52         return pageContext.findAttribute(name);
53       }
54       public void setBean(String JavaDoc name, Object JavaDoc bean) {
55         if (bean == null)
56           pageContext.removeAttribute(name);
57         else
58           pageContext.setAttribute(name, bean, PageContext.SESSION_SCOPE);
59       }
60     };
61   }
62
63   public static Object JavaDoc getModelReference(ExprContext context, String JavaDoc expr) {
64     try {
65       if (expr == null || expr.length() == 0)
66         return null;
67       // plain string?
68
if (!isExpression(expr))
69         return context.findBean(expr);
70
71       if (!expr.endsWith("}"))
72         throw new IllegalArgumentException JavaDoc("expr must end with '}'");
73
74       // dotted expression?
75
int pos = expr.indexOf('.');
76       if (pos < 0) {
77         // no, find attribute in context
78
String JavaDoc name = expr.substring(2, expr.length() - 1);
79         return context.findBean(name);
80       }
81       // yes, evaluate property path
82
String JavaDoc name = expr.substring(2, pos);
83       Object JavaDoc bean = context.findBean(name);
84       if (bean == null)
85         throw new IllegalArgumentException JavaDoc("bean \"" + name + "\" not found");
86       String JavaDoc path = expr.substring(pos + 1, expr.length() - 1);
87       return PropertyUtils.getProperty(bean, path);
88     } catch (IllegalAccessException JavaDoc e) {
89       logger.error("?", e);
90       throw new SoftException(e);
91     } catch (InvocationTargetException JavaDoc e) {
92       logger.error("?", e);
93       throw new SoftException(e);
94     } catch (NoSuchMethodException JavaDoc e) {
95       logger.error("?", e);
96       throw new SoftException(e);
97     }
98   }
99
100   public static void setModelReference(ExprContext context, String JavaDoc expr, Object JavaDoc value) {
101     if (expr == null)
102       throw new NullPointerException JavaDoc("expr is null");
103
104     // plain "bean" expr?
105
if (!isExpression(expr)) {
106       context.setBean(expr, value);
107       return;
108     }
109
110     if (!expr.endsWith("}"))
111       throw new IllegalArgumentException JavaDoc("expr must end with '}'");
112
113     // "#{bean}" expr?
114
if (expr.indexOf('.') < 0) {
115       // no bean, set session attribute
116
String JavaDoc name = expr.substring(2, expr.length()- 1);
117       context.setBean(name, value);
118       return;
119     }
120
121     // "#{bean.property.path}" expr
122
try {
123       int pos = expr.indexOf('.');
124       String JavaDoc name = expr.substring(2, pos);
125       Object JavaDoc bean = context.findBean(name);
126       if (bean == null)
127         throw new IllegalArgumentException JavaDoc("bean \"" + name + "\" not found");
128       String JavaDoc path = expr.substring(pos + 1, expr.length() - 1);
129       PropertyUtils.setProperty(bean, path, value);
130     } catch (IllegalAccessException JavaDoc e) {
131       logger.error("exception caught", e);
132       throw new SoftException(e);
133     } catch (InvocationTargetException JavaDoc e) {
134       logger.error("exception caught", e);
135       throw new SoftException(e);
136     } catch (NoSuchMethodException JavaDoc e) {
137       logger.error("exception caught", e);
138       throw new SoftException(e);
139     }
140   }
141
142   public static PropertyDescriptor JavaDoc getPropertyDescriptor(ExprContext context, String JavaDoc expr) {
143     if (!ExprUtils.isExpression(expr) || !expr.endsWith("}") || expr.indexOf('.') < 0)
144       throw new IllegalArgumentException JavaDoc("'#{bean.property}' expected");
145
146       int pos = expr.indexOf('.');
147       String JavaDoc name = expr.substring(2, pos);
148       Object JavaDoc bean = context.findBean(name);
149       if (bean == null)
150         throw new IllegalArgumentException JavaDoc("bean \"" + name + "\" not found");
151       String JavaDoc path = expr.substring(pos + 1, expr.length() - 1);
152       try {
153         return PropertyUtils.getPropertyDescriptor(bean, path);
154       } catch (Exception JavaDoc e) {
155         logger.error(null, e);
156         return null;
157       }
158   }
159
160   /**
161    * extract the name of the bean. For example, in the expression
162    * "${bean.property}" the bean name is "bean".
163    */

164   public static String JavaDoc getBeanName(String JavaDoc expr) {
165     if (isExpression(expr))
166       expr = expr.substring(2, expr.length() - 1);
167     int pos = expr.indexOf('.');
168     if (pos > 0)
169       expr = expr.substring(0, pos);
170     return expr;
171   }
172
173   /**
174    * true, if expr starts with "${" or with "#{"
175    */

176   public static boolean isExpression(String JavaDoc expr) {
177     if (expr == null)
178       return false;
179     return expr.startsWith("${") || expr.startsWith("#{");
180   }
181
182 }
183
Popular Tags