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.forms.check;
17:
18: import java.util.Locale;
19:
20: import javax.servlet.http.HttpServletRequest;
21: import javax.servlet.jsp.el.ELException;
22: import javax.servlet.jsp.el.Expression;
23: import javax.servlet.jsp.el.VariableResolver;
24:
25: import org.apache.commons.logging.Log;
26: import org.apache.commons.logging.LogFactory;
27:
28: import de.odysseus.calyxo.base.misc.CalyxoVariableResolver;
29: import de.odysseus.calyxo.forms.Checker;
30:
31: /**
32: * EL expression checker.
33: *
34: * @author Christoph Beck
35: * @author Oliver Stuhr
36: */
37: public class ELChecker implements Checker {
38: private static final Log log = LogFactory.getLog(ELChecker.class);
39:
40: private Expression expr;
41:
42: /* (non-Javadoc)
43: * @see de.odysseus.calyxo.forms.Checker#check(javax.servlet.http.HttpServletRequest, java.lang.Object)
44: */
45: public boolean check(HttpServletRequest request, final Object value) {
46: VariableResolver resolver = new CalyxoVariableResolver(request) {
47: public Object resolveVariable(String variable)
48: throws ELException {
49: if ("property".equals(variable)) {
50: return value;
51: }
52: return super .resolveVariable(variable);
53: }
54: };
55: Object result = null;
56: try {
57: result = expr.evaluate(resolver);
58: } catch (Exception e) {
59: log.error("Evaluation error in '" + expr + "'", e);
60: return false;
61: }
62: if (result instanceof Boolean) {
63: return ((Boolean) result).booleanValue();
64: }
65: log.error("Bad evaluation result in '" + expr + "': " + result);
66: return false;
67: }
68:
69: /**
70: * @return Returns the expression.
71: */
72: public Expression getExpression() {
73: return expr;
74: }
75:
76: /**
77: * @param expr The expression to set.
78: */
79: public void setExpression(Expression expr) {
80: this .expr = expr;
81: }
82:
83: /* (non-Javadoc)
84: * @see de.odysseus.calyxo.forms.impl.ValidationEngine#localize(java.util.Locale)
85: */
86: public void localize(Locale locale) {
87: }
88:
89: /* (non-Javadoc)
90: * @see de.odysseus.calyxo.forms.impl.ValidationEngine#isSharable()
91: */
92: public boolean isSharable() {
93: return true;
94: }
95: }
|