Skip to content

Commit

Permalink
Update input arguments and messaging.
Browse files Browse the repository at this point in the history
  • Loading branch information
timronan committed Mar 13, 2020
1 parent 108ff64 commit a6f8a3e
Show file tree
Hide file tree
Showing 16 changed files with 409 additions and 120 deletions.
79 changes: 61 additions & 18 deletions src/main/java/edu/iris/dmc/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -77,7 +79,8 @@ public static void main(String[] args) throws Exception {
try {
commandLine = CommandLine.parse(args);
} catch (CommandLineParseException e) {
System.err.println(e.getMessage());
System.err.println(e.getMessage()+ "\n");
help();
System.exit(1);
}
Logger rootLogger = LogManager.getLogManager().getLogger("");
Expand Down Expand Up @@ -132,14 +135,22 @@ public void run() throws Exception {
if (commandLine.output() != null) {
outputFile = commandLine.output().toFile();
if (!outputFile.exists()) {
throw new IOException(String.format("File %s is not found!", commandLine.output().toString()));
outputFile.createNewFile();
//throw new IOException(String.format("File %s is not found!", commandLine.output().toString()));
}

try (OutputStream outputStream = new FileOutputStream(outputFile)) {
run(input, "csv", outputStream, commandLine.ignoreRules(), commandLine.ignoreWarnings());
}
try (OutputStream outputStream = (outputFile != null) ? new FileOutputStream(outputFile) : System.out;) {
run(input, "csv", outputStream, commandLine.ignoreRules(), commandLine.ignoreWarnings());
}else {
try (OutputStream outputStream = System.out;) {
run(input, "csv", outputStream, commandLine.ignoreRules(), commandLine.ignoreWarnings());
}

}

}

private void run(List<Path> input, String format, OutputStream outputStream, int[] ignoreRules,
boolean ignoreWarnings) throws Exception {
RuleEngineService ruleEngineService = new RuleEngineService(ignoreWarnings, ignoreRules);
Expand All @@ -164,6 +175,7 @@ private FDSNStationXML read(Path path) throws Exception {
throw new IOException(String.format("File %s does not exist. File is required!", file.getAbsoluteFile()));
}
try (InputStream is = new FileInputStream(file)) {
// This is where stationxml vs seed is decided.
if (file.getName().toLowerCase().endsWith(".xml")) {
return DocumentMarshaller.unmarshal(is);
} else {
Expand Down Expand Up @@ -211,9 +223,11 @@ public Volume load(InputStream inputStream) throws SeedException, IOException {

private RuleResultPrintStream getOutputStream(String format, OutputStream outputStream) throws IOException {
if (format == null || format.isEmpty() || "html".equalsIgnoreCase(format)) {
return new HtmlPrintStream(outputStream);
return new CsvPrintStream(outputStream);
} else if ("csv".equalsIgnoreCase(format)) {
return new CsvPrintStream(outputStream);
} else if ("html".equalsIgnoreCase(format)) {
return new HtmlPrintStream(outputStream);
} else if ("xml".equalsIgnoreCase(format)) {
return new XmlPrintStream(outputStream);
} else if ("report".equalsIgnoreCase(format)) {
Expand Down Expand Up @@ -248,19 +262,47 @@ private static String center(String text, int length, String pad) {
private static void printRules() {

RuleEngineService ruleEngineService = new RuleEngineService(false, null);
for (Rule rule : ruleEngineService.getRules()) {
List<Rule> ruleslist = ruleEngineService.getRules();
Collections.sort(ruleslist, comparator);
System.out.println("Validator Rule Set:\n");
System.out.println("--------------------------------------------------------------------------");
for (Rule rule : ruleslist) {
System.out.printf("%-8s %s%n", rule.getId(), rule.getDescription());
}
System.out.println("--------------------------------------------------------------------------");

}

private static Comparator<Rule> comparator = new Comparator<Rule>() {
public int compare(Rule c1, Rule c2) {

int r = Integer.compare(c1.getId(), c2.getId());

return r;
}
};

private static void printUnits() {
System.out.println("UNIT TABLE:");
System.out.println("-------------------------------------");
for (String unit : UnitTable.units) {
System.out.println(unit);
System.out.println("Table of Acceptable Units:\n");

System.out.println("--------------------------------------------------------------------------");

List<String> unitlist = UnitTable.units;
int stride = (unitlist.size()/4);
for (int row = 0; row < unitlist.size()/4; row++) {
System.out.println(String.format("%15s %15s %15s %15s",
unitlist.get(row), unitlist.get(row + stride),
unitlist.get(row + stride * 2), unitlist.get(row + stride * 3)));
}


//for (String unit : UnitTable.units) {
// System.out.println();

}
//}
System.out.println("--------------------------------------------------------------------------");

}

private static void help() throws IOException {
String version = "Version " + getVersion();
Expand All @@ -273,12 +315,13 @@ private static void help() throws IOException {
System.out.println("Usage:");
System.out.println("java -jar stationxml-validator <FILE> [OPTIONS]");
System.out.println("OPTIONS");
System.out.println(" --output : where to output result, default is System.out");
System.out.println(" --ignore-warnings: don't show warnings");
System.out.println(" --rules : print a list of validation rules");
System.out.println(" --units : print a list of units used to validate");
System.out.println(" --debug :");
System.out.println(" --help : print this message");
System.out.println(" --file : Full input file path");
System.out.println(" --output : where to output result, default is System.out");
System.out.println(" --ignore-warnings : don't show warnings");
System.out.println(" --rules : print a list of validation rules");
System.out.println(" --units : print a list of units used to validate");
System.out.println(" --debug : Change the verobsity level to debug");
System.out.println(" --help : print this message");
System.out.println("===============================================================");
System.exit(0);
}
Expand Down
71 changes: 55 additions & 16 deletions src/main/java/edu/iris/dmc/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,50 @@ public boolean showUnits() {
public Level getLogLevel() {
return logLevel;
}

public static CommandLine parse(String[] args) throws CommandLineParseException {
CommandLine commandLine = new CommandLine();

if (args == null || args.length == 0) {
throw new CommandLineParseException("Application arguments cannot be empty or null!");

}
CommandLine commandLine = new CommandLine();
// look for showHelp or showVersion flags
if (args.length == 1) {
if ("--help".equalsIgnoreCase(args[0]) || "--showhelp".equalsIgnoreCase(args[0])
|| "-h".equalsIgnoreCase(args[0])) {
commandLine.showHelp = true;
return commandLine;
} else if ("--version".equalsIgnoreCase(args[0]) || "-v".equalsIgnoreCase(args[0])) {
commandLine.showVersion = true;
return commandLine;
} else if ("--units".equalsIgnoreCase(args[0]) || "-u".equalsIgnoreCase(args[0])) {
commandLine.showUnits = true;
return commandLine;
} else if ("--rules".equalsIgnoreCase(args[0]) || "-r".equalsIgnoreCase(args[0])) {
commandLine.showRules = true;
return commandLine;
} else {
String path = args[0];
commandLine.file = Paths.get(path);
if (!commandLine.file.toFile().exists()) {
commandLine.showHelp = true;
System.out.println(String.format("File %s does not exist!", path));
return commandLine;

}
}
return commandLine;

}
if (args.length < 1) {
throw new CommandLineParseException(
"Invalid number of arguments, expected at least 1 but was " + args.length + "!");
"Invalid number of arguments, expected 1 but was " + args.length + "!");
}
String path = args[0];
commandLine.file = Paths.get(path);

if (!commandLine.file.toFile().exists()) {
throw new CommandLineParseException(String.format("File %s does not exist!", path));
}


// look for logLevel
if (args.length > 2) {
for (int i = 1; i < args.length; i++) {
if (args.length >= 2) {
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if ("--help".equalsIgnoreCase(arg) || "--showhelp".equalsIgnoreCase(arg)
|| "-h".equalsIgnoreCase(arg)) {
Expand All @@ -105,17 +119,42 @@ public static CommandLine parse(String[] args) throws CommandLineParseException
} else if ("--ignore-warnings".equalsIgnoreCase(arg)) {
commandLine.ignoreWarnings = true;
} else if ("--ignore-rules".equalsIgnoreCase(arg)) {
String rules = args[i + 1];
commandLine.ignoreRules = Stream.of(rules.split("\\s*,\\s*")).map(String::trim)
.map(Integer::parseInt).mapToInt(item -> item).toArray();
i = i + 1;
if(args.length < (i+2)) {
throw new CommandLineParseException(String.format("Please provide rules to ignore."));
}else {
String rules = args[i + 1];
commandLine.ignoreRules = Stream.of(rules.split("\\s*,\\s*")).map(String::trim)
.map(Integer::parseInt).mapToInt(item -> item).toArray();
i = i + 1;
}
} else if ("--show-rules".equalsIgnoreCase(arg)) {
commandLine.showRules = true;
} else if ("--show-units".equalsIgnoreCase(arg)) {
commandLine.showUnits = true;
} else if ("--output".equalsIgnoreCase(arg) || "-o".equalsIgnoreCase(arg)) {
commandLine.output = Paths.get(args[i + 1]);
if(args.length < (i+2)) {
throw new CommandLineParseException(String.format("Please provide an argument for --output."));
}else {
commandLine.output = Paths.get(args[i + 1]);
i = i + 1;
}
}else if ("--file".equalsIgnoreCase(arg) || "-f".equalsIgnoreCase(arg)) {
if(args.length < (i+2)) {
throw new CommandLineParseException(String.format("Please provide an argument for --file."));
}else {
String path = args[i+1];
commandLine.file = Paths.get(path);
i = i + 1;
if (!commandLine.file.toFile().exists()) {
throw new CommandLineParseException(String.format("File %s does not exist!", path));
}
}
}else {
String path = args[i];
commandLine.file = Paths.get(path);
if (!commandLine.file.toFile().exists()) {
throw new CommandLineParseException(String.format("File %s does not exist!", path));
}
}

}
Expand Down
Loading

0 comments on commit a6f8a3e

Please sign in to comment.