Skip to content

Commit a497b0a

Browse files
committed
Added abstraction layer for EL expressions
1 parent 5c079ee commit a497b0a

3 files changed

Lines changed: 100 additions & 18 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.el;
17+
18+
/**
19+
* Implementation of {@link Expression} used when the EL expression is known in advance.
20+
*
21+
* @author Christian Kaltepoth
22+
*/
23+
class ConstantExpression implements Expression
24+
{
25+
26+
private final String expression;
27+
28+
/**
29+
* Creates a new {@link ConstantExpression}.
30+
*/
31+
public ConstantExpression(String expression)
32+
{
33+
this.expression = expression;
34+
}
35+
36+
@Override
37+
public String getExpression()
38+
{
39+
return expression;
40+
}
41+
42+
@Override
43+
public String toString()
44+
{
45+
return expression;
46+
}
47+
48+
}

‎api-el/src/main/java/org/ocpsoft/rewrite/el/El.java‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ public abstract class El extends BindingBuilder<El, Object> implements Retrieval
4646
*/
4747
public static El methodBinding(final String retrieve, final String submit)
4848
{
49-
return new ElMethod(retrieve, submit);
49+
return new ElMethod(new ConstantExpression(retrieve), new ConstantExpression(submit));
5050
}
5151

5252
/**
5353
* Create a new EL Method binding to retrieve values. The method must return a value of the expected type.
5454
*/
5555
public static El retrievalMethod(final String expression)
5656
{
57-
return new ElMethod(expression, null);
57+
return new ElMethod(new ConstantExpression(expression), null);
5858
}
5959

6060
/**
@@ -64,7 +64,7 @@ public static El retrievalMethod(final String expression)
6464
public static El retrievalMethod(final String expression,
6565
final Class<? extends Converter<?>> converterType)
6666
{
67-
ElMethod el = new ElMethod(expression, null);
67+
ElMethod el = new ElMethod(new ConstantExpression(expression), null);
6868
el.convertedBy(converterType);
6969
return el;
7070
}
@@ -74,7 +74,7 @@ public static El retrievalMethod(final String expression,
7474
*/
7575
public static El submissionMethod(final String expression)
7676
{
77-
return new ElMethod(null, expression);
77+
return new ElMethod(null, new ConstantExpression(expression));
7878
}
7979

8080
/**
@@ -84,7 +84,7 @@ public static El submissionMethod(final String expression)
8484
public static El submissionMethod(final String expression,
8585
final Class<? extends Converter<?>> converterType)
8686
{
87-
ElMethod el = new ElMethod(null, expression);
87+
ElMethod el = new ElMethod(null, new ConstantExpression(expression));
8888
el.convertedBy(converterType);
8989
return el;
9090
}
@@ -98,7 +98,7 @@ public static El submissionMethod(final String expression,
9898
final Class<? extends Converter<?>> converterType,
9999
final Class<? extends Validator<?>> validatorType)
100100
{
101-
ElMethod el = new ElMethod(null, expression);
101+
ElMethod el = new ElMethod(null, new ConstantExpression(expression));
102102
el.convertedBy(converterType);
103103
el.validatedBy(validatorType);
104104
return el;
@@ -110,7 +110,7 @@ public static El submissionMethod(final String expression,
110110
*/
111111
public static El property(final String expression)
112112
{
113-
return new ElProperty(expression);
113+
return new ElProperty(new ConstantExpression(expression));
114114
}
115115

116116
/**
@@ -120,7 +120,7 @@ public static El property(final String expression)
120120
*/
121121
public static El property(final String expression, final Class<? extends Converter<?>> type)
122122
{
123-
ElProperty el = new ElProperty(expression);
123+
ElProperty el = new ElProperty(new ConstantExpression(expression));
124124
el.convertedBy(type);
125125
return el;
126126
}
@@ -133,7 +133,7 @@ public static El property(final String expression, final Class<? extends Convert
133133
public static El property(final String expression, final Class<? extends Converter<?>> converterType,
134134
final Class<? extends Validator<?>> validatorType)
135135
{
136-
ElProperty el = new ElProperty(expression);
136+
ElProperty el = new ElProperty(new ConstantExpression(expression));
137137
el.convertedBy(converterType);
138138
el.validatedBy(validatorType);
139139
return el;
@@ -156,10 +156,10 @@ private static List<ExpressionLanguageProvider> getProviders()
156156
*/
157157
public static class ElMethod extends El
158158
{
159-
private final String getExpression;
160-
private final String setExpression;
159+
private final Expression getExpression;
160+
private final Expression setExpression;
161161

162-
public ElMethod(final String getExpression, final String setExpression)
162+
public ElMethod(final Expression getExpression, final Expression setExpression)
163163
{
164164
this.getExpression = getExpression;
165165
this.setExpression = setExpression;
@@ -180,7 +180,7 @@ public Object retrieve(final Rewrite event, final EvaluationContext context)
180180

181181
try
182182
{
183-
return provider.evaluateMethodExpression(getExpression);
183+
return provider.evaluateMethodExpression(getExpression.getExpression());
184184
}
185185
catch (UnsupportedEvaluationException e) {
186186
log.debug("El provider [" + provider.getClass().getName()
@@ -209,7 +209,7 @@ public Object submit(final Rewrite event, final EvaluationContext context, final
209209
for (ExpressionLanguageProvider provider : getProviders()) {
210210
try
211211
{
212-
return provider.evaluateMethodExpression(setExpression, value);
212+
return provider.evaluateMethodExpression(setExpression.getExpression(), value);
213213
}
214214
catch (UnsupportedEvaluationException e) {
215215
log.debug("El provider [" + provider.getClass().getName()
@@ -252,9 +252,9 @@ public String toString()
252252
*/
253253
public static class ElProperty extends El
254254
{
255-
private final String expression;
255+
private final Expression expression;
256256

257-
public ElProperty(final String expression)
257+
public ElProperty(final Expression expression)
258258
{
259259
this.expression = expression;
260260
}
@@ -270,7 +270,7 @@ public Object retrieve(final Rewrite event, final EvaluationContext context)
270270

271271
try
272272
{
273-
value = provider.retrieveValue(expression);
273+
value = provider.retrieveValue(expression.getExpression());
274274
break;
275275
}
276276
catch (UnsupportedEvaluationException e)
@@ -321,7 +321,7 @@ public Object submit(final Rewrite event, final EvaluationContext context, final
321321
for (ExpressionLanguageProvider provider : providers) {
322322
try
323323
{
324-
provider.submitValue(expression, value);
324+
provider.submitValue(expression.getExpression(), value);
325325
break;
326326
}
327327
catch (UnsupportedEvaluationException e)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.el;
17+
18+
/**
19+
*
20+
* Interface representing a single EL method or value binding expression.
21+
*
22+
* @author Christian Kaltepoth
23+
*
24+
*/
25+
interface Expression
26+
{
27+
/**
28+
* Returns the string representation of the expression.
29+
*
30+
* @return The string representation.
31+
*/
32+
String getExpression();
33+
34+
}

0 commit comments

Comments
 (0)