Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into feature/RAT-369
Browse files Browse the repository at this point in the history
  • Loading branch information
ottlinger committed Apr 26, 2024
2 parents ab5a127 + a49e260 commit bea9d2c
Show file tree
Hide file tree
Showing 182 changed files with 7,018 additions and 3,624 deletions.
3 changes: 1 addition & 2 deletions apache-rat-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
<artifactId>apache-rat-core</artifactId>
<packaging>jar</packaging>
<name>Apache Creadur Rat::Core</name>
<description>The core functionality, shared by the Ant tasks
and the Maven plugin.</description>
<description>The core functionality of Rat that is used by all Clients.</description>
<build>
<resources>
<resource>
Expand Down
64 changes: 64 additions & 0 deletions apache-rat-core/src/main/java/org/apache/rat/BuilderParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.rat;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.SortedSet;

import org.apache.rat.analysis.IHeaderMatcher;
import org.apache.rat.license.ILicenseFamily;

/**
* Parameters that can be set by the BUILDER_PARAM ComponentType. The name of the method listed here
* should be the same as the name specified in the ConfigComponent.
*/
public interface BuilderParams {

/**
* Gets one of the contained methods by name.
*
* @param name the name of the method to get.
* @return the Method.
*/
default Method get(String name) {
try {
return this.getClass().getMethod(name);
} catch (NoSuchMethodException e) {
throw new ImplementationException(String.format("method '%s' is not found in %s", name, this.getClass()));
} catch (IllegalArgumentException e) {
throw new ImplementationException(
String.format("method '%s' in %s can not be retrieved", name, this.getClass()), e);
}
}

/**
* Gets a mapping of matcher names to matchers.
*
* @return the mapping of matcher names to matchers.
*/
Map<String, IHeaderMatcher> matcherMap();

/**
* Gets a sorted set of registered license families.
*
* @return the sorted set of license families.
*/
SortedSet<ILicenseFamily> licenseFamilies();
}
13 changes: 8 additions & 5 deletions apache-rat-core/src/main/java/org/apache/rat/Defaults.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.rat.license.ILicenseFamily;
import org.apache.rat.license.LicenseSetFactory;
import org.apache.rat.license.LicenseSetFactory.LicenseFilter;
import org.apache.rat.utils.Log;

/**
* A class that holds the list of licenses and approved licenses from one or more configuration files.
Expand Down Expand Up @@ -70,8 +71,8 @@ public static void init() {
/**
* Builder constructs instances.
*/
private Defaults(Set<URL> urls) {
this.setFactory = Defaults.readConfigFiles(urls);
private Defaults(Log log, Set<URL> urls) {
this.setFactory = Defaults.readConfigFiles(log, urls);
}

/**
Expand All @@ -86,7 +87,7 @@ public static Builder builder() {
* Reads the configuration files.
* @param urls the URLs to read.
*/
private static LicenseSetFactory readConfigFiles(Collection<URL> urls) {
private static LicenseSetFactory readConfigFiles(Log log, Collection<URL> urls) {

SortedSet<ILicense> licenses = LicenseSetFactory.emptyLicenseSet();

Expand All @@ -102,6 +103,7 @@ private static LicenseSetFactory readConfigFiles(Collection<URL> urls) {

LicenseReader lReader = fmt.licenseReader();
if (lReader != null) {
lReader.setLog(log);
lReader.addLicenses(url);
licenses.addAll(lReader.readLicenses());
lReader.approvedLicenseId().stream().map(ILicenseFamily::makeCategory).forEach(approvedLicenseIds::add);
Expand Down Expand Up @@ -241,10 +243,11 @@ public Builder noDefault() {

/**
* Builds the defaults object.
* @param log the Log to use to report errors when building the defaults.
* @return the current defaults object.
*/
public Defaults build() {
return new Defaults(fileNames);
public Defaults build(Log log) {
return new Defaults(log, fileNames);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
*/
package org.apache.rat;

/**
* An exception thrown when there is an issue with the Configuration.
*/
public class ImplementationException extends RuntimeException {

private static final long serialVersionUID = 7257245932787579431L;

public static ImplementationException makeInstance(Exception e) {
if (e instanceof ImplementationException) {
return (ImplementationException) e;
}
return new ImplementationException(e);
}

public ImplementationException(String message, Throwable cause) {
super(message, cause);
}

public ImplementationException(String message) {
super(message);
}

public ImplementationException(Throwable cause) {
super(cause);
}

}
86 changes: 67 additions & 19 deletions apache-rat-core/src/main/java/org/apache/rat/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -115,7 +116,7 @@ public class Report {
private static final String LIST_FAMILIES = "list-families";

private static final String LOG_LEVEL = "log-level";

private static final String DRY_RUN = "dry-run";
/**
* Set unstyled XML output
*/
Expand All @@ -130,6 +131,33 @@ public class Report {
* @throws Exception on error.
*/
public static void main(String[] args) throws Exception {
ReportConfiguration configuration = parseCommands(args, Report::printUsage);
if (configuration != null) {
configuration.validate(DefaultLog.INSTANCE::error);
new Reporter(configuration).output();
}
}

/**
* Parses the standard options to create a ReportConfiguraton.
* @param args the arguments to parse
* @param helpCmd the help command to run when necessary.
* @return a ReportConfiguration or null if Help was printed.
* @throws IOException on error.
*/
public static ReportConfiguration parseCommands(String[] args, Consumer<Options> helpCmd) throws IOException {
return parseCommands(args, helpCmd, false);
}

/**
* Parses the standard options to create a ReportConfiguraton.
* @param args the arguments to parse
* @param helpCmd the help command to run when necessary.
* @param noArgs If true then the commands do not need extra arguments
* @return a ReportConfiguration or null if Help was printed.
* @throws IOException on error.
*/
public static ReportConfiguration parseCommands(String[] args, Consumer<Options> helpCmd, boolean noArgs) throws IOException {
Options opts = buildOptions();
CommandLine cl;
try {
Expand All @@ -138,7 +166,7 @@ public static void main(String[] args) throws Exception {
DefaultLog.INSTANCE.error(e.getMessage());
DefaultLog.INSTANCE.error("Please use the \"--help\" option to see a list of valid commands and options");
System.exit(1);
return; // dummy return (won't be reached) to avoid Eclipse complaint about possible NPE
return null; // dummy return (won't be reached) to avoid Eclipse complaint about possible NPE
// for "cl"
}

Expand All @@ -152,14 +180,20 @@ public static void main(String[] args) throws Exception {
}
}
if (cl.hasOption(HELP)) {
printUsage(opts);
helpCmd.accept(opts);
return null;
}

args = cl.getArgs();
if (args == null || args.length != 1) {
printUsage(opts);
if (!noArgs) {
args = cl.getArgs();
if (args == null || args.length != 1) {
helpCmd.accept(opts);
return null;
}
} else {
ReportConfiguration configuration = createConfiguration(args[0], cl);
args = new String[] {null};
}
/* ReportConfiguration configuration = createConfiguration(args[0], cl);
configuration.validate(DefaultLog.INSTANCE::error);
boolean dryRun = false;
Expand All @@ -180,14 +214,25 @@ public static void main(String[] args) throws Exception {
}
if (!dryRun) {
Reporter.report(configuration);
new Reporter(configuration).output();
}
}
*/ ReportConfiguration configuration = createConfiguration(args[0], cl);
return configuration;
}

static ReportConfiguration createConfiguration(String baseDirectory, CommandLine cl) throws IOException {
final ReportConfiguration configuration = new ReportConfiguration(DefaultLog.INSTANCE);

configuration.setDryRun(cl.hasOption(DRY_RUN));
if (cl.hasOption(LIST_FAMILIES)) {
configuration.listFamilies( LicenseFilter.valueOf(cl.getOptionValue(LIST_FAMILIES).toLowerCase()));
}

if (cl.hasOption(LIST_LICENSES)) {
configuration.listFamilies( LicenseFilter.valueOf(cl.getOptionValue(LIST_LICENSES).toLowerCase()));
}

if (cl.hasOption('o')) {
configuration.setOut(new File(cl.getOptionValue('o')));
}
Expand Down Expand Up @@ -247,9 +292,11 @@ static ReportConfiguration createConfiguration(String baseDirectory, CommandLine
defaultBuilder.add(fn);
}
}
Defaults defaults = defaultBuilder.build();
Defaults defaults = defaultBuilder.build(DefaultLog.INSTANCE);
configuration.setFrom(defaults);
configuration.setReportable(getDirectory(baseDirectory, configuration));
if (baseDirectory != null) {
configuration.setReportable(getDirectory(baseDirectory, configuration));
}
return configuration;
}

Expand Down Expand Up @@ -288,7 +335,9 @@ static Options buildOptions() {
String licFilterValues = Arrays.stream(LicenseFilter.values()).map(LicenseFilter::name).collect(Collectors.joining(", "));

Options opts = new Options()

.addOption(Option.builder().longOpt(DRY_RUN)
.desc("If set do not update the files but generate the reports.")
.build())
.addOption(
Option.builder().hasArg(true).longOpt(LIST_FAMILIES)
.desc("List the defined license families (default is none). Valid options are: "+licFilterValues+".")
Expand All @@ -299,7 +348,6 @@ static Options buildOptions() {
.build())

.addOption(new Option(HELP, "help", false, "Print help for the RAT command line interface and exit."));


Option out = new Option("o", "out", true,
"Define the output file where to write a report to (default is System.out).");
Expand Down Expand Up @@ -368,13 +416,13 @@ static Options buildOptions() {
private static void printUsage(Options opts) {
HelpFormatter f = new HelpFormatter();
f.setOptionComparator(new OptionComparator());
String header = "\nAvailable options";
String header = System.lineSeparator()+"Available options";

String footer = "\nNOTE:\n" + "Rat is really little more than a grep ATM\n"
+ "Rat is also rather memory hungry ATM\n" + "Rat is very basic ATM\n"
+ "Rat highlights possible issues\n" + "Rat reports require interpretation\n"
+ "Rat often requires some tuning before it runs well against a project\n"
+ "Rat relies on heuristics: it may miss issues\n";
String footer = String.format("%nNOTE:%nRat is really little more than a grep ATM%n"
+ "Rat is also rather memory hungry ATM%n" + "Rat is very basic ATM%n"
+ "Rat highlights possible issues%n" + "Rat reports require interpretation%n"
+ "Rat often requires some tuning before it runs well against a project%n"
+ "Rat relies on heuristics: it may miss issues%n");

f.printHelp("java -jar apache-rat/target/apache-rat-CURRENT-VERSION.jar [options] [DIR|TARBALL]", header, opts,
footer, false);
Expand Down Expand Up @@ -420,7 +468,7 @@ private static IReportable getDirectory(String baseDirectory, ReportConfiguratio
/**
* This class implements the {@code Comparator} interface for comparing Options.
*/
private static class OptionComparator implements Comparator<Option>, Serializable {
public static class OptionComparator implements Comparator<Option>, Serializable {
/** The serial version UID. */
private static final long serialVersionUID = 5305467873966684014L;

Expand Down
Loading

0 comments on commit bea9d2c

Please sign in to comment.