1 /*
2 * $Id: ELAttributeEvaluator.java 891884 2009-12-17 20:43:12Z apetrelli $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21 package org.apache.tiles.el;
22
23 import javax.el.ELResolver;
24 import javax.el.ExpressionFactory;
25 import javax.el.ValueExpression;
26
27 import org.apache.tiles.evaluator.AbstractAttributeEvaluator;
28 import org.apache.tiles.request.ApplicationContext;
29 import org.apache.tiles.request.Request;
30
31 /**
32 * Evaluates string expression with typical EL syntax.<br>
33 * You can use normal EL syntax, knowing that the root objects are
34 * {@link Request}, {@link ApplicationContext} and beans
35 * contained in request, session and application scope.
36 *
37 * @version $Rev: 891884 $ $Date: 2009-12-18 07:43:12 +1100 (Fri, 18 Dec 2009) $
38 * @since 2.2.1
39 */
40 public class ELAttributeEvaluator extends AbstractAttributeEvaluator {
41
42 /**
43 * Initialization parameter to decide the implementation of
44 * {@link ExpressionFactoryFactory}.
45 *
46 * @since 2.2.1
47 */
48 public static final String EXPRESSION_FACTORY_FACTORY_INIT_PARAM =
49 "org.apache.tiles.evaluator.el.ExpressionFactoryFactory";
50
51 /**
52 * The EL expression factory.
53 *
54 * @since 2.2.1
55 */
56 protected ExpressionFactory expressionFactory;
57
58 /**
59 * The EL resolver to use.
60 *
61 * @since 2.2.1
62 */
63 protected ELResolver resolver;
64
65 /**
66 * Constructor.
67 *
68 * @since 2.2.1
69 */
70 public ELAttributeEvaluator() {
71 }
72
73 /**
74 * Sets the expression factory to use.
75 *
76 * @param expressionFactory The expression factory.
77 * @since 2.2.1
78 */
79 public void setExpressionFactory(ExpressionFactory expressionFactory) {
80 this.expressionFactory = expressionFactory;
81 }
82
83 /**
84 * Sets the EL resolver to use.
85 *
86 * @param resolver The EL resolver.
87 * @since 2.2.1
88 */
89 public void setResolver(ELResolver resolver) {
90 this.resolver = resolver;
91 }
92
93 /** {@inheritDoc} */
94 public Object evaluate(String expression, Request request) {
95 ELContextImpl context = new ELContextImpl(resolver);
96 context.putContext(Request.class, request);
97 context.putContext(ApplicationContext.class,
98 request.getApplicationContext());
99 ValueExpression valueExpression = expressionFactory
100 .createValueExpression(context, expression, Object.class);
101
102 return valueExpression.getValue(context);
103 }
104 }