1 13 package com.tonbeller.wcf.expr; 14 15 import java.beans.PropertyDescriptor ; 16 import java.lang.reflect.InvocationTargetException ; 17 18 import javax.servlet.jsp.PageContext ; 19 20 import org.apache.commons.beanutils.PropertyUtils; 21 import org.apache.log4j.Logger; 22 23 import com.tonbeller.wcf.utils.SoftException; 24 25 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 expr) { 37 if (expr == null) 38 throw new IllegalArgumentException ("expr is null"); 39 if (isExpression(expr) && !expr.endsWith("}")) 40 throw new IllegalArgumentException ("expr must end with \"}\""); 41 } 42 43 public static Object getModelReference(PageContext pageContext, String expr) { 44 ExprContext ec = getExprContextAdapter(pageContext); 45 return getModelReference(ec, expr); 46 47 } 48 49 public static ExprContext getExprContextAdapter(final PageContext pageContext) { 50 return new ExprContext() { 51 public Object findBean(String name) { 52 return pageContext.findAttribute(name); 53 } 54 public void setBean(String name, Object 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 getModelReference(ExprContext context, String expr) { 64 try { 65 if (expr == null || expr.length() == 0) 66 return null; 67 if (!isExpression(expr)) 69 return context.findBean(expr); 70 71 if (!expr.endsWith("}")) 72 throw new IllegalArgumentException ("expr must end with '}'"); 73 74 int pos = expr.indexOf('.'); 76 if (pos < 0) { 77 String name = expr.substring(2, expr.length() - 1); 79 return context.findBean(name); 80 } 81 String name = expr.substring(2, pos); 83 Object bean = context.findBean(name); 84 if (bean == null) 85 throw new IllegalArgumentException ("bean \"" + name + "\" not found"); 86 String path = expr.substring(pos + 1, expr.length() - 1); 87 return PropertyUtils.getProperty(bean, path); 88 } catch (IllegalAccessException e) { 89 logger.error("?", e); 90 throw new SoftException(e); 91 } catch (InvocationTargetException e) { 92 logger.error("?", e); 93 throw new SoftException(e); 94 } catch (NoSuchMethodException e) { 95 logger.error("?", e); 96 throw new SoftException(e); 97 } 98 } 99 100 public static void setModelReference(ExprContext context, String expr, Object value) { 101 if (expr == null) 102 throw new NullPointerException ("expr is null"); 103 104 if (!isExpression(expr)) { 106 context.setBean(expr, value); 107 return; 108 } 109 110 if (!expr.endsWith("}")) 111 throw new IllegalArgumentException ("expr must end with '}'"); 112 113 if (expr.indexOf('.') < 0) { 115 String name = expr.substring(2, expr.length()- 1); 117 context.setBean(name, value); 118 return; 119 } 120 121 try { 123 int pos = expr.indexOf('.'); 124 String name = expr.substring(2, pos); 125 Object bean = context.findBean(name); 126 if (bean == null) 127 throw new IllegalArgumentException ("bean \"" + name + "\" not found"); 128 String path = expr.substring(pos + 1, expr.length() - 1); 129 PropertyUtils.setProperty(bean, path, value); 130 } catch (IllegalAccessException e) { 131 logger.error("exception caught", e); 132 throw new SoftException(e); 133 } catch (InvocationTargetException e) { 134 logger.error("exception caught", e); 135 throw new SoftException(e); 136 } catch (NoSuchMethodException e) { 137 logger.error("exception caught", e); 138 throw new SoftException(e); 139 } 140 } 141 142 public static PropertyDescriptor getPropertyDescriptor(ExprContext context, String expr) { 143 if (!ExprUtils.isExpression(expr) || !expr.endsWith("}") || expr.indexOf('.') < 0) 144 throw new IllegalArgumentException ("'#{bean.property}' expected"); 145 146 int pos = expr.indexOf('.'); 147 String name = expr.substring(2, pos); 148 Object bean = context.findBean(name); 149 if (bean == null) 150 throw new IllegalArgumentException ("bean \"" + name + "\" not found"); 151 String path = expr.substring(pos + 1, expr.length() - 1); 152 try { 153 return PropertyUtils.getPropertyDescriptor(bean, path); 154 } catch (Exception e) { 155 logger.error(null, e); 156 return null; 157 } 158 } 159 160 164 public static String getBeanName(String 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 176 public static boolean isExpression(String expr) { 177 if (expr == null) 178 return false; 179 return expr.startsWith("${") || expr.startsWith("#{"); 180 } 181 182 } 183 | Popular Tags |