1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.view.facelets.tag.jstl.core;
21
22 import java.io.IOException;
23 import javax.el.ELContext;
24 import javax.el.ELException;
25 import javax.el.ValueExpression;
26 import javax.faces.FacesException;
27 import javax.faces.component.UIComponent;
28 import javax.faces.view.facelets.FaceletContext;
29 import javax.faces.view.facelets.FaceletException;
30 import javax.faces.view.facelets.TagAttribute;
31 import javax.faces.view.facelets.TagConfig;
32 import javax.faces.view.facelets.TagException;
33 import javax.faces.view.facelets.TagHandler;
34 import org.apache.myfaces.view.facelets.AbstractFaceletContext;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class LegacySetHandler extends TagHandler
51 {
52
53
54
55
56
57
58
59 private final TagAttribute var;
60
61
62
63
64
65
66
67 private final TagAttribute value;
68
69
70
71
72
73 private final TagAttribute scope;
74
75
76
77
78
79
80
81 private final TagAttribute target;
82
83
84
85
86
87 private final TagAttribute property;
88
89 public LegacySetHandler(TagConfig config)
90 {
91 super(config);
92 this.value = this.getAttribute("value");
93 this.var = this.getAttribute("var");
94 this.scope = this.getAttribute("scope");
95 this.target = this.getAttribute("target");
96 this.property = this.getAttribute("property");
97 }
98
99 public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
100 ELException
101 {
102 ValueExpression veObj = this.value.getValueExpression(ctx, Object.class);
103
104 if (this.var != null)
105 {
106
107 String varStr = this.var.getValue(ctx);
108
109 if (this.scope != null)
110 {
111 String scopeStr = this.scope.getValue(ctx);
112
113
114 if (scopeStr == null || scopeStr.length() == 0)
115 {
116 throw new TagException(tag, "scope must not be empty");
117 }
118 if ("page".equals(scopeStr))
119 {
120 throw new TagException(tag, "page scope is not allowed");
121 }
122
123
124 StringBuilder expStr = new StringBuilder().append("#{").append(scopeStr);
125 if ("request".equals(scopeStr) || "view".equals(scopeStr) || "session".equals(scopeStr)
126 || "application".equals(scopeStr))
127 {
128 expStr.append("Scope");
129 }
130 expStr.append(".").append(varStr).append("}");
131 ELContext elCtx = ctx.getFacesContext().getELContext();
132 ValueExpression expr = ctx.getExpressionFactory().createValueExpression(
133 elCtx, expStr.toString(), Object.class);
134 expr.setValue(elCtx, veObj.getValue(elCtx));
135 }
136 else
137 {
138 ctx.getVariableMapper().setVariable(varStr, veObj);
139 AbstractFaceletContext actx = ((AbstractFaceletContext) ctx);
140 actx.getPageContext().setAllowCacheELExpressions(false);
141 }
142 }
143 else
144 {
145
146 if (this.target == null || this.property == null || this.value == null)
147 {
148 throw new TagException(
149 tag, "either attributes var and value or target, property and value must be set");
150 }
151 if (this.target.isLiteral())
152 {
153 throw new TagException(tag, "attribute target must contain a value expression");
154 }
155
156
157 ELContext elCtx = ctx.getFacesContext().getELContext();
158 ValueExpression targetExpr = this.target.getValueExpression(ctx, Object.class);
159 Object targetObj = targetExpr.getValue(elCtx);
160 String propertyName = this.property.getValue(ctx);
161
162 ctx.getELResolver().setValue(elCtx, targetObj, propertyName, veObj.getValue(elCtx));
163 }
164 }
165 }