KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > el > CoreSetTag


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
37 import javax.servlet.jsp.PageContext JavaDoc;
38 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
39 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
40
41 public class CoreSetTag extends BodyTagSupport JavaDoc {
42   private static L10N L = new L10N(CoreSetTag.class);
43   private Expr valueExpr;
44   private String JavaDoc var;
45   private String JavaDoc scope;
46   
47   private Expr targetExpr;
48   private Expr propertyExpr;
49
50   /**
51    * Sets the JSP-EL expression value.
52    */

53   public void setValue(Expr value)
54   {
55     this.valueExpr = value;
56   }
57
58   /**
59    * Sets the variable to assign
60    */

61   public void setVar(String JavaDoc var)
62   {
63     this.var = var;
64   }
65
66   /**
67    * Sets the scope of the variable.
68    */

69   public void setScope(String JavaDoc scope)
70   {
71     this.scope = scope;
72   }
73
74   /**
75    * Sets the target to be set.
76    */

77   public void setTarget(Expr target)
78   {
79     this.targetExpr = target;
80   }
81
82   /**
83    * Sets the property to be set.
84    */

85   public void setProperty(Expr property)
86   {
87     this.propertyExpr = property;
88   }
89
90   /**
91    * Process the tag.
92    */

93   public int doStartTag()
94     throws JspException JavaDoc
95   {
96     try {
97       if (valueExpr == null)
98     return EVAL_BODY_BUFFERED;
99
100       ELContext env = pageContext.getELContext();
101
102       Object JavaDoc value = valueExpr.evalObject(env);
103       if (var != null)
104     setValue(value);
105       else
106     setProperty(value);
107
108       return SKIP_BODY;
109     } catch (Exception JavaDoc e) {
110       throw new JspException JavaDoc(e);
111     }
112   }
113
114   public int doEndTag() throws JspException JavaDoc
115   {
116     BodyContent JavaDoc body = (BodyContent JavaDoc) 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 JavaDoc value)
129     throws JspException JavaDoc
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 JavaDoc(L.l("illegal scope value {0}", scope));
163   }
164   
165   private void setProperty(Object JavaDoc value)
166     throws JspException JavaDoc
167   {
168     try {
169       ELContext env = pageContext.getELContext();
170
171       Object JavaDoc target = targetExpr.evalObject(env);
172       String JavaDoc property = propertyExpr.evalString(env);
173
174       Expr.setProperty(target, property, value);
175     } catch (Exception JavaDoc e) {
176       throw new JspException JavaDoc(e);
177     }
178   }
179
180   public static void setValue(PageContext JavaDoc pageContext,
181                               String JavaDoc var, String JavaDoc scope, Object JavaDoc value)
182     throws JspException JavaDoc
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 JavaDoc(L.l("illegal scope value {0}", scope));
212   }
213 }
214
Popular Tags