View | Details | Raw Unified | Return to bug 33934
Collapse All | Expand All

(-)src/test/java/org/apache/taglibs/standard/tag/common/core/SetSupportTest.java (-60 / +88 lines)
Lines 64-71 Link Here
64
64
65
        bean = new Bean();
65
        bean = new Bean();
66
66
67
        tag = new SetSupport();
68
        tag.setPageContext(pageContext);
69
67
70
        ExpressionFactory expressionFactory = createMock(ExpressionFactory.class);
68
        ExpressionFactory expressionFactory = createMock(ExpressionFactory.class);
71
        JspApplicationContext applicationContext = createMock(JspApplicationContext.class);
69
        JspApplicationContext applicationContext = createMock(JspApplicationContext.class);
Lines 85-93 Link Here
85
83
86
    @Test
84
    @Test
87
    public void testSyntax1WithNoScope() throws JspException {
85
    public void testSyntax1WithNoScope() throws JspException {
86
        tag = new MockSetSupport(VALUE);
87
        tag.setPageContext(pageContext);
88
        tag.setVar(VAR);
88
        tag.setVar(VAR);
89
        tag.valueSpecified = true;
90
        tag.value = VALUE;
91
89
92
        // verify mapper is checked but that no action is taken
90
        // verify mapper is checked but that no action is taken
93
        expect(vm.resolveVariable(VAR)).andReturn(null);
91
        expect(vm.resolveVariable(VAR)).andReturn(null);
Lines 99-108 Link Here
99
97
100
    @Test
98
    @Test
101
    public void testSyntax1WithNullScope() throws JspException {
99
    public void testSyntax1WithNullScope() throws JspException {
100
        tag = new MockSetSupport(VALUE);
101
        tag.setPageContext(pageContext);
102
        tag.setVar(VAR);
102
        tag.setVar(VAR);
103
        tag.setScope(null);
103
        tag.setScope(null);
104
        tag.valueSpecified = true;
105
        tag.value = VALUE;
106
104
107
        // verify mapper is checked but that no action is taken
105
        // verify mapper is checked but that no action is taken
108
        expect(vm.resolveVariable(VAR)).andReturn(null);
106
        expect(vm.resolveVariable(VAR)).andReturn(null);
Lines 114-123 Link Here
114
112
115
    @Test
113
    @Test
116
    public void testSyntax1WithPageScope() throws JspException {
114
    public void testSyntax1WithPageScope() throws JspException {
115
        tag = new MockSetSupport(VALUE);
116
        tag.setPageContext(pageContext);
117
        tag.setVar(VAR);
117
        tag.setVar(VAR);
118
        tag.setScope("page");
118
        tag.setScope("page");
119
        tag.valueSpecified = true;
120
        tag.value = VALUE;
121
119
122
        // verify mapper is checked but that no action is taken
120
        // verify mapper is checked but that no action is taken
123
        expect(vm.resolveVariable(VAR)).andReturn(null);
121
        expect(vm.resolveVariable(VAR)).andReturn(null);
Lines 129-138 Link Here
129
127
130
    @Test
128
    @Test
131
    public void testSyntax1WithNonPageScope() throws JspException {
129
    public void testSyntax1WithNonPageScope() throws JspException {
130
        tag = new MockSetSupport(VALUE);
131
        tag.setPageContext(pageContext);
132
        tag.setVar(VAR);
132
        tag.setVar(VAR);
133
        tag.setScope("request");
133
        tag.setScope("request");
134
        tag.valueSpecified = true;
135
        tag.value = VALUE;
136
134
137
        // verify mapper is not checked
135
        // verify mapper is not checked
138
        pageContext.setAttribute(VAR, VALUE, PageContext.REQUEST_SCOPE);
136
        pageContext.setAttribute(VAR, VALUE, PageContext.REQUEST_SCOPE);
Lines 143-151 Link Here
143
141
144
    @Test
142
    @Test
145
    public void testSyntax1WithNullValueAndNoScope() throws JspException {
143
    public void testSyntax1WithNullValueAndNoScope() throws JspException {
144
        tag = new MockSetSupport(null);
145
        tag.setPageContext(pageContext);
146
        tag.setVar(VAR);
146
        tag.setVar(VAR);
147
        tag.valueSpecified = true;
148
        tag.value = null;
149
147
150
        // verify mapper is checked but that no action is taken
148
        // verify mapper is checked but that no action is taken
151
        expect(vm.resolveVariable(VAR)).andReturn(null);
149
        expect(vm.resolveVariable(VAR)).andReturn(null);
Lines 157-166 Link Here
157
155
158
    @Test
156
    @Test
159
    public void testSyntax1WithNullValueAndNonPageScope() throws JspException {
157
    public void testSyntax1WithNullValueAndNonPageScope() throws JspException {
158
        tag = new MockSetSupport(null);
159
        tag.setPageContext(pageContext);
160
        tag.setVar(VAR);
160
        tag.setVar(VAR);
161
        tag.setScope("request");
161
        tag.setScope("request");
162
        tag.valueSpecified = true;
163
        tag.value = null;
164
162
165
        // verify mapper is checked but that no action is taken
163
        // verify mapper is checked but that no action is taken
166
        expect(vm.resolveVariable(VAR)).andReturn(null);
164
        expect(vm.resolveVariable(VAR)).andReturn(null);
Lines 174-184 Link Here
174
    public void testSyntax3WithMap() throws JspException {
172
    public void testSyntax3WithMap() throws JspException {
175
        @SuppressWarnings("unchecked")
173
        @SuppressWarnings("unchecked")
176
        Map<String, Object> target = createMock(Map.class);
174
        Map<String, Object> target = createMock(Map.class);
177
        tag.target = target;
175
        tag = new MockSetSupport(VALUE, target, PROPERTY);
178
        tag.property = PROPERTY;
176
        tag.setPageContext(pageContext);
179
        tag.valueSpecified = true;
177
180
        tag.value = VALUE;
181
        
182
        expect(target.put(PROPERTY, VALUE)).andStubReturn(null);
178
        expect(target.put(PROPERTY, VALUE)).andStubReturn(null);
183
        replay(target);
179
        replay(target);
184
        tag.doEndTag();
180
        tag.doEndTag();
Lines 189-198 Link Here
189
    public void testSyntax3WithMapWhenPropertyIsNull() throws JspException {
185
    public void testSyntax3WithMapWhenPropertyIsNull() throws JspException {
190
        @SuppressWarnings("unchecked")
186
        @SuppressWarnings("unchecked")
191
        Map<String, Object> target = createMock(Map.class);
187
        Map<String, Object> target = createMock(Map.class);
192
        tag.target = target;
188
        tag = new MockSetSupport(VALUE, target, null);
193
        tag.property = null;
189
        tag.setPageContext(pageContext);
194
        tag.valueSpecified = true;
195
        tag.value = VALUE;
196
190
197
        expect(target.put(null, VALUE)).andStubReturn(null);
191
        expect(target.put(null, VALUE)).andStubReturn(null);
198
        replay(target);
192
        replay(target);
Lines 204-213 Link Here
204
    public void testSyntax3WithMapWhenValueIsNull() throws JspException {
198
    public void testSyntax3WithMapWhenValueIsNull() throws JspException {
205
        @SuppressWarnings("unchecked")
199
        @SuppressWarnings("unchecked")
206
        Map<String, Object> target = createMock(Map.class);
200
        Map<String, Object> target = createMock(Map.class);
207
        tag.target = target;
201
        tag = new MockSetSupport(null, target, PROPERTY);
208
        tag.property = PROPERTY;
202
        tag.setPageContext(pageContext);
209
        tag.valueSpecified = true;
210
        tag.value = null;
211
203
212
        expect(target.remove(PROPERTY)).andStubReturn(null);
204
        expect(target.remove(PROPERTY)).andStubReturn(null);
213
        replay(target);
205
        replay(target);
Lines 217-226 Link Here
217
209
218
    @Test
210
    @Test
219
    public void testSyntax3WithBean() throws JspException {
211
    public void testSyntax3WithBean() throws JspException {
220
        tag.target = bean;
212
        tag = new MockSetSupport(VALUE, bean, PROPERTY);
221
        tag.property = PROPERTY;
213
        tag.setPageContext(pageContext);
222
        tag.valueSpecified = true;
223
        tag.value = VALUE;
224
214
225
        tag.doEndTag();
215
        tag.doEndTag();
226
        Assert.assertEquals(VALUE, bean.getProperty());
216
        Assert.assertEquals(VALUE, bean.getProperty());
Lines 228-237 Link Here
228
218
229
    @Test
219
    @Test
230
    public void testSyntax3WithBeanAndNullValue() throws JspException {
220
    public void testSyntax3WithBeanAndNullValue() throws JspException {
231
        tag.target = bean;
221
        tag = new MockSetSupport(null, bean, PROPERTY);
232
        tag.property = PROPERTY;
222
        tag.setPageContext(pageContext);
233
        tag.valueSpecified = true;
234
        tag.value = null;
235
223
236
        tag.doEndTag();
224
        tag.doEndTag();
237
        Assert.assertNull(bean.getProperty());
225
        Assert.assertNull(bean.getProperty());
Lines 239-248 Link Here
239
227
240
    @Test
228
    @Test
241
    public void testSyntax3WithBeanAndUndefinedProperty() throws JspException {
229
    public void testSyntax3WithBeanAndUndefinedProperty() throws JspException {
242
        tag.target = bean;
230
        tag = new MockSetSupport(VALUE, bean, "undefined");
243
        tag.property = "undefined";
231
        tag.setPageContext(pageContext);
244
        tag.valueSpecified = true;
245
        tag.value = VALUE;
246
232
247
        try {
233
        try {
248
            tag.doEndTag();
234
            tag.doEndTag();
Lines 253-262 Link Here
253
239
254
    @Test
240
    @Test
255
    public void testSyntax3WithBeanAndReadOnlyProperty() throws JspException {
241
    public void testSyntax3WithBeanAndReadOnlyProperty() throws JspException {
256
        tag.target = bean;
242
        tag = new MockSetSupport(VALUE, bean, "readOnly");
257
        tag.property = "readOnly";
243
        tag.setPageContext(pageContext);
258
        tag.valueSpecified = true;
259
        tag.value = VALUE;
260
244
261
        try {
245
        try {
262
            tag.doEndTag();
246
            tag.doEndTag();
Lines 267-276 Link Here
267
251
268
    @Test
252
    @Test
269
    public void testSyntax3WhenTargetIsNull() throws JspException {
253
    public void testSyntax3WhenTargetIsNull() throws JspException {
270
        tag.target = null;
254
        tag = new MockSetSupport(VALUE, null, PROPERTY);
271
        tag.property = PROPERTY;
255
        tag.setPageContext(pageContext);
272
        tag.valueSpecified = true;
273
        tag.value = VALUE;
274
256
275
        try {
257
        try {
276
            tag.doEndTag();
258
            tag.doEndTag();
Lines 287-295 Link Here
287
     */
269
     */
288
    @Test
270
    @Test
289
    public void test49526WhenNotMapped() throws JspException {
271
    public void test49526WhenNotMapped() throws JspException {
272
        tag = new MockSetSupport(VALUE);
273
        tag.setPageContext(pageContext);
290
        tag.setVar(VAR);
274
        tag.setVar(VAR);
291
        tag.valueSpecified = true;
292
        tag.value = VALUE;
293
275
294
        // verify mapper is checked but that no action is taken
276
        // verify mapper is checked but that no action is taken
295
        expect(vm.resolveVariable(VAR)).andReturn(null);
277
        expect(vm.resolveVariable(VAR)).andReturn(null);
Lines 306-314 Link Here
306
     */
288
     */
307
    @Test
289
    @Test
308
    public void test49526WhenAlreadyMapped() throws JspException {
290
    public void test49526WhenAlreadyMapped() throws JspException {
291
        tag = new MockSetSupport(VALUE);
292
        tag.setPageContext(pageContext);
309
        tag.setVar(VAR);
293
        tag.setVar(VAR);
310
        tag.valueSpecified = true;
311
        tag.value = VALUE;
312
294
313
        // verify mapper is checked and the mapped variable removed
295
        // verify mapper is checked and the mapped variable removed
314
        ValueExpression ve = createMock(ValueExpression.class);
296
        ValueExpression ve = createMock(ValueExpression.class);
Lines 327-335 Link Here
327
     */
309
     */
328
    @Test
310
    @Test
329
    public void test49526WhenNotUsingPageContext() throws JspException {
311
    public void test49526WhenNotUsingPageContext() throws JspException {
312
        tag = new MockSetSupport(VALUE);
313
        tag.setPageContext(pageContext);
330
        tag.setVar(VAR);
314
        tag.setVar(VAR);
331
        tag.valueSpecified = true;
332
        tag.value = VALUE;
333
        tag.setScope("request");
315
        tag.setScope("request");
334
316
335
        // verify mapper is not checked
317
        // verify mapper is not checked
Lines 341-361 Link Here
341
323
342
    @Test
324
    @Test
343
    public void testResultFromValueAttribute() {
325
    public void testResultFromValueAttribute() {
344
        tag.valueSpecified = true;
326
        tag = new MockSetSupport(VALUE);
345
        tag.value = VALUE;
346
        Assert.assertSame(VALUE, tag.getResult());
327
        Assert.assertSame(VALUE, tag.getResult());
347
    }
328
    }
348
329
349
    @Test
330
    @Test
350
    public void testResultFromNullValueAttribute() {
331
    public void testResultFromNullValueAttribute() {
351
        tag.valueSpecified = true;
332
        tag = new MockSetSupport(null);
352
        tag.value = null;
353
        Assert.assertNull(tag.getResult());
333
        Assert.assertNull(tag.getResult());
354
    }
334
    }
355
335
356
    @Test
336
    @Test
357
    public void testResultFromBodyContent() {
337
    public void testResultFromBodyContent() {
358
        tag.valueSpecified = false;
338
        tag = new MockSetSupport();
359
        BodyContent bodyContent = createMock(BodyContent.class);
339
        BodyContent bodyContent = createMock(BodyContent.class);
360
        expect(bodyContent.getString()).andStubReturn("  Hello  ");
340
        expect(bodyContent.getString()).andStubReturn("  Hello  ");
361
        replay(bodyContent);
341
        replay(bodyContent);
Lines 365-383 Link Here
365
345
366
    @Test
346
    @Test
367
    public void testResultFromNullBodyContent() {
347
    public void testResultFromNullBodyContent() {
368
        tag.valueSpecified = false;
348
        tag = new MockSetSupport();
369
        tag.setBodyContent(null);
349
        tag.setBodyContent(null);
370
        Assert.assertEquals("", tag.getResult());
350
        Assert.assertEquals("", tag.getResult());
371
    }
351
    }
372
352
373
    @Test
353
    @Test
374
    public void testResultFromEmptyBodyContent() {
354
    public void testResultFromEmptyBodyContent() {
375
        tag.valueSpecified = false;
355
        tag = new MockSetSupport();
376
        BodyContent bodyContent = createMock(BodyContent.class);
356
        BodyContent bodyContent = createMock(BodyContent.class);
377
        expect(bodyContent.getString()).andStubReturn(null);
357
        expect(bodyContent.getString()).andStubReturn(null);
378
        Assert.assertEquals("", tag.getResult());
358
        Assert.assertEquals("", tag.getResult());
379
    }
359
    }
380
360
361
    public static class MockSetSupport extends SetSupport {
362
        private final boolean valueSpecified;
363
        private final Object value;
364
        private final Object target;
365
        private final String property;
366
367
        public MockSetSupport() {
368
            this.value = null;
369
            this.valueSpecified = false;
370
            this.target = null;
371
            this.property = null;
372
        }
373
374
        public MockSetSupport(Object value, Object target, String property) {
375
            this.value = value;
376
            this.valueSpecified = true;
377
            this.target = target;
378
            this.property = property;
379
        }
380
381
        public MockSetSupport(Object value) {
382
            this.value = value;
383
            this.valueSpecified = true;
384
            this.target = null;
385
            this.property = null;
386
        }
387
388
        @Override
389
        protected boolean isValueSpecified() {
390
            return valueSpecified;
391
        }
392
393
        @Override
394
        protected Object evalValue() {
395
            return value;
396
        }
397
398
        @Override
399
        protected Object evalTarget() {
400
            return target;
401
        }
402
403
        @Override
404
        protected String evalProperty() {
405
            return property;
406
        }
407
    }
408
381
    public static class Bean {
409
    public static class Bean {
382
        private String property;
410
        private String property;
383
411
(-)src/main/java/org/apache/taglibs/standard/tag/el/core/SetTag.java (-81 / +36 lines)
Lines 13-130 Link Here
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
15
 * limitations under the License.
16
 */ 
16
 */
17
17
18
package org.apache.taglibs.standard.tag.el.core;
18
package org.apache.taglibs.standard.tag.el.core;
19
19
20
import javax.servlet.jsp.JspException;
20
import javax.el.ValueExpression;
21
21
22
import org.apache.taglibs.standard.tag.common.core.NullAttributeException;
23
import org.apache.taglibs.standard.tag.common.core.SetSupport;
22
import org.apache.taglibs.standard.tag.common.core.SetSupport;
24
23
25
/**
24
/**
26
 * <p>A handler for &lt;set&gt;, which redirects the browser to a
25
 * JSTL 1.0 compatible version of &lt;set&gt; that accepts expression text for attribute values.
27
 * new URL.
28
 *
26
 *
29
 * @author Shawn Bayern
27
 * @author Shawn Bayern
30
 */
28
 */
31
29
32
public class SetTag extends SetSupport {
30
public class SetTag extends SetSupport {
33
31
34
    //*********************************************************************
32
    private boolean valueSpecified;
35
    // 'Private' state (implementation details)
33
    private ValueExpression valueExpression;
34
    private ValueExpression targetExpression;
35
    private ValueExpression propertyExpression;
36
36
37
    private String value_;			// stores EL-based property
38
    private String target_;			// stores EL-based property
39
    private String property_;			// stores EL-based property
40
41
42
    //*********************************************************************
43
    // Constructor
44
45
    public SetTag() {
37
    public SetTag() {
46
        super();
47
        init();
48
    }
38
    }
49
39
50
40
    @Override
51
    //*********************************************************************
52
    // Tag logic
53
54
    // evaluates expression and chains to parent
55
    public int doStartTag() throws JspException {
56
57
        // evaluate any expressions we were passed, once per invocation
58
        evaluateExpressions();
59
60
	// chain to the parent implementation
61
	return super.doStartTag();
62
    }
63
64
65
    // Releases any resources we may have (or inherit)
66
    public void release() {
41
    public void release() {
42
        valueExpression = null;
43
        targetExpression = null;
44
        propertyExpression = null;
45
        valueSpecified = false;
67
        super.release();
46
        super.release();
68
        init();
69
    }
47
    }
70
48
49
    public void setValue(String value) {
50
        valueExpression = createExpression(value, Object.class);
51
        valueSpecified = true;
52
    }
71
53
72
    //*********************************************************************
54
    public void setTarget(String target) {
73
    // Accessor methods
55
        targetExpression = createExpression(target, Object.class);
56
    }
74
57
75
    public void setValue(String value_) {
58
    public void setProperty(String property) {
76
        this.value_ = value_;
59
        propertyExpression = createExpression(property, String.class);
77
	this.valueSpecified = true;
78
    }
60
    }
79
61
80
    public void setTarget(String target_) {
62
    private ValueExpression createExpression(String property, Class<?> type) {
81
        this.target_ = target_;
63
        return getExpressionFactory().createValueExpression(pageContext.getELContext(), property, type);
82
    }
64
    }
83
65
84
    public void setProperty(String property_) {
66
    @Override
85
        this.property_ = property_;
67
    protected boolean isValueSpecified() {
68
        return valueSpecified;
86
    }
69
    }
87
70
71
    @Override
72
    protected Object evalValue() {
73
        return valueExpression.getValue(pageContext.getELContext());
74
    }
88
75
89
    //*********************************************************************
76
    @Override
90
    // Private (utility) methods
77
    protected Object evalTarget() {
91
78
        return targetExpression.getValue(pageContext.getELContext());
92
    // (re)initializes state (during release() or construction)
93
    private void init() {
94
        // null implies "no expression"
95
	value_ = target_ = property_ = null;
96
    }
79
    }
97
80
98
    /* Evaluates expressions as necessary */
81
    @Override
99
    private void evaluateExpressions() throws JspException {
82
    protected String evalProperty() {
100
        /* 
83
        return (String) propertyExpression.getValue(pageContext.getELContext());
101
         * Note: we don't check for type mismatches here; we assume
102
         * the expression evaluator will return the expected type
103
         * (by virtue of knowledge we give it about what that type is).
104
         * A ClassCastException here is truly unexpected, so we let it
105
         * propagate up.
106
         */
107
108
	// 'value'
109
	try {
110
	    value = ExpressionUtil.evalNotNull(
111
	        "set", "value", value_, Object.class, this, pageContext);
112
	} catch (NullAttributeException ex) {
113
	    // explicitly let 'value' be null
114
	    value = null;
115
	}
116
117
	// 'target'
118
	target = ExpressionUtil.evalNotNull(
119
	    "set", "target", target_, Object.class, this, pageContext);
120
121
	// 'property'
122
	try {
123
	    property = (String) ExpressionUtil.evalNotNull(
124
	         "set", "property", property_, String.class, this, pageContext);
125
        } catch (NullAttributeException ex) {
126
            // explicitly let 'property' be null
127
            property = null;
128
        }
129
    }
84
    }
130
}
85
}
(-)src/main/java/org/apache/taglibs/standard/tag/rt/core/SetTag.java (-6 / +36 lines)
Lines 20-48 Link Here
20
import org.apache.taglibs.standard.tag.common.core.SetSupport;
20
import org.apache.taglibs.standard.tag.common.core.SetSupport;
21
21
22
/**
22
/**
23
 * <p>Tag handler for &lt;set&gt; in JSTL's rtexprvalue library.</p>
23
 * JSTL 1.1 compatible version of &lt;set&gt; that accepts expression results for attribute values.
24
 *
24
 *
25
 * @author Shawn Bayern
25
 * @author Shawn Bayern
26
 */
26
 */
27
27
28
public class SetTag extends SetSupport {
28
public class SetTag extends SetSupport {
29
    private boolean valueSpecified;
30
    private Object value;
31
    private Object target;
32
    private String property;
29
33
30
    //*********************************************************************
34
    public SetTag() {
31
    // Accessors
35
    }
32
36
33
    // for tag attribute
34
    public void setValue(Object value) {
37
    public void setValue(Object value) {
35
        this.value = value;
38
        this.value = value;
36
        this.valueSpecified = true;
39
        this.valueSpecified = true;
37
    }
40
    }
38
41
39
    // for tag attribute
40
    public void setTarget(Object target) {
42
    public void setTarget(Object target) {
41
        this.target = target;
43
        this.target = target;
42
    }
44
    }
43
45
44
    // for tag attribute
45
    public void setProperty(String property) {
46
    public void setProperty(String property) {
46
        this.property = property;
47
        this.property = property;
47
    }
48
    }
49
50
    @Override
51
    public void release() {
52
        value = null;
53
        target = null;
54
        property = null;
55
        valueSpecified = false;
56
        super.release();
57
    }
58
59
    @Override
60
    protected boolean isValueSpecified() {
61
        return valueSpecified;
62
    }
63
64
    @Override
65
    protected Object evalValue() {
66
        return value;
67
    }
68
69
    @Override
70
    protected Object evalTarget() {
71
        return target;
72
    }
73
74
    @Override
75
    protected String evalProperty() {
76
        return property;
77
    }
48
}
78
}
(-)src/main/java/org/apache/taglibs/standard/tag/common/core/SetSupport.java (-36 / +61 lines)
Lines 26-31 Link Here
26
26
27
import java.util.Map;
27
import java.util.Map;
28
28
29
import javax.servlet.jsp.JspApplicationContext;
29
import javax.servlet.jsp.JspException;
30
import javax.servlet.jsp.JspException;
30
import javax.servlet.jsp.JspFactory;
31
import javax.servlet.jsp.JspFactory;
31
import javax.servlet.jsp.JspTagException;
32
import javax.servlet.jsp.JspTagException;
Lines 44-65 Link Here
44
/**
45
/**
45
 * <p>Support for handlers of the &lt;set&gt; tag.</p>
46
 * <p>Support for handlers of the &lt;set&gt; tag.</p>
46
 *
47
 *
47
 * <p>The protected <code>value</code> and <code>valueSpecified</code>
48
 * attributes must be set in sync. That is, if you set the value then 
49
 * you should set <code>valueSpecified</code> to <code>true<code>, if you unset the value, then 
50
 * you should set <code>valueSpecified</code> to <code>false<code>. </p>
51
 *
52
 * @author Shawn Bayern
48
 * @author Shawn Bayern
53
 */
49
 */
54
public class SetSupport extends BodyTagSupport {
50
public abstract class SetSupport extends BodyTagSupport {
55
51
56
    //*********************************************************************
52
    //*********************************************************************
57
    // Internal state
53
    // Internal state
58
54
59
    protected Object value;             // tag attribute
60
    protected boolean valueSpecified;   // status
61
    protected Object target;            // tag attribute
62
    protected String property;          // tag attribute
63
    private String var;                 // tag attribute
55
    private String var;                 // tag attribute
64
    private String scope;               // tag attribute
56
    private String scope;               // tag attribute
65
57
Lines 71-90 Link Here
71
     * not provide other constructors and are expected to call the
63
     * not provide other constructors and are expected to call the
72
     * superclass constructor.
64
     * superclass constructor.
73
     */
65
     */
74
    public SetSupport() {
66
    protected SetSupport() {
75
        super();
67
        super();
76
        init();
77
    }
68
    }
78
69
79
    // resets local state
80
    private void init() {
81
        value = target = property = var = scope = null;
82
        valueSpecified = false;
83
    }
84
85
    // Releases any resources we may have (or inherit)
70
    // Releases any resources we may have (or inherit)
86
    public void release() {
71
    public void release() {
87
        init();
72
        var = null;
73
        scope = null;
88
        super.release();
74
        super.release();
89
    }
75
    }
90
76
Lines 94-120 Link Here
94
80
95
    public int doEndTag() throws JspException {
81
    public int doEndTag() throws JspException {
96
82
97
        // what we'll store in scope:var
98
        Object result = getResult();
99
100
        // decide what to do with the result
83
        // decide what to do with the result
101
        if (var != null) {
84
        if (var != null) {
102
            exportToVariable(result);
85
            exportToVariable(getResult());
103
        } else if (target == null) {
104
            // can happen if target evaluates to null
105
            throw new JspTagException(Resources.getMessage("SET_INVALID_TARGET"));
106
        } else if (target instanceof Map) {
107
            exportToMapProperty(result);
108
        } else {
86
        } else {
109
            exportToBeanProperty(result);
87
            Object target = evalTarget();
88
            if (target == null) {
89
                // can happen if target evaluates to null
90
                throw new JspTagException(Resources.getMessage("SET_INVALID_TARGET"));
91
            }
92
93
            String property = evalProperty();
94
            if (target instanceof Map) {
95
                exportToMapProperty(target, property, getResult());
96
            } else {
97
                exportToBeanProperty(target, property, getResult());
98
            }
110
        }
99
        }
111
100
112
        return EVAL_PAGE;
101
        return EVAL_PAGE;
113
    }
102
    }
114
103
115
    Object getResult() {
104
    Object getResult() {
116
        if (valueSpecified) {
105
        if (isValueSpecified()) {
117
            return value;
106
            return evalValue();
118
        } else if (bodyContent == null) {
107
        } else if (bodyContent == null) {
119
            return "";
108
            return "";
120
        } else {
109
        } else {
Lines 128-133 Link Here
128
    }
117
    }
129
118
130
    /**
119
    /**
120
     * Indicates that the value attribute was specified.
121
     * If no value attribute is supplied then the value is taken from the tag's body content.
122
     *
123
     * @return true if the value attribute was specified
124
     */
125
    protected abstract boolean isValueSpecified();
126
127
    /**
128
     * Evaluate the value attribute.
129
     *
130
     * @return the result of evaluating the value attribute
131
     */
132
    protected abstract Object evalValue();
133
134
    /**
135
     * Evaluate the target attribute.
136
     *
137
     * @return the result of evaluating the target attribute
138
     */
139
    protected abstract Object evalTarget();
140
141
    /**
142
     * Evaluate the property attribute.
143
     *
144
     * @return the result of evaluating the property attribute
145
     */
146
    protected abstract String evalProperty();
147
148
    /**
131
     * Export the result into a scoped variable.
149
     * Export the result into a scoped variable.
132
     *
150
     *
133
     * @param result the value to export
151
     * @param result the value to export
Lines 173-181 Link Here
173
    /**
191
    /**
174
     * Export the result into a Map.
192
     * Export the result into a Map.
175
     *
193
     *
194
     * @param target the Map to export into
195
     * @param property the key to export into
176
     * @param result the value to export
196
     * @param result the value to export
177
     */
197
     */
178
    void exportToMapProperty(Object result) {
198
    void exportToMapProperty(Object target, String property, Object result) {
179
        @SuppressWarnings("unchecked")
199
        @SuppressWarnings("unchecked")
180
        Map<Object, Object> map = (Map<Object, Object>) target;
200
        Map<Object, Object> map = (Map<Object, Object>) target;
181
        if (result == null) {
201
        if (result == null) {
Lines 188-197 Link Here
188
    /**
208
    /**
189
     * Export the result into a bean property.
209
     * Export the result into a bean property.
190
     *
210
     *
211
     * @param target the bean to export into
212
     * @param property the bean property to set
191
     * @param result the value to export
213
     * @param result the value to export
192
     * @throws JspTagException if there was a problem exporting the result
214
     * @throws JspTagException if there was a problem exporting the result
193
     */
215
     */
194
    void exportToBeanProperty(Object result) throws JspTagException {
216
    void exportToBeanProperty(Object target, String property, Object result) throws JspTagException {
195
        PropertyDescriptor[] descriptors;
217
        PropertyDescriptor[] descriptors;
196
        try {
218
        try {
197
            descriptors = Introspector.getBeanInfo(target.getClass()).getPropertyDescriptors();
219
            descriptors = Introspector.getBeanInfo(target.getClass()).getPropertyDescriptors();
Lines 234-244 Link Here
234
            return null;
256
            return null;
235
        }
257
        }
236
        Class<?> expectedType = m.getParameterTypes()[0];
258
        Class<?> expectedType = m.getParameterTypes()[0];
237
        JspFactory jspFactory = JspFactory.getDefaultFactory();
259
        return getExpressionFactory().coerceToType(value, expectedType);
238
        ExpressionFactory expressionFactory = jspFactory.getJspApplicationContext(pageContext.getServletContext()).getExpressionFactory();
239
        return expressionFactory.coerceToType(value, expectedType);
240
    }
260
    }
241
261
262
    protected ExpressionFactory getExpressionFactory() {
263
        JspApplicationContext appContext = JspFactory.getDefaultFactory().getJspApplicationContext(pageContext.getServletContext());
264
        return appContext.getExpressionFactory();
265
    }
266
242
    //*********************************************************************
267
    //*********************************************************************
243
    // Accessor methods
268
    // Accessor methods
244
269

Return to bug 33934