1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.teeda.core.el.impl; |
17 | |
|
18 | |
import java.util.Map; |
19 | |
|
20 | |
import javax.faces.application.Application; |
21 | |
import javax.faces.context.FacesContext; |
22 | |
import javax.faces.el.EvaluationException; |
23 | |
import javax.faces.el.PropertyNotFoundException; |
24 | |
import javax.faces.el.PropertyResolver; |
25 | |
import javax.faces.el.ReferenceSyntaxException; |
26 | |
import javax.faces.el.VariableResolver; |
27 | |
|
28 | |
import org.seasar.framework.util.AssertionUtil; |
29 | |
import org.seasar.teeda.core.el.ELParser; |
30 | |
import org.seasar.teeda.core.el.ExpressionProcessor; |
31 | |
import org.seasar.teeda.core.el.ValueBindingBase; |
32 | |
import org.seasar.teeda.core.managedbean.ManagedBeanFactory; |
33 | |
import org.seasar.teeda.core.managedbean.ManagedBeanScopeSaver; |
34 | |
import org.seasar.teeda.core.scope.Scope; |
35 | |
import org.seasar.teeda.core.util.PropertyResolverUtil; |
36 | |
import org.seasar.teeda.core.util.VariableResolverUtil; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public class ValueBindingImpl extends ValueBindingBase { |
43 | |
|
44 | |
private Application application; |
45 | |
|
46 | |
private String expressionString; |
47 | |
|
48 | |
private ELParser parser; |
49 | |
|
50 | |
private Object expression; |
51 | |
|
52 | 158 | private boolean transientValue = false; |
53 | |
|
54 | 47 | public ValueBindingImpl() { |
55 | 47 | } |
56 | |
|
57 | |
public ValueBindingImpl(Application application, String expressionStr, |
58 | 111 | ELParser p) { |
59 | 111 | this.application = application; |
60 | 111 | this.expressionString = expressionStr; |
61 | 111 | this.parser = p; |
62 | 111 | this.expression = p.parse(this.expressionString); |
63 | 111 | } |
64 | |
|
65 | |
public Object getValue(FacesContext context) throws EvaluationException, |
66 | |
PropertyNotFoundException { |
67 | |
try { |
68 | 73 | return parser.getExpressionProcessor() |
69 | |
.evaluate(context, expression); |
70 | 0 | } catch (IndexOutOfBoundsException e) { |
71 | 0 | throw new PropertyNotFoundException(e); |
72 | 0 | } catch (EvaluationException e) { |
73 | 0 | throw e; |
74 | |
} |
75 | |
} |
76 | |
|
77 | |
public void setValue(FacesContext context, Object newValue) |
78 | |
throws EvaluationException, PropertyNotFoundException { |
79 | 7 | AssertionUtil.assertNotNull("context is null.", context); |
80 | |
try { |
81 | 6 | ExpressionProcessor processor = parser.getExpressionProcessor(); |
82 | 6 | Object obj = processor.resolveBase(context, expression); |
83 | 6 | if (obj == null) { |
84 | 0 | throw new EvaluationException(); |
85 | |
} |
86 | 6 | if (obj instanceof String) { |
87 | 2 | String name = (String) obj; |
88 | 2 | if (isImplicitObject(name)) { |
89 | 0 | throw new ReferenceSyntaxException( |
90 | |
"Prohibited to set to implicit object."); |
91 | |
} |
92 | 2 | setValueInScope(context, name, newValue); |
93 | |
} else { |
94 | 4 | Object[] bases = (Object[]) obj; |
95 | 4 | Object base = bases[0]; |
96 | 4 | Object property = bases[1]; |
97 | 4 | Integer index = parser.getExpressionProcessor().toIndex(base, |
98 | |
property); |
99 | 4 | PropertyResolver resolver = application.getPropertyResolver(); |
100 | 4 | if (index == null) { |
101 | 3 | resolver.setValue(base, property, newValue); |
102 | |
} else { |
103 | 1 | resolver.setValue(base, index.intValue(), newValue); |
104 | |
} |
105 | |
} |
106 | 0 | } catch (IndexOutOfBoundsException e) { |
107 | 0 | throw new PropertyNotFoundException(e); |
108 | 6 | } |
109 | 6 | } |
110 | |
|
111 | |
protected void setValueInScope(FacesContext context, String name, |
112 | |
Object newValue) { |
113 | 2 | VariableResolver resolver = application.getVariableResolver(); |
114 | 2 | ExpressionProcessor processor = parser.getExpressionProcessor(); |
115 | 2 | Map scopeMap = VariableResolverUtil.getDefaultScopeMap(context, |
116 | |
resolver, name); |
117 | 2 | if (scopeMap != null) { |
118 | 0 | Object previous = scopeMap.get(name); |
119 | 0 | if (previous != null) { |
120 | 0 | scopeMap.put(name, processor.getCoercedObject(newValue, |
121 | |
previous.getClass())); |
122 | 0 | return; |
123 | |
} |
124 | |
} |
125 | 2 | ManagedBeanFactory managedBeanFactory = getManagedBeanFactory(); |
126 | 2 | Scope scope = managedBeanFactory.getManagedBeanScope(name); |
127 | 2 | ManagedBeanScopeSaver saver = managedBeanFactory |
128 | |
.getManagedBeanScopeSaver(); |
129 | 2 | if (scope != null) { |
130 | 1 | saver.saveToScope(context, scope, name, newValue); |
131 | 1 | return; |
132 | |
} |
133 | |
|
134 | 1 | saver.saveToScope(context, Scope.REQUEST, name, newValue); |
135 | 1 | } |
136 | |
|
137 | |
public boolean isReadOnly(FacesContext context) throws EvaluationException, |
138 | |
PropertyNotFoundException { |
139 | 13 | Object obj = parser.getExpressionProcessor().resolveBase(context, |
140 | |
expression); |
141 | 13 | if (obj == null) { |
142 | 1 | return true; |
143 | |
} |
144 | 12 | if (obj instanceof String) { |
145 | 10 | return isImplicitObject((String) obj); |
146 | |
} else { |
147 | 2 | Object[] bases = (Object[]) obj; |
148 | 2 | Object base = bases[0]; |
149 | 2 | Object property = bases[1]; |
150 | 2 | Integer index = parser.getExpressionProcessor().toIndex(base, |
151 | |
property); |
152 | 2 | return PropertyResolverUtil.isReadOnly(application, base, property, |
153 | |
index); |
154 | |
} |
155 | |
} |
156 | |
|
157 | |
public Class getType(FacesContext context) throws EvaluationException, |
158 | |
PropertyNotFoundException { |
159 | 5 | Object obj = parser.getExpressionProcessor().resolveBase(context, |
160 | |
expression); |
161 | 5 | if (obj == null) { |
162 | 2 | return getValue(context).getClass(); |
163 | |
} |
164 | 3 | if (obj instanceof String) { |
165 | 2 | String name = (String) obj; |
166 | 2 | ManagedBeanFactory managedBeanFactory = getManagedBeanFactory(); |
167 | 2 | Object managedBean = managedBeanFactory.getManagedBean(name); |
168 | 2 | if (managedBean != null) { |
169 | 1 | return managedBean.getClass(); |
170 | |
} |
171 | 1 | Object value = application.getVariableResolver().resolveVariable( |
172 | |
context, name); |
173 | 1 | return (value != null) ? value.getClass() : Object.class; |
174 | |
} else { |
175 | 1 | Object[] bases = (Object[]) obj; |
176 | 1 | Object base = bases[0]; |
177 | 1 | Object property = bases[1]; |
178 | 1 | Integer index = parser.getExpressionProcessor().toIndex(base, |
179 | |
property); |
180 | 1 | return PropertyResolverUtil.getType(application, base, property, |
181 | |
index); |
182 | |
} |
183 | |
} |
184 | |
|
185 | |
public String getExpressionString() { |
186 | 119 | return expressionString; |
187 | |
} |
188 | |
|
189 | |
public Object getExpression() { |
190 | 10 | return expression; |
191 | |
} |
192 | |
|
193 | |
public boolean isTransient() { |
194 | 43 | return transientValue; |
195 | |
} |
196 | |
|
197 | |
public void setTransient(boolean transientValue) { |
198 | 0 | this.transientValue = transientValue; |
199 | 0 | } |
200 | |
|
201 | |
public Object saveState(FacesContext context) { |
202 | 47 | Object[] state = new Object[2]; |
203 | 47 | state[0] = expressionString; |
204 | 47 | state[1] = parser; |
205 | 47 | return state; |
206 | |
} |
207 | |
|
208 | |
public void restoreState(FacesContext context, Object obj) { |
209 | 47 | Object[] state = (Object[]) obj; |
210 | 47 | application = context.getApplication(); |
211 | 47 | expressionString = (String) state[0]; |
212 | 47 | parser = (ELParser) state[1]; |
213 | 47 | expression = parser.parse(expressionString); |
214 | 47 | } |
215 | |
|
216 | |
} |