Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b3e46a1
fix DocumentName issues
Claudenw Jun 9, 2026
51064da
checkpoint
Claudenw Jun 16, 2026
805b423
Fixed several parsing errors in DocumentName.Builder
Claudenw Jun 18, 2026
57124fc
initial changes
Claudenw May 10, 2026
84a4079
created testhelper and updated tests
Claudenw May 10, 2026
e61bcdd
fixed tests
Claudenw May 10, 2026
90a12f8
Change to Reporter.Output usage
Claudenw May 24, 2026
b9e9955
updated javadoc
Claudenw May 24, 2026
fc99411
updated UI strategy
Claudenw May 31, 2026
1a9e1bc
Remove ArchiveEntryDocument and clean up ArchiveWalker.
Claudenw Jun 1, 2026
3355412
Change ArgumentContext to build from Options and arguments rather tha…
Claudenw Jun 1, 2026
b028db2
Switch to UnmodifiableSets for licences
Claudenw Jun 1, 2026
9b90121
Switch to UnmodifiableSets for licences
Claudenw Jun 1, 2026
462ed9b
Use MutableInt in ClaimValidator
Claudenw Jun 1, 2026
bc8cf42
fix case statement
Claudenw Jun 1, 2026
73cef38
fix spotbugs issues
Claudenw Jun 1, 2026
2c73194
fix spotbugs issues
Claudenw Jun 1, 2026
86eac78
fix spotbugs issues
Claudenw Jun 1, 2026
e886cf8
created TempDir replacement to support Windows
Claudenw Jun 1, 2026
f576ae2
Fixed issues with DocumentName and DocumentNameBuilder.
Claudenw Jun 5, 2026
ac06009
fixes for DocumentName errors
Claudenw Jun 9, 2026
a8478ad
fixed rebasing issues
Claudenw Jun 22, 2026
2605ddd
fix DocumentName issues
Claudenw Jun 9, 2026
862a8ad
checkpoint
Claudenw Jun 16, 2026
febe704
Fixed several parsing errors in DocumentName.Builder
Claudenw Jun 18, 2026
8917531
RAT-559: Refactor during review
ottlinger Jun 23, 2026
d9896be
RAT-559: Shorten tests
ottlinger Jun 23, 2026
f3fd02e
fixed merge issues
Claudenw Jun 24, 2026
c484029
fixed rebase issues
Claudenw Jun 25, 2026
b15b06b
fix some sonarqube issues
Claudenw Jun 25, 2026
f4939c2
cleaned up sonar issues
Claudenw Jun 26, 2026
42aeae6
fixed sonar issues
Claudenw Jun 26, 2026
cfa48bb
fixed checkstyle issues
Claudenw Jun 26, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apache-rat-core/src/it/java/org/apache/rat/ReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void integrationTest(String testName, Document commandLineDoc) throws Exc
try {
Object value = shell.run(groovyScript, new String[]{outputFile.getAbsolutePath(), logFile.getAbsolutePath()});
if (value != null) {
fail(String.format("%s", value));
fail(String.format("%s: %s", testName, value));
}
} catch (AssertionError e) {
throw new AssertionError(String.format("%s: %s", testName, e.getMessage()), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ myArgs[3] = src.getAbsolutePath()

ReportConfiguration configuration = OptionCollection.parseCommands(src, myArgs, { opts -> })
assertNotNull(configuration)
configuration.validate(DefaultLog.getInstance().&error)
configuration.validate()
Reporter reporter = new Reporter(configuration)
ClaimStatistic statistic = reporter.execute()
ClaimStatistic statistic = reporter.execute().getStatistic()

assertEquals(3, statistic.getCounter(ClaimStatistic.Counter.APPROVED))
assertEquals(2, statistic.getCounter(ClaimStatistic.Counter.ARCHIVES))
Expand Down
24 changes: 9 additions & 15 deletions apache-rat-core/src/main/java/org/apache/rat/OptionCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.util.stream.Collectors;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
Expand All @@ -48,7 +47,7 @@
import org.apache.rat.document.FileDocument;
import org.apache.rat.help.Licenses;
import org.apache.rat.license.LicenseSetFactory;
import org.apache.rat.report.IReportable;
import org.apache.rat.report.Reportable;
import org.apache.rat.report.claim.ClaimStatistic;
import org.apache.rat.utils.DefaultLog;
import org.apache.rat.utils.Log.Level;
Expand Down Expand Up @@ -131,27 +130,22 @@ public static ReportConfiguration parseCommands(final File workingDirectory, fin
final Consumer<Options> helpCmd, final boolean noArgs) throws IOException {

Options opts = buildOptions();
CommandLine commandLine;
ArgumentContext argumentContext;
try {
commandLine = DefaultParser.builder().setDeprecatedHandler(DeprecationReporter.getLogReporter())
.setAllowPartialMatching(true).build().parse(opts, args);
argumentContext = new ArgumentContext(workingDirectory, opts, args);
} catch (ParseException e) {
DefaultLog.getInstance().error(e.getMessage());
DefaultLog.getInstance().error("Please use the \"--help\" option to see a list of valid commands and options.", e);
System.exit(1);
return null; // dummy return (won't be reached) to avoid Eclipse complaint about possible NPE
// for "commandLine"
}

ArgumentContext argumentContext = new ArgumentContext(workingDirectory, commandLine);
Arg.processLogLevel(argumentContext, CLIOptionCollection.INSTANCE);

if (commandLine.hasOption(HELP)) {
if (argumentContext.getCommandLine().hasOption(HELP)) {
helpCmd.accept(opts);
return null;
}

if (commandLine.hasOption(Arg.HELP_LICENSES.option())) {
if (argumentContext.getCommandLine().hasOption(Arg.HELP_LICENSES.option())) {
new Licenses(createConfiguration(argumentContext), new PrintWriter(System.out, false, StandardCharsets.UTF_8)).printHelp();
return null;
}
Expand Down Expand Up @@ -183,14 +177,14 @@ public static ReportConfiguration createConfiguration(final ArgumentContext argu
Optional<Option> dirOpt = CLIOptionCollection.INSTANCE.getSelected(Arg.DIR);
if (dirOpt.isPresent()) {
try {
configuration.addSource(getReportable(commandLine.getParsedOptionValue(
dirOpt.get()), configuration));
DocumentName directoryName = commandLine.getParsedOptionValue(dirOpt.get());
configuration.addSource(getReportable(directoryName.asFile(), configuration));
} catch (ParseException e) {
throw new ConfigurationException("Unable to set parse " + dirOpt.get(), e);
}
}
for (String s : commandLine.getArgs()) {
IReportable reportable = getReportable(new File(s), configuration);
Reportable reportable = getReportable(new File(s), configuration);
if (reportable != null) {
configuration.addSource(reportable);
}
Expand All @@ -215,7 +209,7 @@ public static Options buildOptions() {
* @param config the ReportConfiguration.
* @return the IReportable instance containing the files.
*/
public static IReportable getReportable(final File base, final ReportConfiguration config) {
public static Reportable getReportable(final File base, final ReportConfiguration config) {
File absBase = base.getAbsoluteFile();
DocumentName documentName = DocumentName.builder(absBase).build();
if (!absBase.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.lang3.StringUtils;
import org.apache.rat.api.RatException;
import org.apache.rat.commandline.Arg;
import org.apache.rat.commandline.ArgumentContext;
import org.apache.rat.help.Licenses;
import org.apache.rat.report.IReportable;
import org.apache.rat.report.Reportable;
import org.apache.rat.ui.UIOptionCollection;
import org.apache.rat.utils.DefaultLog;

Expand All @@ -61,11 +62,10 @@
* @param workingDirectory The directory to resolve relative file names against.
* @param args the arguments to parse
* @return the ArgumentContext for the process.
* @throws IOException on error.
* @throws ParseException on option parsing error.
* @throws RatException on error.
*/
public ArgumentContext parseCommands(final File workingDirectory, final String[] args)
throws IOException, ParseException {
throws RatException {
return parseCommands(workingDirectory, args, uiOptionCollection.getOptions());
}

Expand All @@ -76,8 +76,7 @@
* @return the CommandLine
* @throws ParseException on option parsing error.
*/
//@VisibleForTesting
CommandLine parseCommandLine(final Options opts, final String[] args) throws ParseException {
public static CommandLine parseCommandLine(final Options opts, final String[] args) throws ParseException {
try {
return DefaultParser.builder().setDeprecatedHandler(DeprecationReporter.getLogReporter())
.setAllowPartialMatching(true).build().parse(opts, args);
Expand All @@ -95,22 +94,27 @@
* @param args the arguments to parse.
* @param options An Options object containing Apache command line options.
* @return the ArgumentContext for the process.
* @throws IOException on error.
* @throws ParseException on option parsing error.
* @throws RatException on error.
*/
private ArgumentContext parseCommands(final File workingDirectory, final String[] args,
final Options options) throws IOException, ParseException {
CommandLine commandLine = parseCommandLine(options, args);
ArgumentContext argumentContext = new ArgumentContext(workingDirectory, commandLine);
Arg.processLogLevel(argumentContext, uiOptionCollection);
populateConfiguration(argumentContext);
if (uiOptionCollection.isSelected(Arg.HELP_LICENSES)) {
new Licenses(argumentContext.getConfiguration(),
new PrintWriter(argumentContext.getConfiguration().getOutput().get(),
false, StandardCharsets.UTF_8)).printHelp();
final Options options) throws RatException {
try {
ArgumentContext argumentContext = new ArgumentContext(workingDirectory, options, args);
Arg.processLogLevel(argumentContext, uiOptionCollection);
populateConfiguration(argumentContext);
if (uiOptionCollection.isSelected(Arg.HELP_LICENSES)) {
try {

Check warning on line 106 in apache-rat-core/src/main/java/org/apache/rat/OptionCollectionParser.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Extract this nested try block into a separate method.

See more on https://sonarcloud.io/project/issues?id=apache_creadur-rat&issues=AZ8DCZsnFmdf2XmY5rvi&open=AZ8DCZsnFmdf2XmY5rvi&pullRequest=655
new Licenses(argumentContext.getConfiguration(),
new PrintWriter(argumentContext.getConfiguration().getOutput().get(),
false, StandardCharsets.UTF_8)).printHelp();
} catch (IOException e) {
throw new RatException("Unable to print help: " + e.getMessage(), e);
}
}
return argumentContext;
} catch (ParseException e) {
throw new RatException("Unable to parse command line: " + e.getMessage(), e);
}

return argumentContext;
}

/**
Expand All @@ -126,7 +130,7 @@
final CommandLine commandLine = argumentContext.getCommandLine();
if (!configuration.hasSource()) {
for (String s : commandLine.getArgs()) {
IReportable reportable = OptionCollection.getReportable(new File(s), configuration);
Reportable reportable = OptionCollection.getReportable(new File(s), configuration);
if (reportable != null) {
configuration.addSource(reportable);
}
Expand Down
65 changes: 52 additions & 13 deletions apache-rat-core/src/main/java/org/apache/rat/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@
package org.apache.rat;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

import org.apache.commons.cli.Options;
import org.apache.commons.io.function.IOSupplier;
import org.apache.rat.api.RatException;
import org.apache.rat.commandline.ArgumentContext;
import org.apache.rat.document.RatDocumentAnalysisException;
import org.apache.rat.help.Help;
import org.apache.rat.utils.DefaultLog;
Expand All @@ -42,34 +49,66 @@ public final class Report {
public static void main(final String[] args) throws Exception {
DefaultLog.getInstance().info(new VersionInfo().toString());


if (args == null || args.length == 0) {
DefaultLog.getInstance().info("Please use the \"--help\" option to see a " +
"list of valid commands and options, as you did not provide any arguments.");
System.exit(0);
}

ReportConfiguration configuration = OptionCollection.parseCommands(new File("."), args, Report::printUsage);
if (configuration != null) {
configuration.validate(DefaultLog.getInstance()::error);
Reporter reporter = new Reporter(configuration);
reporter.output();
reporter.writeSummary(DefaultLog.getInstance().asWriter());
Reporter.Output result = generateReport(CLIOptionCollection.INSTANCE, new File("."), args);
if (result != null) {
result.writeSummary(DefaultLog.getInstance().asWriter());

if (configuration.getClaimValidator().hasErrors()) {
configuration.getClaimValidator().logIssues(reporter.getClaimsStatistic());
if (result.getConfiguration().getClaimValidator().hasErrors()) {
result.getConfiguration().getClaimValidator().logIssues(result.getStatistic());
throw new RatDocumentAnalysisException(format("Issues with %s",
String.join(", ",
configuration.getClaimValidator().listIssues(reporter.getClaimsStatistic()))));
result.getConfiguration().getClaimValidator().listIssues(result.getStatistic()))));
}
}
}

/**
* Prints the usage message on {@code System.out}.
* @param opts The defined options.
* Prints the usage message on the specified output stream.
* @param out The OutputStream supplier
*/
private static void printUsage(final Options opts) {
new Help(System.out).printUsage(opts);
private static void printUsage(final Options options, final IOSupplier<OutputStream> out) {
try (OutputStream stream = out.get();
PrintWriter writer = new PrintWriter(stream, false, StandardCharsets.UTF_8)) {
new Help(writer).printUsage(options);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

/**
* Generates the report
* @param workingDirectory the directory that we are executing in
* @param args the arguments from the command line.
* @return The Client output.
* @throws Exception on error.
*/
static Reporter.Output generateReport(final CLIOptionCollection optionCollection, final File workingDirectory, final String[] args) throws RatException {
Reporter.Output output = null;
OptionCollectionParser optionParser = new OptionCollectionParser(optionCollection);
ArgumentContext argumentContext = optionParser.parseCommands(workingDirectory, args);
ReportConfiguration configuration = argumentContext.getConfiguration();
if (configuration != null) {
if (argumentContext.getCommandLine().hasOption(CLIOptionCollection.HELP)) {
printUsage(optionCollection.getOptions(), argumentContext.getConfiguration().getOutput());
} else if (!configuration.hasSource()) {
String msg = "No directories or files specified for scanning. Did you forget to close a multi-argument option?";
DefaultLog.getInstance().error(msg);
printUsage(optionCollection.getOptions(), argumentContext.getConfiguration().getOutput());
} else {
configuration.validate();
Reporter reporter = new Reporter(configuration);
output = reporter.execute();
output.format(configuration);
}
}
return output;
}

private Report() {
Expand Down
Loading
Loading