From 939ef0b283cced9da044cd185c3a5982330a5528 Mon Sep 17 00:00:00 2001 From: marhali Date: Wed, 12 Jan 2022 09:04:06 +0100 Subject: [PATCH] introduce improved key path representation models --- .../de/marhali/easyi18n/model/KeyPath.java | 30 ++++++++ .../easyi18n/model/KeyPathConverter.java | 69 +++++++++++++++++++ .../de/marhali/easyi18n/util/PathUtil.java | 2 + .../easyi18n/KeyPathConverterTest.java | 53 ++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 src/main/java/de/marhali/easyi18n/model/KeyPath.java create mode 100644 src/main/java/de/marhali/easyi18n/model/KeyPathConverter.java create mode 100644 src/test/java/de/marhali/easyi18n/KeyPathConverterTest.java diff --git a/src/main/java/de/marhali/easyi18n/model/KeyPath.java b/src/main/java/de/marhali/easyi18n/model/KeyPath.java new file mode 100644 index 0000000..9091539 --- /dev/null +++ b/src/main/java/de/marhali/easyi18n/model/KeyPath.java @@ -0,0 +1,30 @@ +package de.marhali.easyi18n.model; + +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * Represents a full translation key with all sections. + * Implementations can use single section or variable section length variants. + * The respective layer (io, presentation) is responsible for using the correct mapping mechanism. + * @author marhali + */ +public class KeyPath extends ArrayList { + + public static final String DELIMITER = "."; + + public static KeyPath of(String... path) { + return new KeyPath(List.of(path)); + } + + public KeyPath() { + super(); + } + + public KeyPath(@NotNull Collection c) { + super(c); + } +} \ No newline at end of file diff --git a/src/main/java/de/marhali/easyi18n/model/KeyPathConverter.java b/src/main/java/de/marhali/easyi18n/model/KeyPathConverter.java new file mode 100644 index 0000000..c5b0ecd --- /dev/null +++ b/src/main/java/de/marhali/easyi18n/model/KeyPathConverter.java @@ -0,0 +1,69 @@ +package de.marhali.easyi18n.model; + +import com.intellij.openapi.project.Project; + +import de.marhali.easyi18n.service.SettingsService; + +import org.jetbrains.annotations.NotNull; + +import java.util.regex.Pattern; + +/** + * Responsible for mapping {@link KeyPath} into single string and backwards. + * If nesting is enabled the delimiter within a section is escaped otherwise the delimiter between the key sections. + * @author marhali + */ +public class KeyPathConverter { + + private final boolean nestKeys; + + public KeyPathConverter(boolean nestKeys) { + this.nestKeys = nestKeys; + } + + public KeyPathConverter(@NotNull Project project) { + this(SettingsService.getInstance(project).getState().isNestedKeys()); + } + + public @NotNull String concat(@NotNull KeyPath path) { + StringBuilder builder = new StringBuilder(); + + for(String section : path) { + if(builder.length() > 0) { + if(!this.nestKeys) { + builder.append("\\\\"); + } + + builder.append(KeyPath.DELIMITER); + } + + if(this.nestKeys) { + builder.append(section.replace(KeyPath.DELIMITER, "\\\\" + KeyPath.DELIMITER)); + } else { + builder.append(section); + } + } + + return builder.toString(); + } + + public @NotNull KeyPath split(@NotNull String concatPath) { + String[] sections = concatPath.split(this.nestKeys ? + "(?