001 /*
002 * Copyright 2003-2005 The Apache Software Foundation
003 * Copyright 2005 Stephen McConnell
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package net.dpml.cli.validation;
018
019 import java.util.Iterator;
020 import java.util.List;
021 import java.util.Set;
022
023 import net.dpml.cli.resource.ResourceConstants;
024 import net.dpml.cli.resource.ResourceHelper;
025
026 /**
027 * The <code>EnumValidator</code> validates the string argument
028 * values are valid.
029 *
030 * The following example shows how to limit the valid values
031 * for the color argument to 'red', 'green', or 'blue'.
032 *
033 * <pre>
034 * Set values = new HashSet();
035 * values.add("red");
036 * values.add("green");
037 * values.add("blue");
038 * ...
039 * ArgumentBuilder builder = new ArgumentBuilder();
040 * Argument color =
041 * builder.withName("color");
042 * .withValidator(new EnumValidator(values));
043 * </pre>
044 *
045 * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a>
046 * @version @PROJECT-VERSION@
047 */
048 public class EnumValidator implements Validator
049 {
050 /** List of permitted values */
051 private Set m_validValues;
052
053 /**
054 * Creates a new EnumValidator for the specified values.
055 *
056 * @param values The list of permitted values
057 */
058 public EnumValidator( final Set values )
059 {
060 setValidValues( values );
061 }
062
063 /**
064 * Validate the list of values against the list of permitted values.
065 *
066 * @param values the list of values to validate
067 * @exception InvalidArgumentException if a value is invalid
068 * @see net.dpml.cli.validation.Validator#validate(java.util.List)
069 */
070 public void validate( final List values ) throws InvalidArgumentException
071 {
072 for( final Iterator iter = values.iterator(); iter.hasNext();)
073 {
074 final String value = (String) iter.next();
075 if( !m_validValues.contains( value ) )
076 {
077 throw new InvalidArgumentException(
078 ResourceHelper.getResourceHelper().getMessage(
079 ResourceConstants.ENUM_ILLEGAL_VALUE,
080 new Object[]{value, getValuesAsString()} ) );
081 }
082 }
083 }
084
085 /**
086 * Returns the permitted values in a comma separated String
087 *
088 * @return String formatted list of values
089 */
090 String getValuesAsString()
091 {
092 final StringBuffer buff = new StringBuffer();
093 buff.append( "[" );
094 for( final Iterator iter = m_validValues.iterator(); iter.hasNext();)
095 {
096 buff.append( "'" ).append( iter.next() ).append( "'" );
097 if( iter.hasNext() )
098 {
099 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 }