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  
23  import org.apache.commons.cli.Options;
24  import org.apache.rat.document.RatDocumentAnalysisException;
25  import org.apache.rat.help.Help;
26  import org.apache.rat.utils.DefaultLog;
27  
28  import static java.lang.String.format;
29  
30  /**
31   * The CLI based configuration object for report generation.
32   */
33  public final class Report {
34  
35      /**
36       * Processes the command line and builds a configuration and executes the
37       * report.
38       *
39       * @param args the arguments.
40       * @throws Exception on error.
41       */
42      public static void main(final String[] args) throws Exception {
43          DefaultLog.getInstance().info(new VersionInfo().toString());
44          ReportConfiguration configuration = OptionCollection.parseCommands(new File("."), args, Report::printUsage);
45          if (configuration != null) {
46              configuration.validate(DefaultLog.getInstance()::error);
47              Reporter reporter = new Reporter(configuration);
48              reporter.output();
49              reporter.writeSummary(DefaultLog.getInstance().asWriter());
50  
51              if (configuration.getClaimValidator().hasErrors()) {
52                  configuration.getClaimValidator().logIssues(reporter.getClaimsStatistic());
53                  throw new RatDocumentAnalysisException(format("Issues with %s",
54                          String.join(", ",
55                                  configuration.getClaimValidator().listIssues(reporter.getClaimsStatistic()))));
56              }
57          }
58      }
59  
60      /**
61       * Prints the usage message on {@code System.out}.
62       * @param opts The defined options.
63       */
64      private static void printUsage(final Options opts) {
65          new Help(System.out).printUsage(opts);
66      }
67  
68      private Report() {
69          // do not instantiate
70      }
71  }