All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jaxx.runtime.validator.field.ExistingDirectoryFieldValidator Maven / Gradle / Ivy

The newest version!
package jaxx.runtime.validator.field;

import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;

import java.io.File;

/**
 * 
 * ExistingDirectoryFieldValidator checks that a File field exists and is a directory.
 * 
 * 

*

* *

    *
  • fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required
  • *
* *

*

*

 * 
 *     <validators>
 *         <!-- Plain-Validator Syntax -->
 *         <validator type="existingDirectory">
 *             <param name="fieldName">tmp</param>
 *             <message>tmp is not an existing directory</message>
 *         </validator>
 * 

* <!-- Field-Validator Syntax --> * <field name="tmp"> * <field-validator type="existingDirectory"> * <message>tmp is not an existing directory</message> * </field-validator> * </field> * </validators> * *

* * @author chemit */ public class ExistingDirectoryFieldValidator extends FieldValidatorSupport { public void validate(Object object) throws ValidationException { String fieldName = getFieldName(); Object value = this.getFieldValue(fieldName, object); if (value==null) { // no value defined addFieldError(fieldName, object); return; } File f; if (value instanceof File) { f = (File) value; } else if (value instanceof String) { f = new File((String) value); } else { addFieldError(fieldName, object); return; } if (!(f.isDirectory() && f.exists())) { // f is not a directory, nor exists addFieldError(fieldName, object); } } @Override public String getValidatorType() { return "existingDirectory"; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy