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.io.File; 20: import java.util.List; 21: import java.util.ListIterator; 22: 23: /** 24: * The <code>FileValidator</code> validates the string argument 25: * values are files. If the value is a file, the string value in 26: * the {@link java.util.List} of values is replaced with the 27: * {@link java.io.File} instance. 28: * 29: * The following attributes can also be specified using the 30: * appropriate settors: 31: * <ul> 32: * <li>existing</li> 33: * <li>is a file</li> 34: * <li>is a directory</li> 35: * </ul> 36: * 37: * The following example shows how to limit the valid values 38: * for the config attribute to files that exist. 39: * 40: * <pre> 41: * ... 42: * ArgumentBuilder builder = new ArgumentBuilder(); 43: * FileValidator validator = FileValidator.getExistingFileInstance(); 44: * Argument age = 45: * builder.withName("config"); 46: * .withValidator(validator); 47: * </pre> 48: * 49: * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a> 50: * @version @PROJECT-VERSION@ 51: */ 52: public class FileValidator implements Validator 53: { 54: /** 55: * Returns a <code>FileValidator</code> for existing files/directories. 56: * 57: * @return a <code>FileValidator</code> for existing files/directories. 58: */ 59: public static FileValidator getExistingInstance() 60: { 61: final FileValidator validator = new FileValidator(); 62: validator.setExisting( true ); 63: return validator; 64: } 65: 66: /** 67: * Returns a <code>FileValidator</code> for existing files. 68: * 69: * @return a <code>FileValidator</code> for existing files. 70: */ 71: public static FileValidator getExistingFileInstance() 72: { 73: final FileValidator validator = new FileValidator(); 74: validator.setExisting( true ); 75: validator.setFile( true ); 76: return validator; 77: } 78: 79: /** 80: * Returns a <code>FileValidator</code> for existing directories. 81: * 82: * @return a <code>FileValidator</code> for existing directories. 83: */ 84: public static FileValidator getExistingDirectoryInstance() 85: { 86: final FileValidator validator = new FileValidator(); 87: validator.setExisting( true ); 88: validator.setDirectory( true ); 89: return validator; 90: } 91: 92: /** whether the argument value exists */ 93: private boolean m_existing = false; 94: 95: /** whether the argument value is a directory */ 96: private boolean m_directory = false; 97: 98: /** whether the argument value is a file */ 99: private boolean m_file = false; 100: 101: /** 102: * Validate the list of values against the list of permitted values. 103: * If a value is valid, replace the string in the <code>values</code> 104: * {@link java.util.List} with the {@link java.io.File} instance. 105: * 106: * @param values the list of values to validate 107: * @exception InvalidArgumentException if a value is invalid 108: * @see net.dpml.cli.validation.Validator#validate(java.util.List) 109: */ 110: public void validate( final List values ) throws InvalidArgumentException 111: { 112: for( final ListIterator i = values.listIterator(); i.hasNext();) 113: { 114: final Object next = i.next(); 115: if( next instanceof File ) 116: { 117: return; 118: } 119: final String name = (String) next; 120: final File f = new File( name ); 121: if( ( m_existing && !f.exists() ) 122: || ( m_file && !f.isFile() ) 123: || ( m_directory && !f.isDirectory() ) ) 124: { 125: throw new InvalidArgumentException( name ); 126: } 127: i.set( f ); 128: } 129: } 130: 131: /** 132: * Returns whether the argument values must represent directories. 133: * 134: * @return whether the argument values must represent directories. 135: */ 136: public boolean isDirectory() 137: { 138: return m_directory; 139: } 140: 141: /** 142: * Specifies whether the argument values must represent directories. 143: * 144: * @param directory specifies whether the argument values must 145: * represent directories. 146: */ 147: public void setDirectory( boolean directory ) 148: { 149: m_directory = directory; 150: } 151: 152: /** 153: * Returns whether the argument values must represent existing 154: * files/directories. 155: * 156: * @return whether the argument values must represent existing 157: * files/directories. 158: */ 159: public boolean isExisting() 160: { 161: return m_existing; 162: } 163: 164: /** 165: * Specifies whether the argument values must represent existing 166: * files/directories. 167: * 168: * @param existing specifies whether the argument values must 169: * represent existing files/directories. 170: */ 171: public void setExisting( boolean existing ) 172: { 173: m_existing = existing; 174: } 175: 176: /** 177: * Returns whether the argument values must represent directories. 178: * 179: * @return whether the argument values must represent directories. 180: */ 181: public boolean isFile() 182: { 183: return m_file; 184: } 185: 186: /** 187: * Specifies whether the argument values must represent files. 188: * 189: * @param file specifies whether the argument values must 190: * represent files. 191: */ 192: public void setFile( boolean file ) 193: { 194: m_file = file; 195: } 196: }