From 2ad3ee841c0524c16b79129b4a4f84d408b1fd4a Mon Sep 17 00:00:00 2001 From: Lorenzo Coppi Date: Wed, 6 Mar 2024 10:31:47 +0100 Subject: [PATCH] refactor: use enums for table models columns --- .../model/LogsTableModel.java | 71 +++++++++++++------ .../model/RegexListViewerTableModel.java | 63 +++++++++++----- .../sensitivediscoverer/tab/LoggerTab.java | 16 ++--- 3 files changed, 101 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/cys4/sensitivediscoverer/model/LogsTableModel.java b/src/main/java/com/cys4/sensitivediscoverer/model/LogsTableModel.java index 253a664..bb1abb5 100644 --- a/src/main/java/com/cys4/sensitivediscoverer/model/LogsTableModel.java +++ b/src/main/java/com/cys4/sensitivediscoverer/model/LogsTableModel.java @@ -25,42 +25,27 @@ public int getRowCount() { @Override public int getColumnCount() { - return 3; + return Column.getSize(); } @Override public String getColumnName(int columnIndex) { - return switch (columnIndex) { - case 0 -> getLocaleString("common-url"); - case 1 -> getLocaleString("common-regex"); - case 2 -> getLocaleString("common-match"); - default -> ""; - }; - } - - public String getColumnNameFormatted(int columnIndex) { - return switch (columnIndex) { - case 0 -> "url"; - case 1 -> "regex"; - case 2 -> "match"; - default -> ""; - }; + return getLocaleString(Column.getById(columnIndex).localeKey); } @Override public Class getColumnClass(int columnIndex) { - return String.class; + return Column.getById(columnIndex).columnType; } @Override public Object getValueAt(int rowIndex, int columnIndex) { LogEntity logEntity = logEntries.get(rowIndex); - return switch (columnIndex) { - case 0 -> logEntity.getRequestResponse().finalRequest().url(); - case 1 -> logEntity.getRegexEntity().getDescription() + " - " + logEntity.getRegexEntity().getRegex(); - case 2 -> logEntity.getMatch(); - default -> ""; + return switch (Column.getById(columnIndex)) { + case URL -> logEntity.getRequestResponse().finalRequest().url(); + case REGEX -> logEntity.getRegexEntity().getDescription() + " - " + logEntity.getRegexEntity().getRegex(); + case MATCH -> logEntity.getMatch(); }; } @@ -71,4 +56,46 @@ public void addNewRow(int row) { public void clear() { fireTableDataChanged(); } + + /** + * Enum representing the columns of the table model for logs + */ + public enum Column { + URL("common-url", "url", String.class), + REGEX("common-regex", "regex", String.class), + MATCH("common-match", "match", String.class); + + private static final List columns = List.of(URL, REGEX, MATCH); + + private final String localeKey; + private final String formattedName; + private final Class columnType; + + Column(String localeKey, String formattedName, Class columnType) { + this.localeKey = localeKey; + this.formattedName = formattedName; + this.columnType = columnType; + } + + public static Column getById(int index) { + return columns.get(index); + } + + /** + * Returns the number of elements in this enum + * + * @return the number of elements in this enum + */ + public static int getSize() { + return columns.size(); + } + + public String getNameFormatted() { + return formattedName; + } + + public int getIndex() { + return columns.indexOf(this); + } + } } diff --git a/src/main/java/com/cys4/sensitivediscoverer/model/RegexListViewerTableModel.java b/src/main/java/com/cys4/sensitivediscoverer/model/RegexListViewerTableModel.java index 8066d73..72b4993 100644 --- a/src/main/java/com/cys4/sensitivediscoverer/model/RegexListViewerTableModel.java +++ b/src/main/java/com/cys4/sensitivediscoverer/model/RegexListViewerTableModel.java @@ -24,43 +24,33 @@ public int getRowCount() { @Override public int getColumnCount() { - return 4; + return Column.getSize(); } @Override public String getColumnName(int columnIndex) { - return switch (columnIndex) { - case 0 -> getLocaleString("common-active"); - case 1 -> getLocaleString("common-regex"); - case 2 -> getLocaleString("common-description"); - case 3 -> getLocaleString("common-sections"); - default -> ""; - }; + return getLocaleString(Column.getById(columnIndex).localeKey); } @Override public Class getColumnClass(int columnIndex) { - if (columnIndex == 0) { - return Boolean.class; - } - return String.class; + return Column.getById(columnIndex).columnType; } @Override public boolean isCellEditable(int row, int column) { - return column == 0; + return Column.getById(column).editable; } @Override public Object getValueAt(int rowIndex, int columnIndex) { RegexEntity regexEntry = regexList.get(rowIndex); - return switch (columnIndex) { - case 0 -> regexEntry.isActive(); - case 1 -> regexEntry.getRegex(); - case 2 -> regexEntry.getDescription(); - case 3 -> regexEntry.getSectionsHumanReadable(); - default -> ""; + return switch (Column.getById(columnIndex)) { + case ACTIVE -> regexEntry.isActive(); + case REGEX -> regexEntry.getRegex(); + case DESCRIPTION -> regexEntry.getDescription(); + case SECTIONS -> regexEntry.getSectionsHumanReadable(); }; } @@ -70,4 +60,39 @@ public void setValueAt(Object value, int rowIndex, int columnIndex) { regexEntry.setActive((Boolean) value); fireTableCellUpdated(rowIndex, columnIndex); } + + /** + * Enum representing the columns of the table model for regex lists + */ + public enum Column { + ACTIVE("common-active", true, Boolean.class), + REGEX("common-regex", false, String.class), + DESCRIPTION("common-description", false, String.class), + SECTIONS("common-sections", false, String.class); + + private static final List columns = List.of(ACTIVE, REGEX, DESCRIPTION, SECTIONS); + + private final String localeKey; + private final boolean editable; + private final Class columnType; + + Column(String localeKey, boolean editable, Class columnType) { + this.localeKey = localeKey; + this.editable = editable; + this.columnType = columnType; + } + + public static Column getById(int index) { + return columns.get(index); + } + + /** + * Returns the number of elements in this enum + * + * @return the number of elements in this enum + */ + public static int getSize() { + return columns.size(); + } + } } diff --git a/src/main/java/com/cys4/sensitivediscoverer/tab/LoggerTab.java b/src/main/java/com/cys4/sensitivediscoverer/tab/LoggerTab.java index 2a5a999..76b07b5 100644 --- a/src/main/java/com/cys4/sensitivediscoverer/tab/LoggerTab.java +++ b/src/main/java/com/cys4/sensitivediscoverer/tab/LoggerTab.java @@ -322,13 +322,13 @@ private JToggleButton createExportLogsButton() { java.util.List lines = new ArrayList<>(); lines.add(String.format("\"%s\",\"%s\"", - logsTableModel.getColumnNameFormatted(0), - logsTableModel.getColumnNameFormatted(2))); + LogsTableModel.Column.URL.getNameFormatted(), + LogsTableModel.Column.MATCH.getNameFormatted())); // values for (int i = 0; i < logsTableModel.getRowCount(); i++) { - String url = logsTableModel.getValueAt(i, 0).toString(); - String matchEscaped = logsTableModel.getValueAt(i, 2).toString().replaceAll("\"", "\"\""); + String url = logsTableModel.getValueAt(i, LogsTableModel.Column.URL.getIndex()).toString(); + String matchEscaped = logsTableModel.getValueAt(i, LogsTableModel.Column.MATCH.getIndex()).toString().replaceAll("\"", "\"\""); lines.add(String.format("\"%s\",\"%s\"", url, matchEscaped)); } @@ -340,14 +340,14 @@ private JToggleButton createExportLogsButton() { itemToJSON.addActionListener(actionEvent -> { java.util.List lines = new ArrayList<>(); - String prop1 = logsTableModel.getColumnNameFormatted(0); - String prop2 = logsTableModel.getColumnNameFormatted(2); + String prop1 = LogsTableModel.Column.URL.getNameFormatted(); + String prop2 = LogsTableModel.Column.MATCH.getNameFormatted(); // values for (int i = 0; i < logsTableModel.getRowCount(); i++) { JsonObject obj = new JsonObject(); - obj.addProperty(prop1, logsTableModel.getValueAt(i, 0).toString()); - obj.addProperty(prop2, logsTableModel.getValueAt(i, 2).toString()); + obj.addProperty(prop1, logsTableModel.getValueAt(i, LogsTableModel.Column.URL.getIndex()).toString()); + obj.addProperty(prop2, logsTableModel.getValueAt(i, LogsTableModel.Column.MATCH.getIndex()).toString()); lines.add(obj); }