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;
14  
15  import static net.sf.oval.Validator.*;
16  
17  import java.util.Map;
18  
19  import net.sf.oval.expression.ExpressionLanguage;
20  
21  /**
22   * Partial implementation of exclusion classes.
23   * 
24   * @author Sebastian Thomschke
25   */
26  public abstract class AbstractCheckExclusion implements CheckExclusion
27  {
28  	private static final long serialVersionUID = 1L;
29  
30  	private String[] profiles;
31  
32  	private String when;
33  	private String whenFormula;
34  	private String whenLang;
35  
36  	public Map<String, String> getMessageVariables()
37  	{
38  		return null;
39  	}
40  
41  	/**
42  	 * {@inheritDoc}
43  	 */
44  	public String[] getProfiles()
45  	{
46  		return profiles;
47  	}
48  
49  	/**
50  	 * {@inheritDoc}
51  	 */
52  	public String getWhen()
53  	{
54  		return whenLang + ":" + when;
55  	}
56  
57  	/**
58  	 * {@inheritDoc}
59  	 */
60  	public boolean isActive(final Object validatedObject, final Object valueToValidate, final Validator validator)
61  	{
62  		if (when == null) return true;
63  
64  		final Map<String, Object> values = getCollectionFactory().createMap();
65  		values.put("_value", valueToValidate);
66  		values.put("_this", validatedObject);
67  
68  		final ExpressionLanguage el = validator.getExpressionLanguageRegistry().getExpressionLanguage(whenLang);
69  		return el.evaluateAsBoolean(whenFormula, values);
70  	}
71  
72  	/**
73  	 * {@inheritDoc}
74  	 */
75  	public void setProfiles(final String... profiles)
76  	{
77  		this.profiles = profiles;
78  	}
79  
80  	/**
81  	 * {@inheritDoc}
82  	 */
83  	public void setWhen(final String when)
84  	{
85  		if (when == null || when.length() == 0)
86  		{
87  			this.when = null;
88  			this.whenFormula = null;
89  			this.whenLang = null;
90  		}
91  		else
92  		{
93  			this.when = when;
94  			final String[] parts = when.split(":", 2);
95  			if (parts.length == 0)
96  				throw new IllegalArgumentException("[when] is missing the scripting language declaration");
97  			whenLang = parts[0];
98  			whenFormula = parts[1];
99  		}
100 	}
101 }