Skip to content

Commit

Permalink
feat: add validation strategy REMOVE_DOTS_AND_REPLACE_WHITESPACE
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
munterfi committed Dec 9, 2024
1 parent 87425bc commit 1a3d988
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Trainrun> 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();
}
Expand All @@ -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();
Expand All @@ -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));
}
}
}

}

0 comments on commit 1a3d988

Please sign in to comment.