From b8f061b5914c1f08ba733e0cfac5c176dbb4c98f Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Sat, 18 Nov 2023 17:09:37 +0100 Subject: [PATCH] Fixed sonarcloud issues. --- cli/src/main/java/de/jplag/cli/CLI.java | 20 +++++++++++-------- .../de/jplag/reporting/csv/CsvPrinter.java | 2 +- .../csv/comparisons/CsvComparisonOutput.java | 3 +++ .../csv/comparisons/NameMapperRandomIds.java | 5 +++-- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/cli/src/main/java/de/jplag/cli/CLI.java b/cli/src/main/java/de/jplag/cli/CLI.java index 3e016f85c..c923ae8f9 100644 --- a/cli/src/main/java/de/jplag/cli/CLI.java +++ b/cli/src/main/java/de/jplag/cli/CLI.java @@ -81,14 +81,7 @@ public static void main(String[] args) { ReportObjectFactory reportObjectFactory = new ReportObjectFactory(); reportObjectFactory.createAndSaveReport(result, cli.getResultFolder()); - if (cli.options.csv.print) { - try { - CsvComparisonOutput.writeCsvResults(result.getAllComparisons(), cli.options.csv.anonymize, new File(cli.getResultFolder()), - cli.options.csv.fileName); - } catch (IOException e) { - logger.error("Could not write csv", e); - } - } + cli.printCsv(result); } } catch (ExitException exception) { logger.error(exception.getMessage()); // do not pass exception here to keep log clean @@ -97,6 +90,17 @@ public static void main(String[] args) { } } + private void printCsv(JPlagResult result) { + if (options.csv.print) { + try { + CsvComparisonOutput.writeCsvResults(result.getAllComparisons(), options.csv.anonymize, new File(getResultFolder()), + options.csv.fileName); + } catch (IOException e) { + logger.error("Could not write csv", e); + } + } + } + /** * Creates a new instance */ diff --git a/core/src/main/java/de/jplag/reporting/csv/CsvPrinter.java b/core/src/main/java/de/jplag/reporting/csv/CsvPrinter.java index 9110bdcb9..ee4fe1224 100644 --- a/core/src/main/java/de/jplag/reporting/csv/CsvPrinter.java +++ b/core/src/main/java/de/jplag/reporting/csv/CsvPrinter.java @@ -114,7 +114,7 @@ private void printCell(Writer writer, String cellValue) throws IOException { String actualValue = cellValue; if (literalsNeeded) { writer.write(LITERAL); - actualValue = actualValue.replaceAll("\"", "\"\""); + actualValue = actualValue.replace("\"", "\"\""); } writer.write(actualValue); if (literalsNeeded) { diff --git a/core/src/main/java/de/jplag/reporting/csv/comparisons/CsvComparisonOutput.java b/core/src/main/java/de/jplag/reporting/csv/comparisons/CsvComparisonOutput.java index da4200b46..26cab1435 100644 --- a/core/src/main/java/de/jplag/reporting/csv/comparisons/CsvComparisonOutput.java +++ b/core/src/main/java/de/jplag/reporting/csv/comparisons/CsvComparisonOutput.java @@ -18,6 +18,9 @@ public class CsvComparisonOutput { private static final String[] titles = new String[] {"submissionName1", "submissionName2", "averageSimilarity", "maxSimilarity"}; + private CsvComparisonOutput() { + } + /** * Writes the comparisons as a csv * @param comparisons The list of comparisons diff --git a/core/src/main/java/de/jplag/reporting/csv/comparisons/NameMapperRandomIds.java b/core/src/main/java/de/jplag/reporting/csv/comparisons/NameMapperRandomIds.java index a9a613d84..eebe9621f 100644 --- a/core/src/main/java/de/jplag/reporting/csv/comparisons/NameMapperRandomIds.java +++ b/core/src/main/java/de/jplag/reporting/csv/comparisons/NameMapperRandomIds.java @@ -1,5 +1,6 @@ package de.jplag.reporting.csv.comparisons; +import java.security.SecureRandom; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -17,11 +18,11 @@ public class NameMapperRandomIds implements NameMapper { */ public NameMapperRandomIds() { this.map = new HashMap<>(); - this.random = new Random(); + this.random = new SecureRandom(); } private String newId() { - String id = String.valueOf(Math.abs(random.nextInt())); + String id = String.valueOf(this.random.nextInt(0, Integer.MAX_VALUE)); if (this.map.containsKey(id)) { return newId();