Source code: org/apache/struts/taglib/bean/DefineTag.java
1 /*
2 * $Id: DefineTag.java 54929 2004-10-16 16:38:42Z germuska $
3 *
4 * Copyright 1999-2004 The Apache Software Foundation.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19
20 package org.apache.struts.taglib.bean;
21
22
23 import javax.servlet.jsp.JspException;
24 import javax.servlet.jsp.PageContext;
25 import javax.servlet.jsp.tagext.BodyTagSupport;
26
27 import org.apache.struts.taglib.TagUtils;
28 import org.apache.struts.util.MessageResources;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32
33 /**
34 * Define a scripting variable based on the value(s) of the specified
35 * bean property.
36 *
37 * @version $Rev: 54929 $ $Date: 2004-10-16 09:38:42 -0700 (Sat, 16 Oct 2004) $
38 */
39
40 public class DefineTag extends BodyTagSupport {
41
42 /**
43 * Commons logging instance.
44 */
45 private static final Log log = LogFactory.getLog(DefineTag.class);
46
47 // ---------------------------------------------------- Protected variables
48
49 /**
50 * The message resources for this package.
51 */
52 protected static MessageResources messages =
53 MessageResources.getMessageResources
54 ("org.apache.struts.taglib.bean.LocalStrings");
55
56
57 /**
58 * The body content of this tag (if any).
59 */
60 protected String body = null;
61
62
63 // ------------------------------------------------------------- Properties
64
65
66 /**
67 * The name of the scripting variable that will be exposed as a page
68 * scope attribute.
69 */
70 protected String id = null;
71
72 public String getId() {
73 return (this.id);
74 }
75
76 public void setId(String id) {
77 this.id = id;
78 }
79
80
81 /**
82 * The name of the bean owning the property to be exposed.
83 */
84 protected String name = null;
85
86 public String getName() {
87 return (this.name);
88 }
89
90 public void setName(String name) {
91 this.name = name;
92 }
93
94
95 /**
96 * The name of the property to be retrieved.
97 */
98 protected String property = null;
99
100 public String getProperty() {
101 return (this.property);
102 }
103
104 public void setProperty(String property) {
105 this.property = property;
106 }
107
108
109 /**
110 * The scope within which to search for the specified bean.
111 */
112 protected String scope = null;
113
114 public String getScope() {
115 return (this.scope);
116 }
117
118 public void setScope(String scope) {
119 this.scope = scope;
120 }
121
122
123 /**
124 * The scope within which the newly defined bean will be creatd.
125 */
126 protected String toScope = null;
127
128 public String getToScope() {
129 return (this.toScope);
130 }
131
132 public void setToScope(String toScope) {
133 this.toScope = toScope;
134 }
135
136
137 /**
138 * The fully qualified Java class name of the value to be exposed.
139 */
140 protected String type = null;
141
142 public String getType() {
143 return (this.type);
144 }
145
146 public void setType(String type) {
147 this.type = type;
148 }
149
150
151 /**
152 * The (String) value to which the defined bean will be set.
153 */
154 protected String value = null;
155
156 public String getValue() {
157 return (this.value);
158 }
159
160 public void setValue(String value) {
161 this.value = value;
162 }
163
164
165 // --------------------------------------------------------- Public Methods
166
167
168 /**
169 * Check if we need to evaluate the body of the tag
170 *
171 * @exception JspException if a JSP exception has occurred
172 */
173 public int doStartTag() throws JspException {
174
175 return (EVAL_BODY_TAG);
176
177 }
178
179
180 /**
181 * Save the body content of this tag (if any), or throw a JspException
182 * if the value was already defined.
183 *
184 * @exception JspException if value was defined by an attribute
185 */
186 public int doAfterBody() throws JspException {
187
188 if (bodyContent != null) {
189 body = bodyContent.getString();
190 if (body != null) {
191 body = body.trim();
192 }
193 if (body.length() < 1) {
194 body = null;
195 }
196 }
197 return (SKIP_BODY);
198
199 }
200
201
202 /**
203 * Retrieve the required property and expose it as a scripting variable.
204 *
205 * @exception JspException if a JSP exception has occurred
206 */
207 public int doEndTag() throws JspException {
208
209 // Enforce restriction on ways to declare the new value
210 int n = 0;
211 if (this.body != null) {
212 n++;
213 }
214 if (this.name != null) {
215 n++;
216 }
217 if (this.value != null) {
218 n++;
219 }
220 if (n != 1) {
221 JspException e =
222 new JspException(messages.getMessage("define.value"));
223 TagUtils.getInstance().saveException(pageContext, e);
224 throw e;
225 }
226
227 // Retrieve the required property value
228 Object value = this.value;
229 if ((value == null) && (name != null)) {
230 value = TagUtils.getInstance().lookup(pageContext, name, property, scope);
231 }
232 if ((value == null) && (body != null)) {
233 value = body;
234 }
235 if (value == null) {
236 JspException e =
237 new JspException(messages.getMessage("define.null"));
238 TagUtils.getInstance().saveException(pageContext, e);
239 throw e;
240 }
241
242 // Expose this value as a scripting variable
243 int inScope = PageContext.PAGE_SCOPE;
244 try {
245 if (toScope != null) {
246 inScope = TagUtils.getInstance().getScope(toScope);
247 }
248 } catch (JspException e) {
249 log.warn("toScope was invalid name so we default to PAGE_SCOPE",e);
250 }
251
252 pageContext.setAttribute(id, value, inScope);
253
254 // Continue processing this page
255 return (EVAL_PAGE);
256
257 }
258
259 /**
260 * Release all allocated resources.
261 */
262 public void release() {
263
264 super.release();
265 body = null;
266 id = null;
267 name = null;
268 property = null;
269 scope = null;
270 toScope = "page";
271 type = null;
272 value = null;
273
274 }
275
276
277 }