Skip to content

Commit

Permalink
Fix for bad reporting of license status for non standard documents (e…
Browse files Browse the repository at this point in the history
….g. binary).

Collected license filtering into new LicenseSetFactory class and applied it where expected.
Added more tests to DefaultsTests
Added more tests to ReportConfigurtionTest
  • Loading branch information
Claudenw committed Oct 22, 2023
1 parent c96982b commit 6034936
Show file tree
Hide file tree
Showing 23 changed files with 619 additions and 308 deletions.
61 changes: 23 additions & 38 deletions apache-rat-core/src/main/java/org/apache/rat/Defaults.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;
import java.util.SortedSet;
Expand All @@ -35,6 +34,8 @@
import org.apache.rat.configuration.MatcherReader;
import org.apache.rat.license.ILicense;
import org.apache.rat.license.ILicenseFamily;
import org.apache.rat.license.LicenseSetFactory;
import org.apache.rat.license.LicenseSetFactory.LicenseFilter;

/**
* A class that holds the list of licenses and approved licences from one or more Configuration file.
Expand All @@ -54,15 +55,8 @@ public class Defaults {
*/
public static final String UNAPPROVED_LICENSES_STYLESHEET = "org/apache/rat/unapproved-licenses.xsl";

/**
* The set of defined licenses.
*/
private final SortedSet<ILicense> licenses;
/**
* The set of approved license ids.
*/
private final SortedSet<String> approvedLicenseIds;

private LicenseSetFactory setFactory;

/**
* Initialize the system configuration reader..
*/
Expand All @@ -76,9 +70,8 @@ public static void init() {
/**
* Builder constructs instances.
*/
private Defaults() {
licenses = new TreeSet<>(ILicense.getComparator());
approvedLicenseIds = new TreeSet<>();
private Defaults(Set<URL> urls) {
this.setFactory = Defaults.readConfigFiles(urls);
}

/**
Expand All @@ -89,19 +82,16 @@ public static Builder builder() {
return new Builder();
}

/**
* Clears all licenses and approved licenses.
*/
public void clear() {
licenses.clear();
approvedLicenseIds.clear();
}

/**
* Reads the configuration files.
* @param urls the URLS to read.
*/
private void readConfigFiles(Collection<URL> urls) {
private static LicenseSetFactory readConfigFiles(Collection<URL> urls) {

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

SortedSet<String> approvedLicenseIds = new TreeSet<String>();

for (URL url : urls) {
Format fmt = Format.fromURL(url);
MatcherReader mReader = fmt.matcherReader();
Expand All @@ -117,6 +107,7 @@ private void readConfigFiles(Collection<URL> urls) {
lReader.approvedLicenseId().stream().map(ILicenseFamily::makeCategory).forEach(approvedLicenseIds::add);
}
}
return new LicenseSetFactory(licenses, approvedLicenseIds);
}

/**
Expand All @@ -135,27 +126,23 @@ public static IOSupplier<InputStream> getUnapprovedLicensesStyleSheet() {
return () -> Defaults.class.getClassLoader().getResourceAsStream(Defaults.UNAPPROVED_LICENSES_STYLESHEET);
}

/**
* @return the sorted set of defined licenses.
*/
public SortedSet<ILicense> getLicenses() {
return Collections.unmodifiableSortedSet(licenses);
public SortedSet<ILicense> getLicenses(LicenseFilter filter) {
return setFactory.getLicenses(filter);
}

public SortedSet<ILicenseFamily> getLicenseFamilies(LicenseFilter filter) {
return setFactory.getLicenseFamilies(filter);
}

/**
* Gets the sorted set of approved license ids.
* If not licenses have been explicitly listed as approved all licenses are assumed to be approved.
* @return The sorted set of approved licenseIds.
*/
public SortedSet<String> getLicenseIds() {
if (approvedLicenseIds.isEmpty()) {
SortedSet<String> result = new TreeSet<>();
licenses.stream().map(x -> x.getLicenseFamily().getFamilyCategory()).forEach(result::add);
return result;
}
return Collections.unmodifiableSortedSet(approvedLicenseIds);
public SortedSet<String> getLicenseIds(LicenseFilter filter) {
return setFactory.getLicenseFamilyIds(filter);
}

/**
* The Defaults builder.
*/
Expand Down Expand Up @@ -247,9 +234,7 @@ public Builder noDefault() {
* Builds the defaults object.
*/
public Defaults build() {
Defaults result = new Defaults();
result.readConfigFiles(fileNames);
return result;
return new Defaults(fileNames);
}
}
}
2 changes: 1 addition & 1 deletion apache-rat-core/src/main/java/org/apache/rat/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
import org.apache.commons.io.filefilter.RegexFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.commons.lang3.StringUtils;
import org.apache.rat.ReportConfiguration.LicenseFilter;
import org.apache.rat.config.AddLicenseHeaders;
import org.apache.rat.license.ILicense;
import org.apache.rat.license.ILicenseFamily;
import org.apache.rat.license.LicenseSetFactory.LicenseFilter;
import org.apache.rat.report.IReportable;
import org.apache.rat.walker.ArchiveWalker;
import org.apache.rat.walker.DirectoryWalker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import org.apache.rat.config.AddLicenseHeaders;
import org.apache.rat.license.ILicense;
import org.apache.rat.license.ILicenseFamily;
import org.apache.rat.license.LicenseSetFactory;
import org.apache.rat.license.LicenseSetFactory.LicenseFilter;
import org.apache.rat.report.IReportable;

/**
Expand All @@ -46,19 +48,7 @@
* configuration and invoke the {@link Reporter}.
*/
public class ReportConfiguration {
/**
* An enum that defines the types of License filters.
*/
public enum LicenseFilter {
/** All defined licenses are returned */
all,
/** Only approved licenses are returned */
approved,
/** No licenses are returned */
none
}

private SortedSet<ILicense> licenses = new TreeSet<>(ILicense.getComparator());
private SortedSet<ILicense> licenses = LicenseSetFactory.emptyLicenseSet();
private SortedSet<String> approvedLicenseCategories = new TreeSet<>();
private SortedSet<String> removedLicenseCategories = new TreeSet<>();
private boolean addingLicenses;
Expand All @@ -69,7 +59,6 @@ public enum LicenseFilter {
private IOSupplier<InputStream> styleSheet = null;
private IReportable reportable = null;
private FilenameFilter inputFileFilter = null;
// private LicenseFilter approvalFilter = LicenseFilter.approved;

/**
* @return The filename filter for the potential input files.
Expand Down Expand Up @@ -121,8 +110,8 @@ public void setStyleSheet(IOSupplier<InputStream> styleSheet) {
* @param defaults The defaults to set.
*/
public void setFrom(Defaults defaults) {
addLicenses(defaults.getLicenses());
addApprovedLicenseCategories(defaults.getLicenseIds());
addLicenses(defaults.getLicenses(LicenseFilter.all));
addApprovedLicenseCategories(defaults.getLicenseIds(LicenseFilter.approved));
if (isStyleReport() && getStyleSheet() == null) {
setStyleSheet(Defaults.getPlainStyleSheet());
}
Expand Down Expand Up @@ -354,14 +343,10 @@ public SortedSet<ILicense> getLicenses(LicenseFilter filter) {
case all:
return Collections.unmodifiableSortedSet(licenses);
case approved:
SortedSet<String> approvedLicenses = getApprovedLicenseCategories();
SortedSet<ILicense> result = new TreeSet<>(ILicense.getComparator());
licenses.stream().filter(x -> approvedLicenses.contains(x.getLicenseFamily().getFamilyCategory()))
.forEach(result::add);
return result;
return new LicenseSetFactory(licenses, getApprovedLicenseCategories()).getLicenses(filter);
case none:
default:
return Collections.emptySortedSet();
return LicenseSetFactory.emptyLicenseSet();
}
}

Expand All @@ -378,9 +363,7 @@ public SortedSet<ILicense> getLicenses(LicenseFilter filter) {
* @return The set of defined licenses.
*/
public SortedSet<ILicenseFamily> getLicenseFamilies(LicenseFilter filter) {
SortedSet<ILicenseFamily> result = new TreeSet<>();
getLicenses(filter).stream().map(ILicense::getLicenseFamily).forEach(result::add);
return result;
return new LicenseSetFactory(licenses, getApprovedLicenseCategories()).getLicenseFamilies(filter);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,8 @@ public void read() throws RatHeaderAnalysisException {
if (license.finalizeState().asBoolean()) {
document.getMetaData().reportOnLicense(license);
} else {
final String notes = headers.toString();
final MetaData metaData = document.getMetaData();
metaData.set(new MetaData.Datum(MetaData.RAT_URL_HEADER_SAMPLE, notes));
metaData.set(new MetaData.Datum(MetaData.RAT_URL_HEADER_CATEGORY,
MetaData.RAT_LICENSE_FAMILY_CATEGORY_VALUE_UNKNOWN));
metaData.set(MetaData.RAT_LICENSE_FAMILY_NAME_DATUM_UNKNOWN);
document.getMetaData().reportOnLicense(UnknownLicense.INSTANCE);
document.getMetaData().set(new MetaData.Datum(MetaData.RAT_URL_HEADER_SAMPLE, headers.toString()));
}
} catch (IOException e) {
throw new RatHeaderAnalysisException("Cannot read header for " + document, e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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.analysis;

import org.apache.rat.license.ILicense;
import org.apache.rat.license.ILicenseFamily;
import org.apache.rat.license.ILicenseFamilyBuilder;

/**
* An ILicense implementation that represents an unknown license.
* <p>
* The UnknownLicense is used during processing to report that a document license can not be determined.
* </p>
*/
public class UnknownLicense implements ILicense {

/**
* The single instance of this class.
*/
static final UnknownLicense INSTANCE = new UnknownLicense();

private final ILicenseFamily family ;

/**
* Do not allow other constructions.
*/
private UnknownLicense() {
family = new ILicenseFamilyBuilder().setLicenseFamilyCategory("?????")
.setLicenseFamilyName("?????").build();
}

@Override
public String getId() {
return "?????";
}

@Override
public void reset() {
// do nothing
}

@Override
public State matches(String line) {
return State.f;
}

@Override
public State finalizeState() {
return State.f;
}

@Override
public State currentState() {
return State.f;
}

@Override
public int compareTo(ILicense arg0) {
return getLicenseFamily().compareTo(arg0.getLicenseFamily());
}

@Override
public ILicenseFamily getLicenseFamily() {
return family;
}

@Override
public String getNotes() {
return null;
}

@Override
public String derivedFrom() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,12 @@ public class MetaData {
public static final String RAT_URL_APPROVED_LICENSE = RAT_BASE_URL + "#ApprovedLicense";
public static final String RAT_APPROVED_LICENSE_VALUE_TRUE = Boolean.TRUE.toString();
public static final String RAT_APPROVED_LICENSE_VALUE_FALSE = Boolean.FALSE.toString();
public static final Datum RAT_APPROVED_LICENSE_DATIM_TRUE = new Datum(RAT_URL_APPROVED_LICENSE, RAT_APPROVED_LICENSE_VALUE_TRUE);
public static final Datum RAT_APPROVED_LICENSE_DATIM_FALSE = new Datum(RAT_URL_APPROVED_LICENSE, RAT_APPROVED_LICENSE_VALUE_FALSE);
public static final Datum RAT_APPROVED_LICENSE_DATUM_TRUE = new Datum(RAT_URL_APPROVED_LICENSE, RAT_APPROVED_LICENSE_VALUE_TRUE);
public static final Datum RAT_APPROVED_LICENSE_DATUM_FALSE = new Datum(RAT_URL_APPROVED_LICENSE, RAT_APPROVED_LICENSE_VALUE_FALSE);
@Deprecated
public static final Datum RAT_APPROVED_LICENSE_DATIM_TRUE = RAT_APPROVED_LICENSE_DATUM_TRUE;
@Deprecated
public static final Datum RAT_APPROVED_LICENSE_DATIM_FALSE = RAT_APPROVED_LICENSE_DATUM_FALSE;

/**
* Only likely to be a small quantity of data
Expand Down
Loading

0 comments on commit 6034936

Please sign in to comment.