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
45 if (args == null || args.length == 0) {
46 DefaultLog.getInstance().info("Please use the \"--help\" option to see a " +
47 "list of valid commands and options, as you did not provide any arguments.");
48 System.exit(0);
49 }
50
51 ReportConfiguration configuration = OptionCollection.parseCommands(new File("."), args, Report::printUsage);
52 if (configuration != null) {
53 configuration.validate(DefaultLog.getInstance()::error);
54 Reporter reporter = new Reporter(configuration);
55 reporter.output();
56 reporter.writeSummary(DefaultLog.getInstance().asWriter());
57
58 if (configuration.getClaimValidator().hasErrors()) {
59 configuration.getClaimValidator().logIssues(reporter.getClaimsStatistic());
60 throw new RatDocumentAnalysisException(format("Issues with %s",
61 String.join(", ",
62 configuration.getClaimValidator().listIssues(reporter.getClaimsStatistic()))));
63 }
64 }
65 }
66
67 /**
68 * Prints the usage message on {@code System.out}.
69 * @param opts The defined options.
70 */
71 private static void printUsage(final Options opts) {
72 new Help(System.out).printUsage(opts);
73 }
74
75 private Report() {
76 // do not instantiate
77 }
78 }