Skip to content
This repository has been archived by the owner on Dec 20, 2022. It is now read-only.

Commit

Permalink
Merge pull request #133 from pfistererm/mp_config_file_encoding
Browse files Browse the repository at this point in the history
Added way to specify the encoding of configuration files
  • Loading branch information
pfistererm committed Jan 14, 2021
2 parents d47764a + c6f2379 commit ba320db
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,17 @@ public class ExecutionParameters {
*/
private String messageDigestAlgorithm;

/**
* Encoding of the configuration files.
*
* <p>A JAVA encoding name to use for reading the configuration files.
* If not configured, the value of 'project.build.sourceEncoding' is used.
* If this is not configured, too, the platform specific default encoding is used.</p>
*
* @since 1.4.0
*/
private String configurationFileEncoding;

/**
* @return the archiveType
*/
Expand Down Expand Up @@ -667,4 +678,18 @@ public final void setMessageDigestAlgorithm(String messageDigestAlgorithm) {
this.messageDigestAlgorithm = messageDigestAlgorithm;
}

/**
* @return the configurationFileEncoding
*/
public final String getConfigurationFileEncoding() {
return configurationFileEncoding;
}

/**
* @param configurationFileEncoding the configurationFileEncoding to set
*/
public final void setConfigurationFileEncoding(String configurationFileEncoding) {
this.configurationFileEncoding = configurationFileEncoding;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
import org.aposin.licensescout.configuration.ConfigFileHandler;
import org.aposin.licensescout.configuration.ExecutionDatabaseConfiguration;
import org.aposin.licensescout.configuration.ExecutionOutput;
import org.aposin.licensescout.configuration.OutputFileType;
import org.aposin.licensescout.configuration.FinderParameters;
import org.aposin.licensescout.configuration.OutputFileType;
import org.aposin.licensescout.database.DatabaseWriter;
import org.aposin.licensescout.exporter.GeneralStatistics;
import org.aposin.licensescout.exporter.IDetectionStatusStatistics;
Expand Down Expand Up @@ -140,6 +140,7 @@ public void execute() throws LicenseScoutExecutionException, LicenseScoutFailOnE
final Notices notices = readNotices(getConfigFileHandler(), getLog());
final LicenseStoreData licenseStoreData = init(notices, getLog());

getLog().info("Using configuration file encoding: " + getConfigurationFileEncoding());
final GlobalFilters globalFilters = readGlobalFilters(getLog());
final LicenseCheckedList checkedArchives = readCheckedArchives(licenseStoreData, notices, providers, getLog());
final List<License> cleanOutputLicenses = createCleanOutputLicenseList(getLog(), licenseStoreData);
Expand Down Expand Up @@ -540,7 +541,7 @@ protected void readLicenseUrlMappings(final LicenseStoreData licenseStoreData, f
throws LicenseScoutExecutionException {
try (final InputStream inputStream = getConfigFileHandler().getLicenseUrlMappingsInputStream()) {
if (inputStream != null) {
licenseStoreData.readUrlMappings(inputStream, log);
licenseStoreData.readUrlMappings(inputStream, getConfigurationFileEncoding(), log);
}
} catch (IOException e) {
throw new LicenseScoutExecutionException("cannot read license URL mappings", e);
Expand All @@ -556,7 +557,7 @@ protected void readLicenseNameMappings(final LicenseStoreData licenseStoreData,
throws LicenseScoutExecutionException {
try (final InputStream inputStream = getConfigFileHandler().getLicenseNameMappingsInputStream()) {
if (inputStream != null) {
licenseStoreData.readNameMappings(inputStream, log);
licenseStoreData.readNameMappings(inputStream, getConfigurationFileEncoding(), log);
}
} catch (IOException e) {
throw new LicenseScoutExecutionException("cannot read license name mappings", e);
Expand All @@ -572,7 +573,7 @@ protected GlobalFilters readGlobalFilters(final ILSLog log) throws LicenseScoutE
final GlobalFilters globalFilters = new GlobalFilters();
try (final InputStream inputStream = getConfigFileHandler().getGlobalFiltersInputStream()) {
if (inputStream != null) {
globalFilters.read(inputStream);
globalFilters.read(inputStream, getConfigurationFileEncoding());
}
} catch (IOException e) {
throw new LicenseScoutExecutionException("cannot read global filters", e);
Expand Down Expand Up @@ -659,7 +660,8 @@ protected LicenseCheckedList readCheckedArchives(final LicenseStoreData licenseS
final LicenseCheckedList checkedArchives = new LicenseCheckedList();
try (final InputStream inputStream = getConfigFileHandler().getCheckedArchivesInputStream()) {
if (inputStream != null) {
checkedArchives.readCsv(inputStream, licenseStoreData, providers, notices, log);
checkedArchives.readCsv(inputStream, getConfigurationFileEncoding(), licenseStoreData, providers,
notices, log);
}
} catch (IOException e) {
throw new LicenseScoutExecutionException("cannot read check archives list", e);
Expand All @@ -680,7 +682,8 @@ protected List<String> readAndCollectFilteredVendorNames(final ILSLog log) throw
}
try (final InputStream inputStream = getConfigFileHandler().getFilteredVendorNamesInputStream()) {
if (inputStream != null) {
final List<String> tmpResultList = readFilteredVendorNamesFromFile(inputStream, log);
final List<String> tmpResultList = readFilteredVendorNamesFromFile(inputStream,
getConfigurationFileEncoding(), log);
resultFilteredVendorNames.addAll(tmpResultList);
}
} catch (IOException e) {
Expand All @@ -697,12 +700,13 @@ protected List<String> readAndCollectFilteredVendorNames(final ILSLog log) throw
* @return a list of strings
* @throws IOException if an error occurred while reading from the file
*/
protected static List<String> readFilteredVendorNamesFromFile(final InputStream inputStream, final ILSLog log)
protected static List<String> readFilteredVendorNamesFromFile(final InputStream inputStream, String encoding,
final ILSLog log)
throws IOException {
final List<String> resultList = new ArrayList<>();
String line = "";

try (final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
try (final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, encoding))) {
while ((line = br.readLine()) != null) {

// ignore lines commented out
Expand Down Expand Up @@ -752,4 +756,8 @@ private static boolean isSnapshotVersion(final String version) {
return version.endsWith("-SNAPSHOT");
}

private String getConfigurationFileEncoding() {
return getExecutionParameters().getConfigurationFileEncoding();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ public GlobalFilters() {
* Reads global filters from a file.
*
* @param inputStream an input stream to read the file contents from
* @param encoding an encoding name to use for the input file
* @throws IOException if an error occurs
*/
public void read(final InputStream inputStream) throws IOException {
public void read(final InputStream inputStream, String encoding) throws IOException {
String line = "";

try (final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"))) {
try (final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, encoding))) {
while ((line = br.readLine()) != null) {

// ignore lines commented out
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ public LicenseCheckedList() {
* Reads a CSV file containing checked archives.
*
* @param inputStream an input stream to read the file contents from
* @param encoding an encoding name to use for the input file
* @param licenseStoreData the data object containing information on licenses
* @param providers the data object containing information on providers
* @param notices the data object containing information on notices
* @param log the logger
* @throws IOException if an error occurred while reading from the file
*/
public void readCsv(final InputStream inputStream, final LicenseStoreData licenseStoreData,
public void readCsv(final InputStream inputStream,String encoding, final LicenseStoreData licenseStoreData,
final Providers providers, final Notices notices, final ILSLog log)
throws IOException {
/*
Expand All @@ -110,7 +111,7 @@ public void readCsv(final InputStream inputStream, final LicenseStoreData licens
String line = "";
String cvsSplitBy = ",";
int lineNumber = 0;
try (final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
try (final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, encoding))) {

while ((line = br.readLine()) != null) {
lineNumber++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,13 @@ public void readLicenses(final InputStream inputStream, final Notices notices, b
* Reads license URL mappings from a CSV file.
*
* @param inputStream an input stream to read the file contents from
* @param encoding an encoding name to use for the input file
* @param log the logger
* @throws IOException
*/
public void readUrlMappings(final InputStream inputStream, final ILSLog log) throws IOException {
public void readUrlMappings(final InputStream inputStream,String encoding, final ILSLog log) throws IOException {
final CSVFormat csvFormat = CSVFormat.DEFAULT.withDelimiter(',').withCommentMarker('#');
try (final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"))) {
try (final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, encoding))) {
final CSVParser csvParser = csvFormat.parse(br);
for (final CSVRecord record : csvParser) {
final String url = record.get(0).trim();
Expand All @@ -218,12 +219,13 @@ public void readUrlMappings(final InputStream inputStream, final ILSLog log) thr
* Reads license name mappings from a CSV file.
*
* @param inputStream an input stream to read the file contents from
* @param encoding an encoding name to use for the input file
* @param log the logger
* @throws IOException
*/
public void readNameMappings(final InputStream inputStream, final ILSLog log) throws IOException {
public void readNameMappings(final InputStream inputStream, String encoding, final ILSLog log) throws IOException {
final CSVFormat csvFormat = CSVFormat.DEFAULT.withDelimiter(',').withCommentMarker('#');
try (final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"))) {
try (final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, encoding))) {
final CSVParser csvParser = csvFormat.parse(br);
for (final CSVRecord record : csvParser) {
final String mappedName = record.get(0).trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ private ExecutionParameters createExecutionParameters(final ArchiveType archiveT
executionParameters.setValidateLicenseXml(false);
executionParameters.setArtifactServerUtil(new TestArtifactServerUtil());
executionParameters.setMessageDigestAlgorithm(CryptUtil.DEFAULT_MD_ALGORITHM);
executionParameters.setConfigurationFileEncoding("UTF-8");
return executionParameters;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void assertReadCsv(final String messageDigestAlgorithmName, final String
final Notices notices = new Notices();
final LicenseCheckedList checkedArchives = new LicenseCheckedList();
try (final InputStream inputStream = new FileInputStream(CONFIGURATION_BASE_PATH + checkedArchivesFilename)) {
checkedArchives.readCsv(inputStream, licenseStoreData, providers, notices, log);
checkedArchives.readCsv(inputStream, "UTF-8", licenseStoreData, providers, notices, log);
}
checkArchiveLicensesVersion(checkedArchives, ArchiveType.JAVA, "not_existing", "not_existing", false,
LicenseProcessingMode.OVERWRITE_NORMAL, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static LicenseCheckedList createLicenseCheckedList(final LicenseStoreData
final LicenseCheckedList checkedArchives = new LicenseCheckedList();
final File checkedArchivesPathname = new File(CHECKEDARCHIVES_PATH);
try (final InputStream inputStream = new FileInputStream(checkedArchivesPathname)) {
checkedArchives.readCsv(inputStream, licenseStoreData, new Providers(), new Notices(), new NullLog());
checkedArchives.readCsv(inputStream, "UTF-8", licenseStoreData, new Providers(), new Notices(), new NullLog());
}
return checkedArchives;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,18 @@ public abstract class AbstractScanMojo extends AbstractMojo implements IReposito
@Parameter(defaultValue = CryptUtil.DEFAULT_MD_ALGORITHM, property = "messageDigestAlgorithm", required = false)
private String messageDigestAlgorithm;

/**
* Encoding of the configuration files.
*
* <p>A JAVA encoding name to use for reading the configuration files.
* If not configured, the value of 'project.build.sourceEncoding' is used.
* If this is not configured, too, the platform specific default encoding is used.</p>
*
* @since 1.4.0
*/
@Parameter(property = "configurationFileEncoding", required = false, defaultValue = "${project.build.sourceEncoding}")
private String configurationFileEncoding;

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -586,15 +598,13 @@ public final boolean isShowDocumentationUrlColumn() {
return showDocumentationUrlColumn;
}


/**
* @return the showLicenseCandidateFilesColumn
*/
public final boolean isShowLicenseCandidateFilesColumn() {
return showLicenseCandidateFilesColumn;
}


/**
* @return the showProviderColumn
*/
Expand Down Expand Up @@ -885,6 +895,13 @@ public String getMessageDigestAlgorithm() {
return messageDigestAlgorithm;
}

/**
* @return the configFileEncoding
*/
public final String getConfigurationFileEncoding() {
return configurationFileEncoding;
}

/**
* Obtains an artifact description for use in an attach operation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class Output {
private File template;

/**
* Encoding of the output file.
* Encoding of the template file.
*
* <p>A JAVA encoding name to use for reading the template file.
* If not configured, the value of 'project.build.sourceEncoding' is used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,18 @@ public abstract class AbstractReportMojo extends AbstractMavenReport implements
@Parameter(defaultValue = CryptUtil.DEFAULT_MD_ALGORITHM, property = "messageDigestAlgorithm", required = false)
private String messageDigestAlgorithm;

/**
* Encoding of the configuration files.
*
* <p>A JAVA encoding name to use for reading the configuration files.
* If not configured, the value of 'project.build.sourceEncoding' is used.
* If this is not configured, too, the platform specific default encoding is used.</p>
*
* @since 1.4.0
*/
@Parameter(property = "configurationFileEncoding", required = false, defaultValue = "${project.build.sourceEncoding}")
private String configurationFileEncoding;

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -442,15 +454,13 @@ public final boolean isShowDocumentationUrlColumn() {
return showDocumentationUrlColumn;
}


/**
* @return the showLicenseCandidateFilesColumn
*/
public final boolean isShowLicenseCandidateFilesColumn() {
return showLicenseCandidateFilesColumn;
}


/**
* @return the showProviderColumn
*/
Expand Down Expand Up @@ -634,4 +644,11 @@ public String getMessageDigestAlgorithm() {
return messageDigestAlgorithm;
}

/**
* @return the configurationFileEncoding
*/
public final String getConfigurationFileEncoding() {
return configurationFileEncoding;
}

}

0 comments on commit ba320db

Please sign in to comment.