Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/RuntimeCollective/webapps/tag/DefineTag.java


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/tag/DefineTag.java,v 1.7 2003/09/30 15:13:18 joe Exp $
2    * $Revision: 1.7 $
3    * $Date: 2003/09/30 15:13:18 $
4    *
5    * ====================================================================
6    *
7    * Josephine : http://www.runtime-collective.com/josephine/index.html
8    *
9    * Copyright (C) 2003 Runtime Collective
10   * 
11   * This product includes software developed by the
12   * Apache Software Foundation (http://www.apache.org/).
13   *
14   * This library is free software; you can redistribute it and/or
15   * modify it under the terms of the GNU Lesser General Public
16   * License as published by the Free Software Foundation; either
17   * version 2.1 of the License, or (at your option) any later version.
18   *
19   * This library is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   * Lesser General Public License for more details.
23   *
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this library; if not, write to the Free Software
26   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27   *
28   */
29  
30  package com.RuntimeCollective.webapps.tag;
31  
32  import com.RuntimeCollective.webapps.action.ActionContext;
33  
34  import java.io.IOException;
35  import java.lang.reflect.InvocationTargetException;
36  import javax.servlet.jsp.JspException;
37  import javax.servlet.jsp.PageContext;
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.jsp.tagext.TagSupport;
40  import org.apache.struts.util.RequestUtils;
41  import org.apache.commons.beanutils.PropertyUtils;
42  
43  /** Define a scripting variable based on the value(s) of the specified
44   *  bean property.
45   *
46   * <p>This tag takes the following parameters:</p>
47   * <ul>
48   *     <li><code>id</code> - the name of the scripting variable that will be populated with the value.</li>
49   *     <li><code>param</code> - the name of the request parameter that contains the value.</li>
50   *     <li><code>type</code> - the type of the bean to be defined.</li>
51   *     <li><code>toScope</code> - the scope in which to define the bean (default: page scope).</li>
52   *     <li><code>name</code> - the name of the variable to search for.</li>
53   *     <li><code>property</code> - the property of the named variable that contains the value.</li>
54   *     <li><code>scope</code> - the scope to search for the value in.</li>
55   *     <li><code>value</code> - the value to define the bean as.</li>
56   * </ul>
57   * <p>
58   * In addition to the usual <code>page</code>, <code>request</code> and <code>session</code> values,
59   * <code>scope</code> and <code>toScope</code> can be <code>action</code>, which refers to the
60   * request's ActionContext, if present.
61   *
62   * @version $Id: DefineTag.java,v 1.7 2003/09/30 15:13:18 joe Exp $
63   */
64  public class DefineTag extends TagSupport {
65  
66      public DefineTag() {
67          init();
68      }
69  
70  
71      // == Properties =========================================================
72  
73      private String id;
74      /** get the id. */
75      public String getId() { return id; }
76      /** set the id. */
77      public void setId(String id) { this.id = id; }
78  
79      private String param;
80      /** get the param. */
81      public String getParam() { return param; }
82      /** set the param. */
83      public void setParam(String param) { this.param = param; }
84  
85      private String type;
86      /** get the type. */
87      public String getType() { return type; }
88      /** set the type. */
89      public void setType(String type) { this.type = type; }
90  
91      private String toScope;
92      /** get the toScope. */
93      public String getToScope() { return toScope; }
94      /** set the toScope. */
95      public void setToScope(String toScope) { this.toScope = toScope; }
96  
97      private String name;
98      /** get the name. */
99      public String getName() { return name; }
100     /** set the name. */
101     public void setName(String name) { this.name = name; }
102 
103     private String property;
104     /** get the property. */
105     public String getProperty() { return property; }
106     /** set the property. */
107     public void setProperty(String property) { this.property = property; }
108 
109     private String scope;
110     /** get the scope. */
111     public String getScope() { return scope; }
112     /** set the scope. */
113     public void setScope(String scope) { this.scope = scope; }
114 
115     private Object value;
116     /** get the value. */
117     public Object getValue() { return value; }
118     /** set the value. */
119     public void setValue(Object value) { this.value = value; }
120 
121 
122     // == Tag methods ========================================================
123 
124     /** Retrieve the required property and expose it as a scripting variable.
125      *
126      * @exception JspException if a JSP exception has occurred
127      */
128     public int doStartTag() throws JspException {
129         if (null == value) {
130             lookupValue();
131         }
132 
133         if ("action".equals(toScope)) {
134 
135       // Put this variable into the current ActionContext
136       ActionContext actionCtx = new ActionContext();
137       actionCtx.setAttribute((HttpServletRequest) pageContext.getRequest(),
138            id,
139            value);
140       
141   } else {
142 
143       // Expose this value as a scripting variable
144       int inScope = PageContext.PAGE_SCOPE;
145       if ("request".equals(toScope)) {
146     inScope = PageContext.REQUEST_SCOPE;
147       } else if ("session".equals(toScope)) {
148     inScope = PageContext.SESSION_SCOPE;
149       } else if ("application".equals(toScope)) {
150     inScope = PageContext.APPLICATION_SCOPE;
151       }
152       
153       if (null != value) {
154     pageContext.setAttribute(id, value, inScope);
155       }
156   }
157 
158         return SKIP_BODY;
159     }
160 
161 
162     /** Release all allocated resources. */
163     public void release() {
164         super.release();
165 
166         init();
167     }
168 
169 
170     /** Lookup the value. */
171     protected void lookupValue() throws JspException {
172         if (null != param) {
173             // value is a request parameter
174             value = pageContext.getRequest().getParameter(param);
175         } else if (null != name) {
176             // value is a defined bean
177             if (null != scope && scope.equals("action")) {
178                 // value is defined in the ActionContext for this request
179                 ActionContext actionCtx = new ActionContext();
180 
181                 Object bean = actionCtx.getAttribute(
182                         (HttpServletRequest) pageContext.getRequest(),
183                         name);
184 
185                 if (null == property) {
186                     value = bean;
187                 } else {
188                     try {
189                         value = PropertyUtils.getProperty(bean, property);
190                     } catch (IllegalAccessException e) {
191                         // FIXME: use resource bundle
192                         throw new JspException(e.getMessage());
193                     } catch (InvocationTargetException e) {
194                         // FIXME: use resource bundle
195                         throw new JspException(e.getMessage());
196                     } catch (NoSuchMethodException e) {
197                         // FIXME: use resource bundle
198                         throw new JspException(e.getMessage());
199                     }
200                 }
201             } else {
202                 // value is defined in one of the standard scopes
203                 value = RequestUtils.lookup(pageContext, name, property, scope);
204             }
205         } 
206 
207     }
208     
209 
210     private void init() {
211         id = null;
212         param = null;
213         type = null;
214         toScope = "page";
215         name = null;
216         property = null;
217         value = null;
218     }
219 }