KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > core > SetSupport


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.tag.common.core;
18
19 import java.beans.IntrospectionException JavaDoc;
20 import java.beans.Introspector JavaDoc;
21 import java.beans.PropertyDescriptor JavaDoc;
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.JspTagException JavaDoc;
28 import javax.servlet.jsp.PageContext JavaDoc;
29 import javax.servlet.jsp.el.ELException JavaDoc;
30 import javax.servlet.jsp.el.ExpressionEvaluator JavaDoc;
31 import javax.servlet.jsp.el.VariableResolver JavaDoc;
32 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
33
34 import org.apache.taglibs.standard.resources.Resources;
35
36 /**
37  * <p>Support for handlers of the &lt;set&gt; tag.</p>
38  *
39  * @author Shawn Bayern
40  */

41 public class SetSupport extends BodyTagSupport JavaDoc {
42
43     //*********************************************************************
44
// Internal state
45

46     protected Object JavaDoc value; // tag attribute
47
protected boolean valueSpecified; // status
48
protected Object JavaDoc target; // tag attribute
49
protected String JavaDoc property; // tag attribute
50
private String JavaDoc var; // tag attribute
51
private int scope; // tag attribute
52
private boolean scopeSpecified; // status
53

54     //*********************************************************************
55
// Construction and initialization
56

57     /**
58      * Constructs a new handler. As with TagSupport, subclasses should
59      * not provide other constructors and are expected to call the
60      * superclass constructor.
61      */

62     public SetSupport() {
63         super();
64         init();
65     }
66
67     // resets local state
68
private void init() {
69         value = var = null;
70     scopeSpecified = valueSpecified = false;
71     scope = PageContext.PAGE_SCOPE;
72     }
73
74     // Releases any resources we may have (or inherit)
75
public void release() {
76         super.release();
77         init();
78     }
79
80
81     //*********************************************************************
82
// Tag logic
83

84     public int doEndTag() throws JspException JavaDoc {
85
86         Object JavaDoc result; // what we'll store in scope:var
87

88         // determine the value by...
89
if (value != null) {
90         // ... reading our attribute
91
result = value;
92     } else if (valueSpecified) {
93         // ... accepting an explicit null
94
result = null;
95     } else {
96         // ... retrieving and trimming our body
97
if (bodyContent == null || bodyContent.getString() == null)
98         result = "";
99         else
100             result = bodyContent.getString().trim();
101     }
102
103     // decide what to do with the result
104
if (var != null) {
105
106         /*
107              * Store the result, letting an IllegalArgumentException
108              * propagate back if the scope is invalid (e.g., if an attempt
109              * is made to store something in the session without any
110          * HttpSession existing).
111              */

112         if (result != null) {
113             pageContext.setAttribute(var, result, scope);
114         } else {
115         if (scopeSpecified)
116             pageContext.removeAttribute(var, scope);
117         else
118             pageContext.removeAttribute(var);
119         }
120
121     } else if (target != null) {
122
123         // save the result to target.property
124
if (target instanceof Map JavaDoc) {
125         // ... treating it as a Map entry
126
if (result == null)
127             ((Map JavaDoc) target).remove(property);
128         else
129             ((Map JavaDoc) target).put(property, result);
130         } else {
131         // ... treating it as a bean property
132
try {
133                     PropertyDescriptor JavaDoc pd[] =
134                         Introspector.getBeanInfo(target.getClass())
135                 .getPropertyDescriptors();
136             boolean succeeded = false;
137                     for (int i = 0; i < pd.length; i++) {
138                         if (pd[i].getName().equals(property)) {
139                 Method JavaDoc m = pd[i].getWriteMethod();
140                             if (m == null) {
141                                 throw new JspException JavaDoc(
142                                     Resources.getMessage("SET_NO_SETTER_METHOD",
143                         property));
144                             }
145                 if (result != null) {
146                                 try {
147                     m.invoke(target,
148                          new Object JavaDoc[] {
149                                          convertToExpectedType(result, m.getParameterTypes()[0])});
150                                 } catch (javax.servlet.jsp.el.ELException JavaDoc ex) {
151                                     throw new JspTagException JavaDoc(ex);
152                                 }
153                 } else {
154                 m.invoke(target, new Object JavaDoc[] { null });
155                 }
156                 succeeded = true;
157             }
158             }
159             if (!succeeded) {
160             throw new JspTagException JavaDoc(
161                 Resources.getMessage("SET_INVALID_PROPERTY",
162                 property));
163             }
164         } catch (IllegalAccessException JavaDoc ex) {
165             throw new JspException JavaDoc(ex);
166         } catch (IntrospectionException JavaDoc ex) {
167             throw new JspException JavaDoc(ex);
168         } catch (InvocationTargetException JavaDoc ex) {
169             throw new JspException JavaDoc(ex);
170         }
171         }
172     } else {
173         // should't ever occur because of validation in TLV and setters
174
throw new JspTagException JavaDoc();
175     }
176
177     return EVAL_PAGE;
178     }
179     
180     /**
181      * Convert an object to an expected type according to the conversion
182      * rules of the Expression Language.
183      */

184     private Object JavaDoc convertToExpectedType( final Object JavaDoc value,
185     Class JavaDoc expectedType )
186     throws javax.servlet.jsp.el.ELException JavaDoc {
187         ExpressionEvaluator JavaDoc evaluator = pageContext.getExpressionEvaluator();
188         return evaluator.evaluate( "${result}", expectedType,
189         new VariableResolver JavaDoc() {
190             public Object JavaDoc resolveVariable( String JavaDoc pName )
191             throws ELException JavaDoc {
192                 return value;
193             }
194         }, null );
195     }
196
197     //*********************************************************************
198
// Accessor methods
199

200     // for tag attribute
201
public void setVar(String JavaDoc var) {
202     this.var = var;
203     }
204
205     // for tag attribute
206
public void setScope(String JavaDoc scope) {
207         this.scope = Util.getScope(scope);
208     this.scopeSpecified = true;
209     }
210 }
211
Popular Tags