1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.shale.test.mock; |
19 | |
|
20 | |
import java.util.ArrayList; |
21 | |
import java.util.HashMap; |
22 | |
import java.util.Iterator; |
23 | |
import java.util.List; |
24 | |
import java.util.Locale; |
25 | |
import java.util.Map; |
26 | |
import java.util.ResourceBundle; |
27 | |
import javax.el.ArrayELResolver; |
28 | |
import javax.el.BeanELResolver; |
29 | |
import javax.el.CompositeELResolver; |
30 | |
import javax.el.ELContextListener; |
31 | |
import javax.el.ELException; |
32 | |
import javax.el.ELResolver; |
33 | |
import javax.el.ExpressionFactory; |
34 | |
import javax.el.ListELResolver; |
35 | |
import javax.el.MapELResolver; |
36 | |
import javax.el.ResourceBundleELResolver; |
37 | |
import javax.el.ValueExpression; |
38 | |
import javax.faces.FacesException; |
39 | |
import javax.faces.component.UIComponent; |
40 | |
import javax.faces.component.UIViewRoot; |
41 | |
import javax.faces.context.FacesContext; |
42 | |
import org.apache.shale.test.el.FacesImplicitObjectELResolver; |
43 | |
import org.apache.shale.test.el.FacesPropertyResolverChainWrapper; |
44 | |
import org.apache.shale.test.el.FacesResourceBundleELResolver; |
45 | |
import org.apache.shale.test.el.FacesScopedAttributeELResolver; |
46 | |
import org.apache.shale.test.el.FacesVariableResolverChainWrapper; |
47 | |
import org.apache.shale.test.el.MockExpressionFactory; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public class MockApplication12 extends MockApplication { |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public MockApplication12() { |
67 | |
|
68 | 18 | super(); |
69 | |
|
70 | |
|
71 | 18 | expressionFactory = new MockExpressionFactory(); |
72 | |
|
73 | 18 | } |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | 18 | private Map bundles = new HashMap(); |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | 18 | private List elContextListeners = new ArrayList(); |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | 18 | private ExpressionFactory expressionFactory = null; |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | 18 | private ELResolver resolver = null; |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | 18 | private List resolvers = new ArrayList(); |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
public void addResourceBundle(String name, ResourceBundle bundle) { |
121 | |
bundles.put(name, bundle); |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public Map getResourceBundles() { |
130 | |
return bundles; |
131 | |
} |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public void addELContextListener(ELContextListener listener) { |
139 | |
|
140 | |
elContextListeners.add(listener); |
141 | |
|
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
public void addELResolver(ELResolver resolver) { |
147 | |
|
148 | |
|
149 | |
|
150 | |
if (resolver != null) { |
151 | |
throw new IllegalStateException("Cannot add resolvers now"); |
152 | |
} |
153 | |
|
154 | |
resolvers.add(resolver); |
155 | |
|
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
public UIComponent createComponent(ValueExpression expression, |
161 | |
FacesContext context, |
162 | |
String componentType) { |
163 | |
|
164 | |
UIComponent component = null; |
165 | |
try { |
166 | |
component = (UIComponent) expression.getValue(context.getELContext()); |
167 | |
if (component == null) { |
168 | |
component = createComponent(componentType); |
169 | |
expression.setValue(context.getELContext(), component); |
170 | |
} |
171 | |
|
172 | |
} catch (Exception e) { |
173 | |
throw new FacesException(e); |
174 | |
} |
175 | |
return component; |
176 | |
|
177 | |
} |
178 | |
|
179 | |
|
180 | |
|
181 | |
public Object evaluateExpressionGet(FacesContext context, |
182 | |
String expression, |
183 | |
Class expectedType) throws ELException { |
184 | |
|
185 | |
ValueExpression ve = getExpressionFactory().createValueExpression |
186 | |
(context.getELContext(), expression, expectedType); |
187 | |
return ve.getValue(context.getELContext()); |
188 | |
|
189 | |
} |
190 | |
|
191 | |
|
192 | |
|
193 | |
public ELContextListener[] getELContextListeners() { |
194 | |
|
195 | 4 | return (ELContextListener[]) |
196 | |
elContextListeners.toArray(new ELContextListener[elContextListeners.size()]); |
197 | |
|
198 | |
} |
199 | |
|
200 | |
|
201 | |
|
202 | |
public ELResolver getELResolver() { |
203 | |
|
204 | 1 | if (resolver == null) { |
205 | |
|
206 | |
|
207 | 1 | CompositeELResolver composite = new CompositeELResolver(); |
208 | |
|
209 | 1 | composite.add(new FacesImplicitObjectELResolver()); |
210 | |
|
211 | 1 | CompositeELResolver nested = new CompositeELResolver(); |
212 | |
|
213 | 1 | nested.add(new FacesVariableResolverChainWrapper()); |
214 | 1 | nested.add(new FacesPropertyResolverChainWrapper()); |
215 | 1 | Iterator items = resolvers.iterator(); |
216 | 1 | while (items.hasNext()) { |
217 | |
nested.add((ELResolver) items.next()); |
218 | |
} |
219 | 1 | composite.add(nested); |
220 | |
|
221 | |
|
222 | 1 | composite.add(new ResourceBundleELResolver()); |
223 | 1 | composite.add(new FacesResourceBundleELResolver()); |
224 | 1 | composite.add(new MapELResolver()); |
225 | 1 | composite.add(new ListELResolver()); |
226 | 1 | composite.add(new ArrayELResolver()); |
227 | 1 | composite.add(new BeanELResolver()); |
228 | 1 | composite.add(new FacesScopedAttributeELResolver()); |
229 | |
|
230 | |
|
231 | 1 | resolver = composite; |
232 | |
|
233 | |
} |
234 | 1 | return resolver; |
235 | |
|
236 | |
} |
237 | |
|
238 | |
|
239 | |
|
240 | |
public ExpressionFactory getExpressionFactory() { |
241 | |
|
242 | 14 | return this.expressionFactory; |
243 | |
|
244 | |
} |
245 | |
|
246 | |
|
247 | |
|
248 | |
public ResourceBundle getResourceBundle(FacesContext context, String name) { |
249 | |
|
250 | |
if ((context == null) || (name == null)) { |
251 | |
throw new NullPointerException(); |
252 | |
} |
253 | |
Locale locale = null; |
254 | |
UIViewRoot viewRoot = context.getViewRoot(); |
255 | |
if (viewRoot != null) { |
256 | |
locale = viewRoot.getLocale(); |
257 | |
} |
258 | |
if (locale == null) { |
259 | |
locale = Locale.getDefault(); |
260 | |
} |
261 | |
return ResourceBundle.getBundle(name, locale); |
262 | |
|
263 | |
} |
264 | |
|
265 | |
|
266 | |
|
267 | |
public void removeELContextListener(ELContextListener listener) { |
268 | |
|
269 | |
elContextListeners.remove(listener); |
270 | |
|
271 | |
} |
272 | |
|
273 | |
|
274 | |
} |