diff --git a/src/main/java/de/marhali/easyi18n/util/KeyPathConverter.java b/src/main/java/de/marhali/easyi18n/util/KeyPathConverter.java index 112e119..cde4cd2 100644 --- a/src/main/java/de/marhali/easyi18n/util/KeyPathConverter.java +++ b/src/main/java/de/marhali/easyi18n/util/KeyPathConverter.java @@ -61,7 +61,8 @@ public class KeyPathConverter { // Missing namespace if(i == 0 && settings.getFolderStrategy().isNamespaceMode() && hasDefaultNamespace()) { - if(section.length() == literalPath.length() || !String.valueOf(literalPath.charAt(section.length())).equals(settings.getNamespaceDelimiter())) { + String namespaceDelim = (settings.isNestedKeys() ? "" : "\\") + settings.getNamespaceDelimiter(); + if(section.length() == literalPath.length() || !literalPath.substring(section.length()).startsWith(namespaceDelim)) { path.add(settings.getDefaultNamespace()); } } @@ -102,7 +103,7 @@ public class KeyPathConverter { builder.append(Pattern.quote(settings.getSectionDelimiter())); // Add optional namespace delimiter if present - if(hasDefaultNamespace()) { + if(settings.getNamespaceDelimiter() != null && !settings.getNamespaceDelimiter().isEmpty()) { builder.append("|"); builder.append(Pattern.quote(Objects.requireNonNull(settings.getNamespaceDelimiter()))); } @@ -115,25 +116,27 @@ public class KeyPathConverter { * Securely escape found delimiters inside provided section according to the configured policy. */ private String quoteSection(String section) { + String quoted = section; if(!settings.isNestedKeys()) { - return section; + return quoted; } if(hasDefaultNamespace()) { - section = section.replace(settings.getNamespaceDelimiter(), "\\" + settings.getNamespaceDelimiter()); + quoted = quoted.replace(settings.getNamespaceDelimiter(), "\\" + settings.getNamespaceDelimiter()); } - section = section.replace(settings.getSectionDelimiter(), "\\" + settings.getSectionDelimiter()); - return section; + quoted = quoted.replace(settings.getSectionDelimiter(), "\\" + settings.getSectionDelimiter()); + return quoted; } private String unquoteSection(String section) { + String unquoted = section; if(hasDefaultNamespace()) { - section = section.replace("\\" + settings.getNamespaceDelimiter(), settings.getNamespaceDelimiter()); + unquoted = unquoted.replace("\\" + settings.getNamespaceDelimiter(), settings.getNamespaceDelimiter()); } - section = section.replace("\\" + settings.getSectionDelimiter(), settings.getSectionDelimiter()); - return section; + unquoted = unquoted.replace("\\" + settings.getSectionDelimiter(), settings.getSectionDelimiter()); + return unquoted; } /** diff --git a/src/test/java/de/marhali/easyi18n/KeyPathConverterTest.java b/src/test/java/de/marhali/easyi18n/KeyPathConverterTest.java index 7dfd845..9718215 100644 --- a/src/test/java/de/marhali/easyi18n/KeyPathConverterTest.java +++ b/src/test/java/de/marhali/easyi18n/KeyPathConverterTest.java @@ -11,13 +11,30 @@ import org.jetbrains.annotations.Nullable; import org.junit.Assert; import org.junit.Test; - /** * Unit tests for {@link KeyPathConverter}. * @author marhali */ public class KeyPathConverterTest { + @Test + public void noNamespaceDelimiter() { + KeyPathConverter converter = getConverter(FolderStrategyType.MODULARIZED_NAMESPACE, null, ".", null, true); + + Assert.assertEquals(new KeyPath("username"), converter.fromString("username")); + Assert.assertEquals(new KeyPath("username:nested"), converter.fromString("username:nested")); + Assert.assertEquals(new KeyPath("username:nested", "leaf"), converter.fromString("username:nested.leaf")); + } + + @Test + public void emptyDefaultNamespace() { + KeyPathConverter converter = getConverter(FolderStrategyType.MODULARIZED_NAMESPACE, ":", ".", null, true); + + Assert.assertEquals(new KeyPath("username"), converter.fromString("username")); + Assert.assertEquals(new KeyPath("username", "nested"), converter.fromString("username:nested")); + Assert.assertEquals(new KeyPath("username", "nested", "leaf"), converter.fromString("username:nested.leaf")); + } + @Test public void nonNestedSingle() { KeyPathConverter converter = getConverter(FolderStrategyType.SINGLE, null, ".", null, false); @@ -39,8 +56,7 @@ public class KeyPathConverterTest { Assert.assertEquals("username.title\\:concat.leaf\\.node", converter.toString(new KeyPath("username.title", "concat.leaf", "node"))); Assert.assertEquals(new KeyPath("common", "username"), converter.fromString("username")); - // TODO: problem here - Assert.assertEquals(new KeyPath("username.title", "concat.leaf", "node"), converter.fromString("username.title\\:concat\\.leaf.node")); + Assert.assertEquals(new KeyPath("username.title", "concat", "leaf.node"), converter.fromString("username.title\\:concat\\.leaf.node")); } @Test