001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless requiredSuffix by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.web.tag;
016:
017: import javax.servlet.jsp.JspException;
018: import javax.servlet.jsp.PageContext;
019: import javax.servlet.jsp.tagext.TagSupport;
020:
021: import org.apache.commons.beanutils.PropertyUtils;
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: /**
026: * Tag which allows variable in page context to be to any of the scopes (request, session, or application) using a
027: * supplied expression. Useful, for example, for binding the loop variable of an iteration to an action bean property,
028: * page bean property or view helper property
029: *
030: * @author Phil Zoio
031: */
032: public class BindTag extends TagSupport {
033:
034: Log log = LogFactory.getLog(BindTag.class);
035:
036: private static final long serialVersionUID = -4230698900508229618L;
037:
038: /**
039: * the source page context attribute name
040: */
041: private String sourceAttribute;
042:
043: /**
044: * the target attribute name. This is the attribute to which the property will be bound
045: */
046: private String targetExpression;
047:
048: /**
049: * the target scope. Can be "request", "session" or "application"
050: */
051: private String targetScope;
052:
053: // ------------------------------------------------------- Public Methods
054:
055: public int doStartTag() throws JspException {
056:
057: Object attribute = pageContext.getAttribute(sourceAttribute);
058:
059: if (attribute != null) {
060:
061: String targetAttribute = null;
062: String targetProperty = null;
063:
064: int firstDot = targetExpression.indexOf('.');
065: if (firstDot == -1) {
066: targetAttribute = targetExpression;
067: } else {
068: targetAttribute = targetExpression.substring(0,
069: firstDot);
070: targetProperty = targetExpression
071: .substring(firstDot + 1);
072: }
073:
074: Object bean = null;
075:
076: if (targetScope == null)
077: targetScope = "request";
078:
079: if ("page".equals(targetScope)) {
080: bean = pageContext.getAttribute(targetAttribute,
081: PageContext.PAGE_SCOPE);
082: } else if ("request".equals(targetScope)) {
083: bean = pageContext.getAttribute(targetAttribute,
084: PageContext.REQUEST_SCOPE);
085: } else if ("session".equals(targetScope)) {
086: bean = pageContext.getAttribute(targetAttribute,
087: PageContext.SESSION_SCOPE);
088: } else if ("application".equals(targetScope)) {
089: bean = pageContext.getAttribute(targetAttribute,
090: PageContext.APPLICATION_SCOPE);
091: } else {
092: throw new IllegalStateException(
093: "Invalid scope "
094: + targetScope
095: + ": must be 'request', 'session' or 'application'");
096: }
097:
098: if (bean == null) {
099: log.debug("No bean found for attribute "
100: + targetAttribute + " in scope " + targetScope);
101: } else {
102:
103: // attempt to set property using BeanUtils
104: try {
105: PropertyUtils.setProperty(bean, targetProperty,
106: attribute);
107: } catch (Exception e) {
108: throw new JspException("Unable to set property "
109: + targetProperty + " in bean "
110: + bean.getClass().getName()
111: + " with value " + attribute);
112: }
113:
114: }
115:
116: }
117: return (EVAL_BODY_INCLUDE);
118:
119: }
120:
121: /**
122: * Release any acquired resources.
123: */
124: public void release() {
125: super .release();
126: sourceAttribute = null;
127: targetExpression = null;
128: targetScope = null;
129: }
130:
131: /* ****** getters and setters ******* */
132:
133: public void setSourceAttribute(String sourceAttribute) {
134: this .sourceAttribute = sourceAttribute;
135: }
136:
137: public void setTargetExpression(String targetExpression) {
138: this .targetExpression = targetExpression;
139: }
140:
141: public void setTargetScope(String targetScope) {
142: this.targetScope = targetScope;
143: }
144:
145: }
|