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

jaxx.runtime.validator.field.ExistingFileFieldValidator 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;

/**
 * 
 * ExistingFileFieldValidator checks that a File field exists. *
 * 
 * 

*

* *

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

*

*

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

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

* * @author chemit */ public class ExistingFileFieldValidator 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.isFile() && f.exists())) { // f is not a file nor exists addFieldError(fieldName, object); } } @Override public String getValidatorType() { return "existingFile"; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy