1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.io.OutputStreamWriter;
25 import java.io.Writer;
26
27 import org.apache.commons.cli.Options;
28 import org.apache.commons.io.function.IOSupplier;
29 import org.apache.rat.commandline.ArgumentContext;
30 import org.apache.rat.document.RatDocumentAnalysisException;
31 import org.apache.rat.help.Help;
32 import org.apache.rat.utils.DefaultLog;
33
34 import static java.lang.String.format;
35
36
37
38
39 public final class Report {
40
41
42
43 private static final CLIOptionCollection OPTION_COLLECTION = new CLIOptionCollection();
44
45
46
47
48
49
50
51
52 public static void main(final String[] args) throws Exception {
53 DefaultLog.getInstance().info(new VersionInfo().toString());
54
55 if (args == null || args.length == 0) {
56 DefaultLog.getInstance().info("Please use the \"--help\" option to see a " +
57 "list of valid commands and options, as you did not provide any arguments.");
58 return;
59 }
60 OptionCollectionParser<CLIOption> cliOptionParser = new OptionCollectionParser<>(OPTION_COLLECTION);
61 ArgumentContext argumentContext = cliOptionParser.parseCommands(new File("."), args);
62 ReportConfiguration configuration = argumentContext.getConfiguration();
63
64 if (argumentContext.hasOption(CLIOptionCollection.HELP)) {
65 printUsage(OPTION_COLLECTION.getOptions(), configuration.getOutput());
66 } else if (!configuration.hasSource()) {
67 String msg = "No directories or files specified for scanning. Did you forget to close a multi-argument option?";
68 DefaultLog.getInstance().error(msg);
69 printUsage(OPTION_COLLECTION.getOptions(), configuration.getOutput());
70 } else {
71 configuration.validate(DefaultLog.getInstance()::error);
72 Reporter.Output output = new Reporter(configuration).execute();
73 output.format(argumentContext.getConfiguration());
74 output.writeSummary(DefaultLog.getInstance().asWriter());
75
76 if (configuration.getClaimValidator().hasErrors()) {
77 configuration.getClaimValidator().logIssues(output.getStatistic());
78 throw new RatDocumentAnalysisException(format("Issues with %s",
79 String.join(", ",
80 configuration.getClaimValidator().listIssues(output.getStatistic()))));
81 }
82 }
83 }
84
85
86
87
88
89
90 private static void printUsage(final Options opts, final IOSupplier<OutputStream> out) {
91 try (Writer writer = new OutputStreamWriter(out.get())) {
92 new Help(OPTION_COLLECTION, writer).printUsage(opts);
93 } catch (IOException e) {
94 DefaultLog.getInstance().error("Unable to open output stream", e);
95 }
96 }
97
98 private Report() {
99
100 }
101 }