Skip to content

Commit

Permalink
Update to filter *.json files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudenw committed Apr 26, 2024
1 parent da64737 commit f55d091
Show file tree
Hide file tree
Showing 17 changed files with 256 additions and 204 deletions.
17 changes: 17 additions & 0 deletions apache-rat-core/src/main/java/org/apache/rat/Defaults.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.rat;

import java.io.File;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
Expand All @@ -28,6 +29,9 @@
import java.util.SortedSet;
import java.util.TreeSet;

import org.apache.commons.io.IOCase;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.commons.io.function.IOSupplier;
import org.apache.rat.configuration.Format;
import org.apache.rat.configuration.LicenseReader;
Expand All @@ -37,6 +41,7 @@
import org.apache.rat.license.LicenseSetFactory;
import org.apache.rat.license.LicenseSetFactory.LicenseFilter;
import org.apache.rat.utils.Log;
import org.apache.rat.walker.NameBasedHiddenFileFilter;

/**
* A class that holds the list of licenses and approved licenses from one or more configuration files.
Expand All @@ -57,6 +62,10 @@ public class Defaults {
public static final String UNAPPROVED_LICENSES_STYLESHEET = "org/apache/rat/unapproved-licenses.xsl";

private final LicenseSetFactory setFactory;

private final FilenameFilter filesToIgnore = WildcardFileFilter.builder().setWildcards("*.json").setIoCase(IOCase.INSENSITIVE).get();

private final IOFileFilter directoriesToIgnore = NameBasedHiddenFileFilter.HIDDEN;

/**
* Initialize the system configuration reader..
Expand Down Expand Up @@ -155,6 +164,14 @@ public SortedSet<ILicenseFamily> getLicenseFamilies(LicenseFilter filter) {
public SortedSet<String> getLicenseIds(LicenseFilter filter) {
return setFactory.getLicenseFamilyIds(filter);
}

public FilenameFilter getFilesToIgnore() {
return filesToIgnore;
}

public IOFileFilter getDirectoriesToIgnore() {
return directoriesToIgnore;
}

/**
* The Defaults builder.
Expand Down
10 changes: 5 additions & 5 deletions apache-rat-core/src/main/java/org/apache/rat/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ static ReportConfiguration createConfiguration(String baseDirectory, CommandLine
}

if (cl.hasOption(SCAN_HIDDEN_DIRECTORIES)) {
configuration.setDirectoryFilter(null);
configuration.setDirectoriesToIgnore(null);
}

if (cl.hasOption('a') || cl.hasOption('A')) {
Expand All @@ -250,14 +250,14 @@ static ReportConfiguration createConfiguration(String baseDirectory, CommandLine
String[] excludes = cl.getOptionValues(EXCLUDE_CLI);
if (excludes != null) {
final FilenameFilter filter = parseExclusions(Arrays.asList(excludes));
configuration.setInputFileFilter(filter);
configuration.setFilesToIgnore(filter);
}
} else if (cl.hasOption(EXCLUDE_FILE_CLI)) {
String excludeFileName = cl.getOptionValue(EXCLUDE_FILE_CLI);
if (excludeFileName != null) {
final FilenameFilter filter = parseExclusions(
FileUtils.readLines(new File(excludeFileName), StandardCharsets.UTF_8));
configuration.setInputFileFilter(filter);
configuration.setFilesToIgnore(filter);
}
}

Expand Down Expand Up @@ -452,11 +452,11 @@ private static IReportable getDirectory(String baseDirectory, ReportConfiguratio
}

if (base.isDirectory()) {
return new DirectoryWalker(base, config.getInputFileFilter(), config.getDirectoryFilter());
return new DirectoryWalker(base, config.getFilesToIgnore(), config.getDirectoriesToIgnore());
}

try {
return new ArchiveWalker(base, config.getInputFileFilter());
return new ArchiveWalker(base, config.getFilesToIgnore());
} catch (IOException ex) {
config.getLog().log(Level.ERROR, "file '"+baseDirectory+"' is not valid gzip data.");
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import org.apache.rat.report.IReportable;
import org.apache.rat.utils.Log;
import org.apache.rat.utils.ReportingSet;
import org.apache.rat.walker.NameBasedHiddenFileFilter;

/**
* A configuration object is used by the front end to invoke the
Expand All @@ -69,9 +68,9 @@ public class ReportConfiguration {
private boolean styleReport;
private IOSupplier<InputStream> styleSheet;
private IReportable reportable;
private FilenameFilter inputFileFilter;
private IOFileFilter directoryFilter;
private Log log;
private FilenameFilter filesToIgnore;
private IOFileFilter directoriesToIgnore;
private final Log log;
private LicenseFilter listFamilies;
private LicenseFilter listLicenses;
private boolean dryRun;
Expand All @@ -89,7 +88,6 @@ public ReportConfiguration(Log log) {
.setMsgFormat( s -> String.format( "Duplicate License %s (%s) of type %s", s.getName(), s.getId(), s.getLicenseFamily().getFamilyCategory()));
approvedLicenseCategories = new TreeSet<>();
removedLicenseCategories = new TreeSet<>();
directoryFilter = NameBasedHiddenFileFilter.HIDDEN;
styleReport = true;
listFamilies = LicenseFilter.NONE;
listLicenses = LicenseFilter.NONE;
Expand Down Expand Up @@ -179,31 +177,31 @@ public boolean isDryRun() {
/**
* @return The filename filter for the potential input files.
*/
public FilenameFilter getInputFileFilter() {
return inputFileFilter;
public FilenameFilter getFilesToIgnore() {
return filesToIgnore;
}

/**
* @param inputFileFilter the filename filter to filter the input files.
* @param filesToIgnore the filename filter to filter the input files.
*/
public void setInputFileFilter(FilenameFilter inputFileFilter) {
this.inputFileFilter = inputFileFilter;
public void setFilesToIgnore(FilenameFilter filesToIgnore) {
this.filesToIgnore = filesToIgnore;
}

public IOFileFilter getDirectoryFilter() {
return directoryFilter;
public IOFileFilter getDirectoriesToIgnore() {
return directoriesToIgnore;
}

public void setDirectoryFilter(IOFileFilter directoryFilter) {
if (directoryFilter == null) {
this.directoryFilter = FalseFileFilter.FALSE;
public void setDirectoriesToIgnore(IOFileFilter directoriesToIgnore) {
if (directoriesToIgnore == null) {
this.directoriesToIgnore = FalseFileFilter.FALSE;
} else {
this.directoryFilter = directoryFilter;
this.directoriesToIgnore = directoriesToIgnore;
}
}

public void addDirectoryFilter(IOFileFilter directoryFilter) {
this.directoryFilter = this.directoryFilter.and(directoryFilter);
public void addDirectoryToIgnore(IOFileFilter directoryToIgnore) {
this.directoriesToIgnore = this.directoriesToIgnore.and(directoryToIgnore);
}

/**
Expand Down Expand Up @@ -247,6 +245,8 @@ public void setStyleSheet(IOSupplier<InputStream> styleSheet) {
* @param defaults The defaults to set.
*/
public void setFrom(Defaults defaults) {
setFilesToIgnore(defaults.getFilesToIgnore());
setDirectoriesToIgnore(defaults.getDirectoriesToIgnore());
addLicensesIfNotPresent(defaults.getLicenses(LicenseFilter.ALL));
addApprovedLicenseCategories(defaults.getLicenseIds(LicenseFilter.APPROVED));
if (isStyleReport() && getStyleSheet() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private final static class DefaultAnalyser implements IDocumentAnalyser {

/**
* Constructs a DocumentAnalyser for the specified license.
*
* @param log the Log to use
* @param license The license to analyse
*/
public DefaultAnalyser(final Log log, final Collection<ILicense> licenses) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@

package org.apache.rat.report.claim;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.rat.api.Document;

Expand All @@ -39,12 +41,12 @@ public enum Counter {
/** count of generated files */
GENERATED,
/** count of unknown files */
UNKNOWN };
UNKNOWN }

private final Map<String, int[]> licenseFamilyNameMap = new HashMap<>();
private final Map<String, int[]> licenseFamilyCodeMap = new HashMap<>();
private final Map<Document.Type, int[]> documentCategoryMap = new HashMap<>();
private final Map<ClaimStatistic.Counter, int[]> counterMap = new HashMap<>();
protected final Map<String, int[]> licenseFamilyNameMap = new HashMap<>();
protected final Map<String, int[]> licenseFamilyCodeMap = new HashMap<>();
protected final Map<Document.Type, int[]> documentCategoryMap = new HashMap<>();
protected final Map<ClaimStatistic.Counter, int[]> counterMap = new HashMap<>();


/**
Expand All @@ -57,45 +59,108 @@ public int getCounter(Counter counter) {
return count == null ? 0 : count[0];
}

/**
* @return Returns a map with the file types. The map keys
* are file type names and the map values
* are integers with the number of resources matching
* the file type.
*/
public Map<Counter, int[]> getCounterMap() {
return counterMap;
public void incCounter(Counter key, int value) {
final int[] num = counterMap.get(key);

if (num == null) {
counterMap.put(key, new int[] { value });
} else {
num[0] += value;
}
}


// /**
// * @return Returns a map with the file types. The map keys
// * are file type names and the map values
// * are integers with the number of resources matching
// * the file type.
// */
// public Map<Counter, int[]> getCounterMap() {
// return counterMap;
// }

/**
* @return Returns a map with the file types. The map keys
* are file type names and the map values
* are integers with the number of resources matching
* the file type.
* Returns the counts for the counter.
* @param documentType the document type to get the counter for.
* @return Returns the number of files with approved licenses.
*/
public Map<Document.Type, int[]> getDocumentCategoryMap() {
return documentCategoryMap;
public int getCounter(Document.Type documentType) {
int[] count = documentCategoryMap.get(documentType);
return count == null ? 0 : count[0];
}

/**
* @return Returns a map with the license family codes. The map
* keys are license family category names,
* the map values are integers with the number of resources
* matching the license family code.
*/
public Map<String, int[]> getLicenseFamilyCodeMap() {
return licenseFamilyCodeMap;
public void incCounter(Document.Type documentType, int value) {
final int[] num = documentCategoryMap.get(documentType);

if (num == null) {
documentCategoryMap.put(documentType, new int[] { value });
} else {
num[0] += value;
}
}
// /**
// * @return Returns a map with the file types. The map keys
// * are file type names and the map values
// * are integers with the number of resources matching
// * the file type.
// */
// public Map<Document.Type, int[]> getDocumentCategoryMap() {
// return documentCategoryMap;
// }

/**
* @return Returns a map with the license family codes. The map
* keys are the names of the license families and
* the map values are integers with the number of resources
* matching the license family name.
*/
public Map<String, int[]> getLicenseFileNameMap() {
return licenseFamilyNameMap;
// /**
// * @return Returns a map with the license family codes. The map
// * keys are license family category names,
// * the map values are integers with the number of resources
// * matching the license family code.
// */
// public Map<String, int[]> getLicenseFamilyCodeMap() {
// return licenseFamilyCodeMap;
// }

public int getLicenseFamilyCount(String licenseFamilyName) {
int[] count = licenseFamilyCodeMap.get(licenseFamilyName);
return count == null ? 0 : count[0];
}

public void incLicenseFamilyCount(String licenseFamilyName, int value) {
final int[] num = licenseFamilyCodeMap.get(licenseFamilyName);

if (num == null) {
licenseFamilyCodeMap.put(licenseFamilyName, new int[] { value });
} else {
num[0] += value;
}
}

public Set<String> getLicenseFamilyNames() {
return Collections.unmodifiableSet(licenseFamilyCodeMap.keySet());
}
// /**
// * @return Returns a map with the license family codes. The map
// * keys are the names of the license families and
// * the map values are integers with the number of resources
// * matching the license family name.
// */
// public Map<String, int[]> getLicenseFileNameMap() {
// return licenseFamilyNameMap;
// }
public Set<String> getLicenseFileNames() {
return Collections.unmodifiableSet(licenseFamilyNameMap.keySet());
}

public int getLicenseFileNameCount(String licenseFilename) {
int[] count = licenseFamilyNameMap.get(licenseFilename);
return count == null ? 0 : count[0];
}

public void incLicenseFileNameCount(String licenseFileNameName, int value) {
final int[] num = licenseFamilyNameMap.get(licenseFileNameName);

if (num == null) {
licenseFamilyNameMap.put(licenseFileNameName, new int[] { value });
} else {
num[0] += value;
}
}
}
Loading

0 comments on commit f55d091

Please sign in to comment.