From 1a3d988c4f2d949a8e9326f8400a88f695fc0269 Mon Sep 17 00:00:00 2001 From: u228298 Date: Mon, 9 Dec 2024 13:54:18 +0100 Subject: [PATCH] feat: add validation strategy REMOVE_DOTS_AND_REPLACE_WHITESPACE This option is deprecated because specialized treatment of the IDs should ideally be performed upstream of the converter. This option is provided only to ensure backward compatibility with the old default behavior. Users are encouraged to perform ID manipulations before invoking the converter, preferably directly in the Network Graphic Editor (NGE). --- .../core/validation/NetworkGraphicValidator.java | 6 ++++++ .../core/validation/ValidationStrategy.java | 16 ++++++++++++++++ .../core/validation/ValidationUtils.java | 7 +++++++ .../validation/NetworkGraphicValidatorTest.java | 16 +++++++++++----- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/core/validation/NetworkGraphicValidator.java b/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/core/validation/NetworkGraphicValidator.java index f3b00b4..c935ee1 100644 --- a/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/core/validation/NetworkGraphicValidator.java +++ b/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/core/validation/NetworkGraphicValidator.java @@ -67,6 +67,12 @@ private void validateId(String id, IssueTarget target, Identifiable object) { } } + void removeDotsReplaceWhitespace() { + log.info("Remove dots and replace whitespace in invalid IDs of network graphic"); + networkGraphic = new NetworkGraphicSanitizer(networkGraphic, considerTrainruns, + ValidationUtils::removeDotsReplaceWhitespace).run(); + } + void replaceWhitespace() { log.info("Strip and replace whitespace in invalid IDs of network graphic"); networkGraphic = new NetworkGraphicSanitizer(networkGraphic, considerTrainruns, diff --git a/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/core/validation/ValidationStrategy.java b/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/core/validation/ValidationStrategy.java index 7db2fe3..0bb32af 100644 --- a/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/core/validation/ValidationStrategy.java +++ b/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/core/validation/ValidationStrategy.java @@ -45,6 +45,22 @@ public boolean apply(NetworkGraphicValidator validator) { } return true; } + }, + + /** + * @deprecated This option is deprecated because specialized treatment of the IDs should ideally be performed + * upstream of the converter. This option is provided only to ensure backward compatibility with the old default + * behavior. Users are encouraged to perform ID manipulations before invoking the converter, preferably directly in + * the Network Graphic Editor (NGE). + */ + @Deprecated REMOVE_DOTS_AND_REPLACE_WHITESPACE { + @Override + public boolean apply(NetworkGraphicValidator validator) { + if (!validator.isValid()) { + validator.removeDotsReplaceWhitespace(); + } + return true; + } }; public abstract boolean apply(NetworkGraphicValidator validator); diff --git a/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/core/validation/ValidationUtils.java b/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/core/validation/ValidationUtils.java index 2a6ba09..fb17aba 100644 --- a/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/core/validation/ValidationUtils.java +++ b/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/core/validation/ValidationUtils.java @@ -6,9 +6,11 @@ class ValidationUtils { private static final Pattern SPECIAL_CHAR_PATTERN = Pattern.compile("[^a-zA-Z0-9_\\-\\s]"); private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+"); + private static final Pattern DOT_PATTERN = Pattern.compile("\\."); private static final String SPECIAL_CHAR_REPLACEMENT = ""; private static final String WHITESPACE_REPLACEMENT = "_"; + private static final String DOT_REPLACEMENT = ""; static boolean containsSpecialCharacter(String input) { return SPECIAL_CHAR_PATTERN.matcher(input).find(); @@ -22,6 +24,11 @@ static boolean containsTrailingWhitespace(String input) { return !input.equals(input.strip()); } + static String removeDotsReplaceWhitespace(String input) { + return WHITESPACE_PATTERN.matcher(DOT_PATTERN.matcher(input).replaceAll(DOT_REPLACEMENT)) + .replaceAll(WHITESPACE_REPLACEMENT); + } + static String removeSpecialCharacters(String input) { return SPECIAL_CHAR_PATTERN.matcher(input).replaceAll(SPECIAL_CHAR_REPLACEMENT); } diff --git a/src/test/java/ch/sbb/pfi/netzgrafikeditor/converter/core/validation/NetworkGraphicValidatorTest.java b/src/test/java/ch/sbb/pfi/netzgrafikeditor/converter/core/validation/NetworkGraphicValidatorTest.java index 19cf2f6..c403af1 100644 --- a/src/test/java/ch/sbb/pfi/netzgrafikeditor/converter/core/validation/NetworkGraphicValidatorTest.java +++ b/src/test/java/ch/sbb/pfi/netzgrafikeditor/converter/core/validation/NetworkGraphicValidatorTest.java @@ -30,13 +30,13 @@ void setUp() { Node.builder().betriebspunktName("in validNode").build(), Node.builder().betriebspunktName(" invalidNode").build(), Node.builder().betriebspunktName("invalidNode ").build(), - Node.builder().betriebspunktName("inVälidNöde").build()); + Node.builder().betriebspunktName("inVälid.Nöde").build()); List trainruns = List.of(Trainrun.builder().name("validTrainrun").build(), Trainrun.builder().name("in validTrainrun").build(), Trainrun.builder().name(" invalidTrainrun").build(), Trainrun.builder().name("invalidTrainrun ").build(), - Trainrun.builder().name("inVälidTräinrün").build()); + Trainrun.builder().name("inVälid.Träinrün").build()); original = NetworkGraphic.builder().nodes(nodes).trainruns(trainruns).build(); } @@ -54,10 +54,10 @@ void run(ValidationStrategy strategy) { case FAIL_ON_ISSUES -> assertThrows(IllegalStateException.class, validator::run); case REPLACE_WHITESPACE -> { NetworkGraphic validated = validator.run(); - assertEquals(List.of("validNode", "in_validNode", "invalidNode", "invalidNode", "inVälidNöde"), + assertEquals(List.of("validNode", "in_validNode", "invalidNode", "invalidNode", "inVälid.Nöde"), getNodeIds(validated)); assertEquals(List.of("validTrainrun", "in_validTrainrun", "invalidTrainrun", "invalidTrainrun", - "inVälidTräinrün"), getTrainrunIds(validated)); + "inVälid.Träinrün"), getTrainrunIds(validated)); } case REMOVE_SPECIAL_CHARACTERS -> { NetworkGraphic validated = validator.run(); @@ -66,7 +66,13 @@ void run(ValidationStrategy strategy) { assertEquals(List.of("validTrainrun", "in_validTrainrun", "invalidTrainrun", "invalidTrainrun", "inVlidTrinrn"), getTrainrunIds(validated)); } + case REMOVE_DOTS_AND_REPLACE_WHITESPACE -> { + NetworkGraphic validated = validator.run(); + assertEquals(List.of("validNode", "in_validNode", "_invalidNode", "invalidNode_", "inVälidNöde"), + getNodeIds(validated)); + assertEquals(List.of("validTrainrun", "in_validTrainrun", "_invalidTrainrun", "invalidTrainrun_", + "inVälidTräinrün"), getTrainrunIds(validated)); + } } } - } \ No newline at end of file