1 28 29 package com.caucho.jstl.rt; 30 31 import com.caucho.el.Expr; 32 import com.caucho.util.L10N; 33 34 import javax.servlet.jsp.JspException ; 35 import javax.servlet.jsp.PageContext ; 36 import javax.servlet.jsp.tagext.BodyContent ; 37 import javax.servlet.jsp.tagext.BodyTagSupport ; 38 39 public class CoreSetTag extends BodyTagSupport { 40 private static L10N L = new L10N(CoreSetTag.class); 41 42 private Object _value; 43 private boolean _hasValue; 44 private String _var; 45 private String _scope; 46 47 private Object _target; 48 private String _property; 49 50 53 public void setValue(Object value) 54 { 55 _value = value; 56 _hasValue = true; 57 } 58 59 62 public void setVar(String var) 63 { 64 _var = var; 65 } 66 67 70 public void setScope(String scope) 71 { 72 _scope = scope; 73 } 74 75 78 public void setTarget(Object target) 79 { 80 _target = target; 81 } 82 83 86 public void setProperty(String property) 87 { 88 _property = property; 89 } 90 91 94 public int doStartTag() 95 throws JspException 96 { 97 if (! _hasValue) 98 return EVAL_BODY_BUFFERED; 99 100 if (_var != null) 101 doSetValue(_value); 102 else 103 doSetProperty(_value); 104 105 return SKIP_BODY; 106 } 107 108 public int doEndTag() throws JspException 109 { 110 BodyContent body = (BodyContent ) getBodyContent(); 111 112 if (body != null) { 113 String value = body.getString().trim(); 114 115 if (_var != null) 116 doSetValue(value); 117 else 118 doSetProperty(value); 119 } 120 121 return EVAL_PAGE; 122 } 123 124 private void doSetValue(Object value) 125 throws JspException 126 { 127 setValue(pageContext, _var, _scope, value); 128 } 129 130 private void doSetProperty(Object value) 131 throws JspException 132 { 133 Expr.setProperty(_target, _property, value); 134 } 135 136 public static void setValue(PageContext pageContext, 137 String var, String scope, Object value) 138 throws JspException 139 { 140 if (var == null) { 141 } 142 else if (scope == null || scope.equals("page")) { 143 if (value != null) 144 pageContext.setAttribute(var, value); 145 else 146 pageContext.removeAttribute(var); 147 } 148 else if (scope.equals("request")) { 149 if (value != null) 150 pageContext.getRequest().setAttribute(var, value); 151 else 152 pageContext.getRequest().removeAttribute(var); 153 } 154 else if (scope.equals("session")) { 155 if (value != null) 156 pageContext.getSession().setAttribute(var, value); 157 else 158 pageContext.getSession().removeAttribute(var); 159 } 160 else if (scope.equals("application")) { 161 if (value != null) 162 pageContext.getServletContext().setAttribute(var, value); 163 else 164 pageContext.getServletContext().removeAttribute(var); 165 } 166 else 167 throw new JspException (L.l("illegal scope value {0}", scope)); 168 } 169 } 170 | Popular Tags |