001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Portions Copyright Apache Software Foundation.
007: *
008: * The contents of this file are subject to the terms of either the GNU
009: * General Public License Version 2 only ("GPL") or the Common Development
010: * and Distribution License("CDDL") (collectively, the "License"). You
011: * may not use this file except in compliance with the License. You can obtain
012: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
013: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
014: * language governing permissions and limitations under the License.
015: *
016: * When distributing the software, include this License Header Notice in each
017: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
018: * Sun designates this particular file as subject to the "Classpath" exception
019: * as provided by Sun in the GPL Version 2 section of the License file that
020: * accompanied this code. If applicable, add the following below the License
021: * Header, with the fields enclosed by brackets [] replaced by your own
022: * identifying information: "Portions Copyrighted [year]
023: * [name of copyright owner]"
024: *
025: * Contributor(s):
026: *
027: * If you wish your version of this file to be governed by only the CDDL or
028: * only the GPL Version 2, indicate your decision by adding "[Contributor]
029: * elects to include this software in this distribution under the [CDDL or GPL
030: * Version 2] license." If you don't indicate a single choice of license, a
031: * recipient has the option to distribute your version of this file under
032: * either the CDDL, the GPL Version 2 or to extend the choice of license to
033: * its licensees as provided above. However, if you add GPL Version 2 code
034: * and therefore, elected the GPL Version 2 license, then the option applies
035: * only if the new code is made subject to such option by the copyright
036: * holder.
037: */
038:
039: package org.apache.taglibs.standard.tag.common.core;
040:
041: import java.beans.IntrospectionException;
042: import java.beans.Introspector;
043: import java.beans.PropertyDescriptor;
044: import java.lang.reflect.InvocationTargetException;
045: import java.lang.reflect.Method;
046: import java.util.Map;
047:
048: import javax.el.ExpressionFactory;
049: import javax.el.ValueExpression;
050: import javax.el.VariableMapper;
051: import javax.el.ELException;
052:
053: import javax.servlet.jsp.JspApplicationContext;
054: import javax.servlet.jsp.JspFactory;
055: import javax.servlet.jsp.JspException;
056: import javax.servlet.jsp.JspTagException;
057: import javax.servlet.jsp.PageContext;
058: import javax.servlet.jsp.tagext.BodyTagSupport;
059:
060: import org.apache.taglibs.standard.resources.Resources;
061:
062: /**
063: * <p>Support for handlers of the <set> tag.</p>
064: *
065: * @author Shawn Bayern
066: */
067: public class SetSupport extends BodyTagSupport {
068:
069: //*********************************************************************
070: // Internal state
071:
072: protected Object value; // tag attribute
073: protected boolean valueSpecified; // status
074: protected Object target; // tag attribute
075: protected String property; // tag attribute
076: private String var; // tag attribute
077: private int scope; // tag attribute
078: private boolean scopeSpecified; // status
079:
080: //*********************************************************************
081: // Construction and initialization
082:
083: /**
084: * Constructs a new handler. As with TagSupport, subclasses should
085: * not provide other constructors and are expected to call the
086: * superclass constructor.
087: */
088: public SetSupport() {
089: super ();
090: init();
091: }
092:
093: // resets local state
094: private void init() {
095: value = var = null;
096: scopeSpecified = valueSpecified = false;
097: scope = PageContext.PAGE_SCOPE;
098: }
099:
100: // Releases any resources we may have (or inherit)
101: public void release() {
102: super .release();
103: init();
104: }
105:
106: //*********************************************************************
107: // Tag logic
108:
109: public int doEndTag() throws JspException {
110:
111: Object result; // what we'll store in scope:var
112:
113: // determine the value by...
114: if (value != null) {
115: // ... reading our attribute
116: result = value;
117: } else if (valueSpecified) {
118: // ... accepting an explicit null
119: result = null;
120: } else {
121: // ... retrieving and trimming our body
122: if (bodyContent == null || bodyContent.getString() == null)
123: result = "";
124: else
125: result = bodyContent.getString().trim();
126: }
127:
128: // decide what to do with the result
129: if (var != null) {
130:
131: /*
132: * Store the result, letting an IllegalArgumentException
133: * propagate back if the scope is invalid (e.g., if an attempt
134: * is made to store something in the session without any
135: * HttpSession existing).
136: */
137: if (result != null) {
138: if (result instanceof ValueExpression) {
139: if (scope != PageContext.PAGE_SCOPE) {
140: throw new JspException(Resources
141: .getMessage("SET_BAD_SCOPE_DEFERRED"));
142: }
143: VariableMapper vm = pageContext.getELContext()
144: .getVariableMapper();
145: if (vm != null) {
146: vm.setVariable(var, (ValueExpression) result);
147: }
148: } else {
149: // Make sure to clear any previous mapping for this
150: // variable in the variable mapper.
151: if (scope == PageContext.PAGE_SCOPE) {
152: VariableMapper vm = pageContext.getELContext()
153: .getVariableMapper();
154: if (vm != null) {
155: vm.setVariable(var, null);
156: }
157: }
158: pageContext.setAttribute(var, result, scope);
159: }
160: } else {
161: if (scopeSpecified)
162: pageContext.removeAttribute(var, scope);
163: else
164: pageContext.removeAttribute(var);
165:
166: if (scope == PageContext.PAGE_SCOPE) {
167: VariableMapper vm = pageContext.getELContext()
168: .getVariableMapper();
169: if (vm != null) {
170: vm.setVariable(var, null);
171: }
172: }
173: }
174:
175: } else if (target != null) {
176:
177: // save the result to target.property
178: if (target instanceof Map) {
179: // ... treating it as a Map entry
180: if (result == null)
181: ((Map) target).remove(property);
182: else
183: ((Map) target).put(property, result);
184: } else {
185: // ... treating it as a bean property
186: try {
187: PropertyDescriptor pd[] = Introspector.getBeanInfo(
188: target.getClass()).getPropertyDescriptors();
189: boolean succeeded = false;
190: for (int i = 0; i < pd.length; i++) {
191: if (pd[i].getName().equals(property)) {
192: Method m = pd[i].getWriteMethod();
193: if (m == null) {
194: throw new JspException(Resources
195: .getMessage(
196: "SET_NO_SETTER_METHOD",
197: property));
198: }
199: if (result != null) {
200: try {
201: m
202: .invoke(
203: target,
204: new Object[] { convertToExpectedType(
205: result,
206: m
207: .getParameterTypes()[0]) });
208: } catch (javax.el.ELException ex) {
209: throw new JspTagException(ex);
210: }
211: } else {
212: m.invoke(target, new Object[] { null });
213: }
214: succeeded = true;
215: }
216: }
217: if (!succeeded) {
218: throw new JspTagException(Resources.getMessage(
219: "SET_INVALID_PROPERTY", property));
220: }
221: } catch (IllegalAccessException ex) {
222: throw new JspException(ex);
223: } catch (IntrospectionException ex) {
224: throw new JspException(ex);
225: } catch (InvocationTargetException ex) {
226: throw new JspException(ex);
227: }
228: }
229: } else {
230: // should't ever occur because of validation in TLV and setters
231: throw new JspTagException();
232: }
233:
234: return EVAL_PAGE;
235: }
236:
237: /**
238: * Convert an object to an expected type according to the conversion
239: * rules of the Expression Language.
240: */
241: private Object convertToExpectedType(final Object value,
242: Class expectedType) {
243:
244: JspFactory jspFactory = JspFactory.getDefaultFactory();
245: JspApplicationContext jspAppContext = jspFactory
246: .getJspApplicationContext(pageContext
247: .getServletContext());
248: ExpressionFactory exprFactory = jspAppContext
249: .getExpressionFactory();
250: return exprFactory.coerceToType(value, expectedType);
251: }
252:
253: //*********************************************************************
254: // Accessor methods
255:
256: // for tag attribute
257: public void setVar(String var) {
258: this .var = var;
259: }
260:
261: // for tag attribute
262: public void setScope(String scope) {
263: this .scope = Util.getScope(scope);
264: this .scopeSpecified = true;
265: }
266: }
|