Commit f095fe1a authored by Jos van den Oever's avatar Jos van den Oever

Better checking of input.

parent 1831e67b
package odf;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
......@@ -20,6 +21,8 @@ public class Main {
try {
conf = RunConfiguration.parseArguments(args);
} catch (ArgumentException e) {
System.err.println(e.getMessage());
System.exit(1);
return;
}
OdfZipNormalizer odfNormalizer = new OdfZipNormalizer();
......@@ -28,6 +31,7 @@ public class Main {
return;
}
}
System.out.println(conf.output);
if (conf.output != null) {
System.out.println(conf.input);
if (conf.input != null && conf.output != null) {
......@@ -57,6 +61,43 @@ class RunConfiguration {
@Nullable
Path output;
static Path stringToWritableDirPath(String path) throws ArgumentException {
Path p = Paths.get(path).toAbsolutePath();
if (!Files.isDirectory(p) || !Files.isWritable(p)) {
throw new ArgumentException("Path " + p
+ " is not a writable directory.");
}
return p;
}
static Path stringToReadableDirPath(String path) throws ArgumentException {
Path p = Paths.get(path).toAbsolutePath();
if (!Files.isDirectory(p) || !Files.isReadable(p)) {
throw new ArgumentException("Path " + p
+ " is not a readable directory.");
}
return p;
}
static Path stringToReadableFilePath(String path) throws ArgumentException {
Path p = Paths.get(path).toAbsolutePath();
if (!Files.isRegularFile(p) || !Files.isReadable(p)) {
throw new ArgumentException("Path " + p
+ " is not a readable file.");
}
return p;
}
static Path stringToWritableFilePath(String path) throws ArgumentException {
Path p = Paths.get(path).toAbsolutePath();
Path dir = p.getParent();
if (!Files.isDirectory(dir) || !Files.isWritable(dir)) {
throw new ArgumentException("Path " + p
+ " is not a writable file.");
}
return p;
}
static RunConfiguration parseArguments(String[] args)
throws ArgumentException {
RunConfiguration c = new RunConfiguration();
......@@ -64,16 +105,16 @@ class RunConfiguration {
int i = 0;
while (i + 1 < args.length) {
if (args[i].equals("--testsDir")) {
c.testsDir = Paths.get(args[i + 1]);
c.testsDir = stringToReadableDirPath(args[i + 1]);
} else if (args[i].equals("--repoDir")) {
c.repoDir = Paths.get(args[i + 1]);
c.repoDir = stringToWritableDirPath(args[i + 1]);
} else if (args[i].equals("--officeDir")) {
c.officeDir = Paths.get(args[i + 1]);
c.officeDir = stringToReadableDirPath(args[i + 1]);
} else if (args[i].equals("--historyFile")) {
c.historyFile = Paths.get(args[i + 1]);
c.historyFile = stringToReadableFilePath(args[i + 1]);
} else {
c.input = Paths.get(args[i]).toAbsolutePath();
c.output = Paths.get(args[i + 1]).toAbsolutePath();
c.input = stringToReadableFilePath(args[i]);
c.output = stringToWritableFilePath(args[i + 1]);
}
i += 2;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment