001: /*
002: * $Id: DefineTag.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.taglib.bean;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.struts.taglib.TagUtils;
026: import org.apache.struts.util.MessageResources;
027:
028: import javax.servlet.jsp.JspException;
029: import javax.servlet.jsp.PageContext;
030: import javax.servlet.jsp.tagext.BodyTagSupport;
031:
032: /**
033: * Define a scripting variable based on the value(s) of the specified bean
034: * property.
035: *
036: * @version $Rev: 471754 $ $Date: 2005-06-15 12:16:32 -0400 (Wed, 15 Jun 2005)
037: * $
038: */
039: public class DefineTag extends BodyTagSupport {
040: /**
041: * Commons logging instance.
042: */
043: private static final Log log = LogFactory.getLog(DefineTag.class);
044:
045: // ---------------------------------------------------- Protected variables
046:
047: /**
048: * The message resources for this package.
049: */
050: protected static MessageResources messages = MessageResources
051: .getMessageResources("org.apache.struts.taglib.bean.LocalStrings");
052:
053: /**
054: * The body content of this tag (if any).
055: */
056: protected String body = null;
057:
058: // ------------------------------------------------------------- Properties
059:
060: /**
061: * The name of the scripting variable that will be exposed as a page scope
062: * attribute.
063: */
064: protected String id = null;
065:
066: /**
067: * The name of the bean owning the property to be exposed.
068: */
069: protected String name = null;
070:
071: /**
072: * The name of the property to be retrieved.
073: */
074: protected String property = null;
075:
076: /**
077: * The scope within which to search for the specified bean.
078: */
079: protected String scope = null;
080:
081: /**
082: * The scope within which the newly defined bean will be creatd.
083: */
084: protected String toScope = null;
085:
086: /**
087: * The fully qualified Java class name of the value to be exposed.
088: */
089: protected String type = null;
090:
091: /**
092: * The (String) value to which the defined bean will be set.
093: */
094: protected String value = null;
095:
096: public String getId() {
097: return (this .id);
098: }
099:
100: public void setId(String id) {
101: this .id = id;
102: }
103:
104: public String getName() {
105: return (this .name);
106: }
107:
108: public void setName(String name) {
109: this .name = name;
110: }
111:
112: public String getProperty() {
113: return (this .property);
114: }
115:
116: public void setProperty(String property) {
117: this .property = property;
118: }
119:
120: public String getScope() {
121: return (this .scope);
122: }
123:
124: public void setScope(String scope) {
125: this .scope = scope;
126: }
127:
128: public String getToScope() {
129: return (this .toScope);
130: }
131:
132: public void setToScope(String toScope) {
133: this .toScope = toScope;
134: }
135:
136: public String getType() {
137: return (this .type);
138: }
139:
140: public void setType(String type) {
141: this .type = type;
142: }
143:
144: public String getValue() {
145: return (this .value);
146: }
147:
148: public void setValue(String value) {
149: this .value = value;
150: }
151:
152: // --------------------------------------------------------- Public Methods
153:
154: /**
155: * Check if we need to evaluate the body of the tag
156: *
157: * @throws JspException if a JSP exception has occurred
158: */
159: public int doStartTag() throws JspException {
160: return (EVAL_BODY_TAG);
161: }
162:
163: /**
164: * Save the body content of this tag (if any), or throw a JspException if
165: * the value was already defined.
166: *
167: * @throws JspException if value was defined by an attribute
168: */
169: public int doAfterBody() throws JspException {
170: if (bodyContent != null) {
171: body = bodyContent.getString();
172:
173: if (body != null) {
174: body = body.trim();
175: }
176:
177: if (body.length() < 1) {
178: body = null;
179: }
180: }
181:
182: return (SKIP_BODY);
183: }
184:
185: /**
186: * Retrieve the required property and expose it as a scripting variable.
187: *
188: * @throws JspException if a JSP exception has occurred
189: */
190: public int doEndTag() throws JspException {
191: // Enforce restriction on ways to declare the new value
192: int n = 0;
193:
194: if (this .body != null) {
195: n++;
196: }
197:
198: if (this .name != null) {
199: n++;
200: }
201:
202: if (this .value != null) {
203: n++;
204: }
205:
206: if (n > 1) {
207: JspException e = new JspException(messages.getMessage(
208: "define.value", id));
209:
210: TagUtils.getInstance().saveException(pageContext, e);
211: throw e;
212: }
213:
214: // Retrieve the required property value
215: Object value = this .value;
216:
217: if ((value == null) && (name != null)) {
218: value = TagUtils.getInstance().lookup(pageContext, name,
219: property, scope);
220: }
221:
222: if ((value == null) && (body != null)) {
223: value = body;
224: }
225:
226: if (value == null) {
227: JspException e = new JspException(messages.getMessage(
228: "define.null", id));
229:
230: TagUtils.getInstance().saveException(pageContext, e);
231: throw e;
232: }
233:
234: // Expose this value as a scripting variable
235: int inScope = PageContext.PAGE_SCOPE;
236:
237: try {
238: if (toScope != null) {
239: inScope = TagUtils.getInstance().getScope(toScope);
240: }
241: } catch (JspException e) {
242: log
243: .warn(
244: "toScope was invalid name so we default to PAGE_SCOPE",
245: e);
246: }
247:
248: pageContext.setAttribute(id, value, inScope);
249:
250: // Continue processing this page
251: return (EVAL_PAGE);
252: }
253:
254: /**
255: * Release all allocated resources.
256: */
257: public void release() {
258: super .release();
259: body = null;
260: id = null;
261: name = null;
262: property = null;
263: scope = null;
264: toScope = "page";
265: type = null;
266: value = null;
267: }
268: }
|