import java.io.File;
import java.text.MessageFormat;
public class AutoCreateDirectoryValidator implements VariableValidator {
public void validate(EnvironmentVariableContext environmentVariableContext) throws ValidationException {
VariableValue value = environmentVariableContext.getVariableValue();
String name = environmentVariableContext.getVariableName();
File dir = value.getAsFile();
if (!dir.exists()) {
if (!dir.mkdirs()) {
throw new ValidationException(
MessageFormat.format("The value of the variable {0} ({1}) is not an existing directory and cannot be created as well!", name, value));
}
} else if (!value.getAsFile().isDirectory()) {
throw new ValidationException(
MessageFormat.format("The value of the variable {0} ({1}) is not an existing directory!", name, value));
}
}