Skip to content

Commit

Permalink
Fix: code optimization export Logs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidebresaola committed Mar 20, 2024
1 parent 80879f9 commit df49ea3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
33 changes: 17 additions & 16 deletions src/main/java/com/cys4/sensitivediscoverer/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static void saveToFile(String extensionName, List<String> lines) {
*
* @param extensionNames the extensions to filter files
* @param openFile Set to true if the file should be opened, false if it should be saved
* @return The filename, or null if there was an error
* @return The filename, or empty string if there was an error
*/
public static String selectFile(List<String> extensionNames, boolean openFile) {
JFrame parentFrame = new JFrame();
Expand Down Expand Up @@ -190,7 +190,7 @@ public static InputStream getResourceAsStream(String name) {
return Utils.class.getClassLoader().getResourceAsStream(name);
}

private static void writeLinesToFile(String fileName, List<String> lines) {
public static void writeLinesToFile(String fileName, List<String> lines) {
try {
PrintWriter pwt = new PrintWriter(fileName, StandardCharsets.UTF_8);
lines.forEach(pwt::println);
Expand All @@ -200,6 +200,15 @@ private static void writeLinesToFile(String fileName, List<String> lines) {
}
}

private static List<String> readLinesFromFile(String fileName) {
try {
return Files.readAllLines(Path.of(fileName));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

public static void saveListToCSV(String csvFile, List<RegexEntity> regexEntities) {
List<String> lines = new ArrayList<>();

Expand Down Expand Up @@ -239,13 +248,9 @@ public static void saveListToJSON(String jsonFile, List<RegexEntity> regexEntiti

public static void openListFromCSV(String csvFile, RegexListContext ctx) {
StringBuilder alreadyAddedMsg = new StringBuilder();
List<String> lines;
try {
lines = Files.readAllLines(Path.of(csvFile));
} catch (IOException e) {
e.printStackTrace();
return;
}

List<String> lines = readLinesFromFile(csvFile);
if (Objects.isNull(lines)) return;

//Skip header line if present
int startRow = (lines.get(0).contains("\"description\",\"regex\"")) ? 1 : 0;
Expand Down Expand Up @@ -287,13 +292,9 @@ public static void openListFromCSV(String csvFile, RegexListContext ctx) {
public static void openListFromJSON(String jsonFile, RegexListContext ctx) {
Gson gson = new Gson();
StringBuilder alreadyAddedMsg = new StringBuilder();
List<String> lines;
try {
lines = Files.readAllLines(Path.of(jsonFile));
} catch (IOException e) {
e.printStackTrace();
return;
}

List<String> lines = readLinesFromFile(jsonFile);
if (Objects.isNull(lines)) return;

Type tArrayListRegexEntity = new TypeToken<ArrayList<JsonRegexEntity>>() {
}.getType();
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/cys4/sensitivediscoverer/tab/LoggerTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ private JToggleButton createExportLogsButton() {

JMenuItem itemToCSV = new JMenuItem(getLocaleString("common-toCSV"));
itemToCSV.addActionListener(actionEvent -> {
String csvFile = Utils.selectFile(List.of("CSV"), false);
if (csvFile.isBlank()) return;

java.util.List<String> lines = new ArrayList<>();

lines.add(String.format("\"%s\",\"%s\"",
Expand All @@ -332,12 +335,15 @@ private JToggleButton createExportLogsButton() {
lines.add(String.format("\"%s\",\"%s\"", url, matchEscaped));
}

Utils.saveToFile("csv", lines);
Utils.writeLinesToFile(csvFile, lines);
});
menu.add(itemToCSV);

JMenuItem itemToJSON = new JMenuItem(getLocaleString("common-toJSON"));
itemToJSON.addActionListener(actionEvent -> {
String jsonFile = Utils.selectFile(List.of("JSON"), false);
if (jsonFile.isBlank()) return;

java.util.List<JsonObject> lines = new ArrayList<>();

String prop1 = logsTableModel.getColumnNameFormatted(0);
Expand All @@ -355,7 +361,8 @@ private JToggleButton createExportLogsButton() {
Gson gson = builder.create();
Type tListEntries = new TypeToken<ArrayList<JsonObject>>() {
}.getType();
Utils.saveToFile("json", List.of(gson.toJson(lines, tListEntries)));

Utils.writeLinesToFile(jsonFile, List.of(gson.toJson(lines, tListEntries)));
});
menu.add(itemToJSON);

Expand Down

0 comments on commit df49ea3

Please sign in to comment.