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