Frames | No Frames |
1: /* 2: * Copyright 2003-2005 The Apache Software Foundation 3: * Copyright 2005 Stephen McConnell 4: * 5: * Licensed under the Apache License, Version 2.0 (the "License"); 6: * you may not use this file except in compliance with the License. 7: * You may obtain a copy of the License at 8: * 9: * http://www.apache.org/licenses/LICENSE-2.0 10: * 11: * Unless required by applicable law or agreed to in writing, software 12: * distributed under the License is distributed on an "AS IS" BASIS, 13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14: * See the License for the specific language governing permissions and 15: * limitations under the License. 16: */ 17: package net.dpml.cli.validation; 18: 19: import java.util.Iterator; 20: import java.util.List; 21: import java.util.Set; 22: 23: import net.dpml.cli.resource.ResourceConstants; 24: import net.dpml.cli.resource.ResourceHelper; 25: 26: /** 27: * The <code>EnumValidator</code> validates the string argument 28: * values are valid. 29: * 30: * The following example shows how to limit the valid values 31: * for the color argument to 'red', 'green', or 'blue'. 32: * 33: * <pre> 34: * Set values = new HashSet(); 35: * values.add("red"); 36: * values.add("green"); 37: * values.add("blue"); 38: * ... 39: * ArgumentBuilder builder = new ArgumentBuilder(); 40: * Argument color = 41: * builder.withName("color"); 42: * .withValidator(new EnumValidator(values)); 43: * </pre> 44: * 45: * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a> 46: * @version @PROJECT-VERSION@ 47: */ 48: public class EnumValidator implements Validator 49: { 50: /** List of permitted values */ 51: private Set m_validValues; 52: 53: /** 54: * Creates a new EnumValidator for the specified values. 55: * 56: * @param values The list of permitted values 57: */ 58: public EnumValidator( final Set values ) 59: { 60: setValidValues( values ); 61: } 62: 63: /** 64: * Validate the list of values against the list of permitted values. 65: * 66: * @param values the list of values to validate 67: * @exception InvalidArgumentException if a value is invalid 68: * @see net.dpml.cli.validation.Validator#validate(java.util.List) 69: */ 70: public void validate( final List values ) throws InvalidArgumentException 71: { 72: for( final Iterator iter = values.iterator(); iter.hasNext();) 73: { 74: final String value = (String) iter.next(); 75: if( !m_validValues.contains( value ) ) 76: { 77: throw new InvalidArgumentException( 78: ResourceHelper.getResourceHelper().getMessage( 79: ResourceConstants.ENUM_ILLEGAL_VALUE, 80: new Object[]{value, getValuesAsString()} ) ); 81: } 82: } 83: } 84: 85: /** 86: * Returns the permitted values in a comma separated String 87: * 88: * @return String formatted list of values 89: */ 90: String getValuesAsString() 91: { 92: final StringBuffer buff = new StringBuffer(); 93: buff.append( "[" ); 94: for( final Iterator iter = m_validValues.iterator(); iter.hasNext();) 95: { 96: buff.append( "'" ).append( iter.next() ).append( "'" ); 97: if( iter.hasNext() ) 98: { 99: buff.append( ", " ); 100: } 101: } 102: buff.append( "]" ); 103: return buff.toString(); 104: } 105: 106: /** 107: * Returns the Set of valid argument values. 108: * 109: * @return Returns the Set of valid argument values. 110: */ 111: public Set getValidValues() 112: { 113: return m_validValues; 114: } 115: 116: /** 117: * Specifies the Set of valid argument values. 118: * 119: * @param validValues The Set of valid argument values. 120: */ 121: protected void setValidValues( Set validValues ) 122: { 123: m_validValues = validValues; 124: } 125: }