1 29 30 package com.caucho.jstl.el; 31 32 import com.caucho.el.Expr; 33 import com.caucho.util.L10N; 34 35 import javax.el.ELContext; 36 import javax.servlet.jsp.JspException ; 37 import javax.servlet.jsp.PageContext ; 38 import javax.servlet.jsp.tagext.BodyContent ; 39 import javax.servlet.jsp.tagext.BodyTagSupport ; 40 41 public class CoreSetTag extends BodyTagSupport { 42 private static L10N L = new L10N(CoreSetTag.class); 43 private Expr valueExpr; 44 private String var; 45 private String scope; 46 47 private Expr targetExpr; 48 private Expr propertyExpr; 49 50 53 public void setValue(Expr value) 54 { 55 this.valueExpr = value; 56 } 57 58 61 public void setVar(String var) 62 { 63 this.var = var; 64 } 65 66 69 public void setScope(String scope) 70 { 71 this.scope = scope; 72 } 73 74 77 public void setTarget(Expr target) 78 { 79 this.targetExpr = target; 80 } 81 82 85 public void setProperty(Expr property) 86 { 87 this.propertyExpr = property; 88 } 89 90 93 public int doStartTag() 94 throws JspException 95 { 96 try { 97 if (valueExpr == null) 98 return EVAL_BODY_BUFFERED; 99 100 ELContext env = pageContext.getELContext(); 101 102 Object value = valueExpr.evalObject(env); 103 if (var != null) 104 setValue(value); 105 else 106 setProperty(value); 107 108 return SKIP_BODY; 109 } catch (Exception e) { 110 throw new JspException (e); 111 } 112 } 113 114 public int doEndTag() throws JspException 115 { 116 BodyContent body = (BodyContent ) getBodyContent(); 117 118 if (body != null) { 119 if (var != null) 120 setValue(body.getString().trim()); 121 else 122 setProperty(body.getString().trim()); 123 } 124 125 return EVAL_PAGE; 126 } 127 128 private void setValue(Object value) 129 throws JspException 130 { 131 if (scope == null) { 132 if (value != null) 133 pageContext.setAttribute(var, value); 134 else 135 pageContext.removeAttribute(var); 136 } 137 else if (scope.equals("page")) { 138 if (value != null) 139 pageContext.setAttribute(var, value); 140 else 141 pageContext.removeAttribute(var); 142 } 143 else if (scope.equals("request")) { 144 if (value != null) 145 pageContext.getRequest().setAttribute(var, value); 146 else 147 pageContext.getRequest().removeAttribute(var); 148 } 149 else if (scope.equals("session")) { 150 if (value != null) 151 pageContext.getSession().setAttribute(var, value); 152 else 153 pageContext.getSession().removeAttribute(var); 154 } 155 else if (scope.equals("application")) { 156 if (value != null) 157 pageContext.getServletContext().setAttribute(var, value); 158 else 159 pageContext.getServletContext().removeAttribute(var); 160 } 161 else 162 throw new JspException (L.l("illegal scope value {0}", scope)); 163 } 164 165 private void setProperty(Object value) 166 throws JspException 167 { 168 try { 169 ELContext env = pageContext.getELContext(); 170 171 Object target = targetExpr.evalObject(env); 172 String property = propertyExpr.evalString(env); 173 174 Expr.setProperty(target, property, value); 175 } catch (Exception e) { 176 throw new JspException (e); 177 } 178 } 179 180 public static void setValue(PageContext pageContext, 181 String var, String scope, Object value) 182 throws JspException 183 { 184 if (var == null) { 185 } 186 else if (scope == null || scope.equals("page")) { 187 if (value != null) 188 pageContext.setAttribute(var, value); 189 else 190 pageContext.removeAttribute(var); 191 } 192 else if (scope.equals("request")) { 193 if (value != null) 194 pageContext.getRequest().setAttribute(var, value); 195 else 196 pageContext.getRequest().removeAttribute(var); 197 } 198 else if (scope.equals("session")) { 199 if (value != null) 200 pageContext.getSession().setAttribute(var, value); 201 else 202 pageContext.getSession().removeAttribute(var); 203 } 204 else if (scope.equals("application")) { 205 if (value != null) 206 pageContext.getServletContext().setAttribute(var, value); 207 else 208 pageContext.getServletContext().removeAttribute(var); 209 } 210 else 211 throw new JspException (L.l("illegal scope value {0}", scope)); 212 } 213 } 214 | Popular Tags |