View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.constraint;
14  
15  import static net.sf.oval.Validator.*;
16  
17  import java.util.Map;
18  
19  import net.sf.oval.Validator;
20  import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
21  import net.sf.oval.context.OValContext;
22  import net.sf.oval.exception.ExpressionEvaluationException;
23  import net.sf.oval.exception.ExpressionLanguageNotAvailableException;
24  import net.sf.oval.expression.ExpressionLanguage;
25  
26  /**
27   * @author Sebastian Thomschke
28   */
29  public class AssertCheck extends AbstractAnnotationCheck<Assert>
30  {
31  	private static final long serialVersionUID = 1L;
32  
33  	private String expr;
34  	private String lang;
35  
36  	/**
37  	 * {@inheritDoc}
38  	 */
39  	@Override
40  	public void configure(final Assert constraintAnnotation)
41  	{
42  		super.configure(constraintAnnotation);
43  		setExpr(constraintAnnotation.expr());
44  		setLang(constraintAnnotation.lang());
45  	}
46  
47  	/**
48  	 * {@inheritDoc}
49  	 */
50  	@Override
51  	public Map<String, String> createMessageVariables()
52  	{
53  		final Map<String, String> messageVariables = getCollectionFactory().createMap(2);
54  		messageVariables.put("expression", expr);
55  		messageVariables.put("language", lang);
56  		return messageVariables;
57  	}
58  
59  	/**
60  	 * @return the expression
61  	 */
62  	public String getExpr()
63  	{
64  		return expr;
65  	}
66  
67  	/**
68  	 * @return the expression language
69  	 */
70  	public String getLang()
71  	{
72  		return lang;
73  	}
74  
75  	/**
76  	 * {@inheritDoc}
77  	 */
78  	public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context,
79  			final Validator validator) throws ExpressionEvaluationException, ExpressionLanguageNotAvailableException
80  	{
81  		final Map<String, Object> values = getCollectionFactory().createMap();
82  		values.put("_value", valueToValidate);
83  		values.put("_this", validatedObject);
84  
85  		final ExpressionLanguage el = validator.getExpressionLanguageRegistry().getExpressionLanguage(lang);
86  		return el.evaluateAsBoolean(expr, values);
87  	}
88  
89  	/**
90  	 * @param expression the expression to set
91  	 */
92  	public void setExpr(final String expression)
93  	{
94  		expr = expression;
95  		requireMessageVariablesRecreation();
96  	}
97  
98  	/**
99  	 * @param language the expression language to set
100 	 */
101 	public void setLang(final String language)
102 	{
103 		lang = language;
104 		requireMessageVariablesRecreation();
105 	}
106 }