Skip to content

Commit 1120717

Browse files
committed
Added implementation of ExpressionLanguageProvider for the JSF module
1 parent 9a03398 commit 1120717

2 files changed

Lines changed: 137 additions & 0 deletions

File tree

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.faces;
17+
18+
import javax.el.ELContext;
19+
import javax.el.ExpressionFactory;
20+
import javax.el.MethodExpression;
21+
import javax.el.ValueExpression;
22+
import javax.faces.context.FacesContext;
23+
24+
import org.ocpsoft.rewrite.exception.UnsupportedEvaluationException;
25+
import org.ocpsoft.rewrite.spi.ExpressionLanguageProvider;
26+
27+
/**
28+
* Implementation of {@link ExpressionLanguageProvider} that uses the {@link FacesContext} to obtain the
29+
* {@link ELContext} and the {@link ExpressionFactory}.
30+
*
31+
* @author Christian Kaltepoth
32+
*/
33+
public class FacesExpressionLanguageProvider implements ExpressionLanguageProvider
34+
{
35+
36+
@Override
37+
public Object retrieveValue(String expression) throws UnsupportedEvaluationException
38+
{
39+
FacesContext facesContext = getFacesContext();
40+
ELContext elContext = facesContext.getELContext();
41+
return getValueExpression(facesContext, expression).getValue(elContext);
42+
}
43+
44+
@Override
45+
public void submitValue(String expression, Object value) throws UnsupportedEvaluationException
46+
{
47+
FacesContext facesContext = getFacesContext();
48+
ELContext elContext = facesContext.getELContext();
49+
50+
ValueExpression valueExpression = getValueExpression(facesContext, expression);
51+
Class<?> referencedType = valueExpression.getType(elContext);
52+
53+
// the value that will be injected
54+
Object toInject = null;
55+
56+
// the expression is referencing an array
57+
if (referencedType.isArray()) {
58+
59+
// ensure the value that will be injected is an array
60+
if (value != null && !value.getClass().isArray()) {
61+
toInject = new Object[] { value };
62+
}
63+
else {
64+
toInject = value;
65+
}
66+
67+
}
68+
69+
// expression is not referencing an array
70+
else {
71+
72+
// if value to inject is an array, just use the first element
73+
if (value != null && value.getClass().isArray()) {
74+
Object[] valueAsArray = (Object[]) value;
75+
if (valueAsArray.length > 0) {
76+
toInject = valueAsArray[0];
77+
}
78+
else {
79+
toInject = null;
80+
}
81+
}
82+
83+
// simple case: neither the expression is referencing an array nor the value is one
84+
else {
85+
toInject = value;
86+
}
87+
88+
}
89+
90+
// set the value
91+
Object coercedValue = facesContext.getApplication().getExpressionFactory().coerceToType(toInject, referencedType);
92+
valueExpression.setValue(elContext, coercedValue);
93+
94+
}
95+
96+
@Override
97+
public Object evaluateMethodExpression(String expression) throws UnsupportedEvaluationException
98+
{
99+
return evaluateMethodExpression(expression, new Object[0]);
100+
}
101+
102+
@Override
103+
public Object evaluateMethodExpression(String expression, Object... values) throws UnsupportedEvaluationException
104+
{
105+
FacesContext facesContext = getFacesContext();
106+
ELContext elContext = facesContext.getELContext();
107+
ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory();
108+
MethodExpression methodExpression = expressionFactory.createMethodExpression(elContext, expression,
109+
Object.class, new Class[values.length]);
110+
return methodExpression.invoke(elContext, values);
111+
}
112+
113+
/**
114+
* Creates an {@link ValueExpression} for the supplied EL expression
115+
*/
116+
private ValueExpression getValueExpression(FacesContext facesContext, String expression)
117+
{
118+
ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory();
119+
return expressionFactory.createValueExpression(facesContext.getELContext(), expression, Object.class);
120+
}
121+
122+
/**
123+
* Obtains the {@link FacesContext} using {@link FacesContext#getCurrentInstance()}. This method will throw an
124+
* {@link IllegalArgumentException} if the {@link FacesContext} is not available.
125+
*/
126+
private FacesContext getFacesContext()
127+
{
128+
FacesContext facesContext = FacesContext.getCurrentInstance();
129+
if (facesContext == null) {
130+
throw new IllegalArgumentException(
131+
"FacesContext.getCurrentInstance() returned null. EL expressions can only be evaluated in the JSF lifecycle. "
132+
+ "You should use PhaseAction and PhaseBinding to perform an deferred operation instead.");
133+
}
134+
return facesContext;
135+
}
136+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.ocpsoft.rewrite.faces.FacesExpressionLanguageProvider

0 commit comments

Comments
 (0)