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; 014 015 import static net.sf.oval.Validator.*; 016 017 import java.util.Map; 018 019 import net.sf.oval.expression.ExpressionLanguage; 020 021 /** 022 * Partial implementation of exclusion classes. 023 * 024 * @author Sebastian Thomschke 025 */ 026 public abstract class AbstractCheckExclusion implements CheckExclusion 027 { 028 private static final long serialVersionUID = 1L; 029 030 private String[] profiles; 031 032 private String when; 033 private String whenFormula; 034 private String whenLang; 035 036 public Map<String, String> getMessageVariables() 037 { 038 return null; 039 } 040 041 /** 042 * {@inheritDoc} 043 */ 044 public String[] getProfiles() 045 { 046 return profiles; 047 } 048 049 /** 050 * {@inheritDoc} 051 */ 052 public String getWhen() 053 { 054 return whenLang + ":" + when; 055 } 056 057 /** 058 * {@inheritDoc} 059 */ 060 public boolean isActive(final Object validatedObject, final Object valueToValidate, final Validator validator) 061 { 062 if (when == null) return true; 063 064 final Map<String, Object> values = getCollectionFactory().createMap(); 065 values.put("_value", valueToValidate); 066 values.put("_this", validatedObject); 067 068 final ExpressionLanguage el = validator.getExpressionLanguageRegistry().getExpressionLanguage(whenLang); 069 return el.evaluateAsBoolean(whenFormula, values); 070 } 071 072 /** 073 * {@inheritDoc} 074 */ 075 public void setProfiles(final String... profiles) 076 { 077 this.profiles = profiles; 078 } 079 080 /** 081 * {@inheritDoc} 082 */ 083 public void setWhen(final String when) 084 { 085 if (when == null || when.length() == 0) 086 { 087 this.when = null; 088 this.whenFormula = null; 089 this.whenLang = null; 090 } 091 else 092 { 093 this.when = when; 094 final String[] parts = when.split(":", 2); 095 if (parts.length == 0) 096 throw new IllegalArgumentException("[when] is missing the scripting language declaration"); 097 whenLang = parts[0]; 098 whenFormula = parts[1]; 099 } 100 } 101 }