/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.forms.check;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.el.ELException;
import javax.servlet.jsp.el.Expression;
import javax.servlet.jsp.el.VariableResolver;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import de.odysseus.calyxo.base.misc.CalyxoVariableResolver;
import de.odysseus.calyxo.forms.Checker;
/**
* EL expression checker.
*
* @author Christoph Beck
* @author Oliver Stuhr
*/
public class ELChecker implements Checker {
private static final Log log = LogFactory.getLog(ELChecker.class);
private Expression expr;
/* (non-Javadoc)
* @see de.odysseus.calyxo.forms.Checker#check(javax.servlet.http.HttpServletRequest, java.lang.Object)
*/
public boolean check(HttpServletRequest request, final Object value) {
VariableResolver resolver = new CalyxoVariableResolver(request) {
public Object resolveVariable(String variable) throws ELException {
if ("property".equals(variable)) {
return value;
}
return super.resolveVariable(variable);
}
};
Object result = null;
try {
result = expr.evaluate(resolver);
} catch (Exception e) {
log.error("Evaluation error in '" + expr + "'", e);
return false;
}
if (result instanceof Boolean) {
return ((Boolean)result).booleanValue();
}
log.error("Bad evaluation result in '" + expr + "': " + result);
return false;
}
/**
* @return Returns the expression.
*/
public Expression getExpression() {
return expr;
}
/**
* @param expr The expression to set.
*/
public void setExpression(Expression expr) {
this.expr = expr;
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.forms.impl.ValidationEngine#localize(java.util.Locale)
*/
public void localize(Locale locale) {
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.forms.impl.ValidationEngine#isSharable()
*/
public boolean isSharable() {
return true;
}
}