| 1 |
package morfologik.tools; |
| 2 |
|
| 3 |
import java.nio.file.Files; |
| 4 |
import java.nio.file.Path; |
| 5 |
import java.util.Locale; |
| 6 |
|
| 7 |
import com.beust.jcommander.IValueValidator; |
| 8 |
import com.beust.jcommander.ParameterException; |
| 9 |
|
| 10 |
public final class ValidateParentDirExists implements IValueValidator<Path> { |
| 11 |
@Override |
| 12 |
public void validate(String name, Path value) throws ParameterException { |
| 13 |
value = value.toAbsolutePath().normalize().getParent(); |
| 14 |
|
| 15 |
if (!Files.exists(value)) { |
| 16 |
throw new ParameterException(String.format(Locale.ROOT, "Directory does not exist: %s", value)); |
| 17 |
} |
| 18 |
|
| 19 |
if (!Files.isDirectory(value)) { |
| 20 |
throw new ParameterException(String.format(Locale.ROOT, "Path is not a directory: %s", value)); |
| 21 |
} |
| 22 |
|
| 23 |
if (!Files.isWritable(value)) { |
| 24 |
throw new ParameterException(String.format(Locale.ROOT, "Path is not writable: %s", value)); |
| 25 |
} |
| 26 |
} |
| 27 |
} |