KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > rt > 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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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 JavaDoc;
35 import javax.servlet.jsp.PageContext JavaDoc;
36 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
37 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
38
39 public class CoreSetTag extends BodyTagSupport JavaDoc {
40   private static L10N L = new L10N(CoreSetTag.class);
41   
42   private Object JavaDoc _value;
43   private boolean _hasValue;
44   private String JavaDoc _var;
45   private String JavaDoc _scope;
46   
47   private Object JavaDoc _target;
48   private String JavaDoc _property;
49
50   /**
51    * Sets the JSP-EL expression value.
52    */

53   public void setValue(Object JavaDoc value)
54   {
55     _value = value;
56     _hasValue = true;
57   }
58
59   /**
60    * Sets the variable to assign
61    */

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

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

78   public void setTarget(Object JavaDoc target)
79   {
80     _target = target;
81   }
82
83   /**
84    * Sets the property to be set.
85    */

86   public void setProperty(String JavaDoc property)
87   {
88     _property = property;
89   }
90
91   /**
92    * Process the tag.
93    */

94   public int doStartTag()
95     throws JspException JavaDoc
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 JavaDoc
109   {
110     BodyContent JavaDoc body = (BodyContent JavaDoc) getBodyContent();
111
112     if (body != null) {
113       String JavaDoc 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 JavaDoc value)
125     throws JspException JavaDoc
126   {
127     setValue(pageContext, _var, _scope, value);
128   }
129   
130   private void doSetProperty(Object JavaDoc value)
131     throws JspException JavaDoc
132   {
133     Expr.setProperty(_target, _property, value);
134   }
135
136   public static void setValue(PageContext JavaDoc pageContext,
137                               String JavaDoc var, String JavaDoc scope, Object JavaDoc value)
138     throws JspException JavaDoc
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 JavaDoc(L.l("illegal scope value {0}", scope));
168   }
169 }
170
Popular Tags