-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from epam/main
Improve substitutions
- Loading branch information
Showing
5 changed files
with
210 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
gflog-core/src/main/java/com/epam/deltix/gflog/core/util/StringSubstitution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package com.epam.deltix.gflog.core.util; | ||
|
||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
|
||
public final class StringSubstitution { | ||
|
||
private static final String SYSTEM_PROPERTY_PREFIX = "sys:"; | ||
private static final String ENVIRONMENT_VARIABLE_PREFIX = "env:"; | ||
|
||
private final Properties defaultProperties; | ||
private final Properties systemProperties; | ||
private final Map<String, String> environmentVariables; | ||
|
||
public StringSubstitution(final Properties defaultProperties, | ||
final Properties systemProperties, | ||
final Map<String, String> environmentVariables) { | ||
this.defaultProperties = defaultProperties; | ||
this.systemProperties = systemProperties; | ||
this.environmentVariables = environmentVariables; | ||
} | ||
|
||
public String substitute(final String text) { | ||
final StringBuilder builder = new StringBuilder(); | ||
|
||
for (int index = 0; index < text.length(); ) { | ||
final int position = text.indexOf("${", index); | ||
|
||
if (position < 0) { | ||
builder.append(text, index, text.length()); | ||
break; | ||
} | ||
|
||
builder.append(text, index, position); | ||
index = substitute(text, position, builder); | ||
} | ||
|
||
return builder.toString(); | ||
} | ||
|
||
private int substitute(final String source, int index, final StringBuilder target) { | ||
final int end = source.length(); | ||
final int start = index; | ||
|
||
index += 2; | ||
|
||
while (index < end) { | ||
char c = source.charAt(index); | ||
|
||
if (c == '}') { | ||
final String key = source.substring(start + 2, index); | ||
final String value = lookup(key); | ||
|
||
if (value == null) { | ||
target.append("${").append(key).append(":#UNRESOLVED#").append("}"); | ||
} else { | ||
target.append(value); | ||
} | ||
|
||
return index + 1; | ||
} | ||
|
||
if (c == ':' && index + 1 < end && source.charAt(index + 1) == '-') { | ||
final String key = source.substring(start + 2, index); | ||
final String value = lookup(key); | ||
|
||
final int length = target.length(); | ||
final int mid = index + 2; | ||
|
||
index = mid; | ||
|
||
while (index < end) { | ||
c = source.charAt(index); | ||
|
||
if (c == '}') { | ||
if (value != null) { | ||
target.setLength(length); | ||
target.append(value); | ||
} | ||
|
||
return index + 1; | ||
} | ||
|
||
if (c == '$' && index + 1 < end && source.charAt(index + 1) == '{') { | ||
index = substitute(source, index, target); | ||
continue; | ||
} | ||
|
||
target.append(c); | ||
index++; | ||
} | ||
|
||
target.insert(length, source, start, mid); | ||
return end; | ||
} | ||
|
||
index++; | ||
} | ||
|
||
target.append(source, start, end); | ||
return index; | ||
} | ||
|
||
private String lookup(final String key) { | ||
if (key.startsWith(SYSTEM_PROPERTY_PREFIX)) { | ||
final String propertyKey = key.substring(SYSTEM_PROPERTY_PREFIX.length()); | ||
return systemProperties.getProperty(propertyKey); | ||
} | ||
|
||
if (key.startsWith(ENVIRONMENT_VARIABLE_PREFIX)) { | ||
final String environmentKey = key.substring(ENVIRONMENT_VARIABLE_PREFIX.length()); | ||
return environmentVariables.get(environmentKey); | ||
} | ||
|
||
return defaultProperties.getProperty(key); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
gflog-core/src/test/java/com/epam/deltix/gflog/core/util/StringSubstitutionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.epam.deltix.gflog.core.util; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
public class StringSubstitutionTest { | ||
|
||
@Test | ||
public void testDefaults() { | ||
final Properties defaults = new Properties(); | ||
defaults.put("a", "A"); | ||
defaults.put("b", "B"); | ||
defaults.put("c", "C"); | ||
|
||
final Properties systems = new Properties(); | ||
final Map<String, String> environments = new HashMap<>(); | ||
final StringSubstitution substitution = new StringSubstitution(defaults, systems, environments); | ||
final Map<String, String> mapping = new LinkedHashMap<>(); | ||
|
||
mapping.put("12345", "12345"); | ||
mapping.put("${a} ${b} ${c}", "A B C"); | ||
mapping.put("${a:-10} ${b:-${c:-100500}}", "A B"); | ||
mapping.put("${missing:-5} + ${missing:-10}", "5 + 10"); | ||
mapping.put("${missing:-${a}}", "A"); | ||
mapping.put("${missing:-${missing2:-${b}}}", "B"); | ||
mapping.put("${missing:-${missing2:-${missing3:-${c}}}}", "C"); | ||
mapping.put("${a:-5", "${a:-5"); | ||
mapping.put(" ${a:-${b} ", " ${a:-B "); | ||
mapping.put(" ${a:-${b:-${c ", " ${a:-${b:-${c "); | ||
mapping.put(" ${a:-${b:-${c} ", " ${a:-${b:-C "); | ||
mapping.put(" ${a:-${b:-${c}} ", " ${a:-B "); | ||
mapping.put("${missing}", "${missing:#UNRESOLVED#}"); | ||
mapping.put("${missing:-1 ${a} 2 ${missing2} 3}", "1 A 2 ${missing2:#UNRESOLVED#} 3"); | ||
mapping.put("${missing:-}", ""); | ||
|
||
for (final Map.Entry<String, String> entry : mapping.entrySet()) { | ||
final String text = entry.getKey(); | ||
final String expected = entry.getValue(); | ||
final String actual = substitution.substitute(text); | ||
|
||
Assert.assertEquals(expected, actual); | ||
} | ||
} | ||
|
||
@Test | ||
public void testEnvironmentAndSystemSubstitution() { | ||
final Properties defaults = new Properties(); | ||
defaults.put("a", "1"); | ||
defaults.put("b", "2"); | ||
defaults.put("c", "3"); | ||
|
||
final Properties systems = new Properties(); | ||
systems.put("a", "4"); | ||
systems.put("b", "5"); | ||
systems.put("c", "6"); | ||
|
||
final Map<String, String> environments = new HashMap<>(); | ||
environments.put("a", "7"); | ||
environments.put("b", "8"); | ||
environments.put("c", "9"); | ||
|
||
final StringSubstitution substitution = new StringSubstitution(defaults, systems, environments); | ||
final Map<String, String> mapping = new LinkedHashMap<>(); | ||
|
||
mapping.put("${a} ${sys:a} ${env:a}", "1 4 7"); | ||
mapping.put("${missing:-${b}} ${missing:-${sys:b}} ${missing:-${env:b}}", "2 5 8"); | ||
mapping.put("${sys:missing:-${b}} ${sys:missing:-${sys:b}} ${sys:missing:-${env:b}}", "2 5 8"); | ||
mapping.put("${env:missing:-${b}} ${env:missing:-${sys:b}} ${env:missing:-${env:b}}", "2 5 8"); | ||
|
||
for (final Map.Entry<String, String> entry : mapping.entrySet()) { | ||
final String text = entry.getKey(); | ||
final String expected = entry.getValue(); | ||
final String actual = substitution.substitute(text); | ||
|
||
Assert.assertEquals(expected, actual); | ||
} | ||
} | ||
} |