001 /******************************************************************************* 002 * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian 003 * Thomschke. 004 * 005 * All Rights Reserved. This program and the accompanying materials 006 * are made available under the terms of the Eclipse Public License v1.0 007 * which accompanies this distribution, and is available at 008 * http://www.eclipse.org/legal/epl-v10.html 009 * 010 * Contributors: 011 * Sebastian Thomschke - initial implementation. 012 *******************************************************************************/ 013 package net.sf.oval.constraint; 014 015 import static net.sf.oval.Validator.*; 016 017 import java.util.Map; 018 019 import net.sf.oval.Validator; 020 import net.sf.oval.configuration.annotation.AbstractAnnotationCheck; 021 import net.sf.oval.context.OValContext; 022 import net.sf.oval.exception.ExpressionEvaluationException; 023 import net.sf.oval.exception.ExpressionLanguageNotAvailableException; 024 import net.sf.oval.expression.ExpressionLanguage; 025 026 /** 027 * @author Sebastian Thomschke 028 */ 029 public class AssertCheck extends AbstractAnnotationCheck<Assert> 030 { 031 private static final long serialVersionUID = 1L; 032 033 private String expr; 034 private String lang; 035 036 /** 037 * {@inheritDoc} 038 */ 039 @Override 040 public void configure(final Assert constraintAnnotation) 041 { 042 super.configure(constraintAnnotation); 043 setExpr(constraintAnnotation.expr()); 044 setLang(constraintAnnotation.lang()); 045 } 046 047 /** 048 * {@inheritDoc} 049 */ 050 @Override 051 public Map<String, String> createMessageVariables() 052 { 053 final Map<String, String> messageVariables = getCollectionFactory().createMap(2); 054 messageVariables.put("expression", expr); 055 messageVariables.put("language", lang); 056 return messageVariables; 057 } 058 059 /** 060 * @return the expression 061 */ 062 public String getExpr() 063 { 064 return expr; 065 } 066 067 /** 068 * @return the expression language 069 */ 070 public String getLang() 071 { 072 return lang; 073 } 074 075 /** 076 * {@inheritDoc} 077 */ 078 public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context, 079 final Validator validator) throws ExpressionEvaluationException, ExpressionLanguageNotAvailableException 080 { 081 final Map<String, Object> values = getCollectionFactory().createMap(); 082 values.put("_value", valueToValidate); 083 values.put("_this", validatedObject); 084 085 final ExpressionLanguage el = validator.getExpressionLanguageRegistry().getExpressionLanguage(lang); 086 return el.evaluateAsBoolean(expr, values); 087 } 088 089 /** 090 * @param expression the expression to set 091 */ 092 public void setExpr(final String expression) 093 { 094 expr = expression; 095 requireMessageVariablesRecreation(); 096 } 097 098 /** 099 * @param language the expression language to set 100 */ 101 public void setLang(final String language) 102 { 103 lang = language; 104 requireMessageVariablesRecreation(); 105 } 106 }