01: /*
02: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 de.odysseus.calyxo.base.misc;
17:
18: import javax.servlet.http.HttpServletRequest;
19: import javax.servlet.jsp.el.ELException;
20: import javax.servlet.jsp.el.ExpressionEvaluator;
21: import javax.servlet.jsp.el.VariableResolver;
22:
23: import de.odysseus.calyxo.base.ModuleContext;
24: import de.odysseus.calyxo.base.access.Accessor;
25: import de.odysseus.calyxo.base.util.MapFacade;
26:
27: /**
28: * This accessor evaluates an el expression passed as a string.
29: * It uses a {@link de.odysseus.calyxo.base.misc.CalyxoVariableResolver}
30: * to resolve implicite objects and to search scopes.
31: *
32: * @author Christoph Beck
33: */
34: public class EvalAccessor implements Accessor {
35: private static class EvalMap extends MapFacade {
36: private ExpressionEvaluator evaluator;
37: private VariableResolver variables;
38:
39: EvalMap(ExpressionEvaluator evaluator,
40: VariableResolver variables) {
41: this .evaluator = evaluator;
42: this .variables = variables;
43: }
44:
45: /* (non-Javadoc)
46: * @see java.util.Map#get(java.lang.Object)
47: */
48: public Object get(Object key) {
49: if (key == null) {
50: return null;
51: }
52: StringBuffer expr = new StringBuffer();
53: expr.append("${");
54: expr.append(key.toString());
55: expr.append("}");
56: try {
57: return evaluator.evaluate(expr.toString(),
58: Object.class, variables, null);
59: } catch (ELException e) {
60: return null;
61: }
62: }
63: }
64:
65: private final ModuleContext context;
66:
67: /**
68: * Constructor.
69: */
70: public EvalAccessor(ModuleContext context) {
71: this .context = context;
72: }
73:
74: /* (non-Javadoc)
75: * @see de.odysseus.calyxo.base.access.Accessor#get(javax.servlet.http.HttpServletRequest)
76: */
77: public Object get(HttpServletRequest request) {
78: return new EvalMap(context.getExpressionEvaluator(),
79: new CalyxoVariableResolver(request, context));
80: }
81:
82: /* (non-Javadoc)
83: * @see de.odysseus.calyxo.base.access.Accessor#isCacheable()
84: */
85: public boolean isCacheable() {
86: return true;
87: }
88: }
|