View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
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   * The CLI based configuration object for report generation.
38   */
39  public final class Report {
40      /**
41       * The option collection that this report is using.
42       */
43      private static final CLIOptionCollection OPTION_COLLECTION = new CLIOptionCollection();
44  
45      /**
46       * Processes the command line and builds a configuration and executes the
47       * report.
48       *
49       * @param args the arguments.
50       * @throws Exception on error.
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       * Prints the usage message on the output stream from {@code out}.
87       * @param opts the defined options.
88       * @param out a supplier of an OutputStream.
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          // do not instantiate
100     }
101 }