From 9d5be9caa09720f546a8cdd4660b487e2bdfab72 Mon Sep 17 00:00:00 2001 From: marhali Date: Sun, 10 Apr 2022 20:29:13 +0200 Subject: [PATCH] partial work on KeyPathConverter replacement --- .../easyi18n/model/KeyPathConverter.java | 1 + .../easyi18n/util/KeyPathConverter.java | 145 ++++++++++++++++++ .../easyi18n/KeyPathConverterTest.java | 145 ++++++++++++++---- 3 files changed, 265 insertions(+), 26 deletions(-) create mode 100644 src/main/java/de/marhali/easyi18n/util/KeyPathConverter.java diff --git a/src/main/java/de/marhali/easyi18n/model/KeyPathConverter.java b/src/main/java/de/marhali/easyi18n/model/KeyPathConverter.java index cfb9d26..1bc5fcd 100644 --- a/src/main/java/de/marhali/easyi18n/model/KeyPathConverter.java +++ b/src/main/java/de/marhali/easyi18n/model/KeyPathConverter.java @@ -13,6 +13,7 @@ import java.util.regex.Pattern; * If nesting is enabled the delimiter within a section is escaped otherwise the delimiter between the key sections. * @author marhali */ +@Deprecated // Replacement moved to utils public class KeyPathConverter { private final boolean nestKeys; diff --git a/src/main/java/de/marhali/easyi18n/util/KeyPathConverter.java b/src/main/java/de/marhali/easyi18n/util/KeyPathConverter.java new file mode 100644 index 0000000..112e119 --- /dev/null +++ b/src/main/java/de/marhali/easyi18n/util/KeyPathConverter.java @@ -0,0 +1,145 @@ +package de.marhali.easyi18n.util; + +import de.marhali.easyi18n.model.translation.KeyPath; +import de.marhali.easyi18n.settings.ProjectSettings; + +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; +import java.util.regex.Pattern; + +/** + * Stateful utility to transform absolute translation keys into their character literal representation and backwards. + * @author marhali + */ +public class KeyPathConverter { + + private final ProjectSettings settings; + + /** + * Constructs a new converter instance + * @param settings Delimiter configuration + */ + public KeyPathConverter(ProjectSettings settings) { + this.settings = settings; + } + + /** + * Transform to character literal representation + * @param path Absolute key path + * @return Character literal + */ + public @NotNull String toString(@NotNull KeyPath path) { + StringBuilder builder = new StringBuilder(); + + for(int i = 0; i < path.size(); i++) { + if(i > 0) { // Delimiters + if(i == 1 && settings.getFolderStrategy().isNamespaceMode() && settings.getNamespaceDelimiter() != null) { + builder.append(quoteDelimiter(settings.getNamespaceDelimiter())); + } else { + builder.append(quoteDelimiter(settings.getSectionDelimiter())); + } + } + + // Section content + builder.append(quoteSection(path.get(i))); + } + + return builder.toString(); + } + + /** + * Splits provided character literal into key path sections. + * If namespace mode is activated and none was provided, the default namespace will be added. + * @return Layered key path sections + */ + public @NotNull KeyPath fromString(@NotNull String literalPath) { + KeyPath path = new KeyPath(); + + int i = 0; + for(String section : literalPath.split(getSplitRegex())) { + + // Missing namespace + if(i == 0 && settings.getFolderStrategy().isNamespaceMode() && hasDefaultNamespace()) { + if(section.length() == literalPath.length() || !String.valueOf(literalPath.charAt(section.length())).equals(settings.getNamespaceDelimiter())) { + path.add(settings.getDefaultNamespace()); + } + } + + path.add(unquoteSection(section)); + + i++; + } + + return path; + } + + @Override + public String toString() { + return "KeyPathConverter{" + + "settings=" + settings + + '}'; + } + + /* + * INTERNAL METHODS + */ + + private boolean hasDefaultNamespace() { + return settings.getDefaultNamespace() != null && !settings.getDefaultNamespace().isEmpty(); + } + + private String getSplitRegex() { + return settings.isNestedKeys() + ? ("(?