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