/** * Tests complex and recursive substitution in variable names. */ @Test public void testReplaceInVariableRecursive() { values.put("animal.2", "brown fox"); values.put("animal.1", "white mouse"); values.put("color", "white"); values.put("species.white", "1"); values.put("species.brown", "2"); final StrSubstitutor sub = new StrSubstitutor(values); sub.setEnableSubstitutionInVariables(true); assertEquals( "Wrong result (1)", "The white mouse jumps over the lazy dog.", sub.replace("The ${animal.${species.${color}}} jumps over the ${target}.")); assertEquals( "Wrong result (2)", "The brown fox jumps over the lazy dog.", sub.replace("The ${animal.${species.${unknownColor:-brown}}} jumps over the ${target}.")); }
/** * Tests whether a variable can be replaced in a variable name. */ @Test public void testReplaceInVariable() { values.put("animal.1", "fox"); values.put("animal.2", "mouse"); values.put("species", "2"); final StrSubstitutor sub = new StrSubstitutor(values); sub.setEnableSubstitutionInVariables(true); assertEquals( "Wrong result (1)", "The mouse jumps over the lazy dog.", sub.replace("The ${animal.${species}} jumps over the ${target}.")); values.put("species", "1"); assertEquals( "Wrong result (2)", "The fox jumps over the lazy dog.", sub.replace("The ${animal.${species}} jumps over the ${target}.")); assertEquals( "Wrong result (3)", "The fox jumps over the lazy dog.", sub.replace("The ${unknown.animal.${unknown.species:-1}:-fox} jumps over the ${unknown.target:-lazy dog}.")); }
public static <T> T loadConfigFromEnv(Class<T> configurationClass, final String path) throws IOException { LOGGER.info("Parsing configuration file from {} ", path); logProcessEnv(); final Path configPath = Paths.get(path); final File file = configPath.toAbsolutePath().toFile(); final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); final StrSubstitutor sub = new StrSubstitutor(new StrLookup<Object>() { @Override public String lookup(String key) { return System.getenv(key); } }); sub.setEnableSubstitutionInVariables(true); final String conf = sub.replace(FileUtils.readFileToString(file)); return mapper.readValue(conf, configurationClass); }
@Override public Context create(final Lookup lookup) { final StrSubstitutor sub = new StrSubstitutor( new StrLookup<String>() { @Override public String lookup(String key) { return lookup.lookup(key); } }, "${", "}", '$').setValueDelimiter(":"); sub.setEnableSubstitutionInVariables(true); return new Context() { @Override public String resolve(String value) { return sub.replace(value); } }; } }
strSubstitutor.setEnableSubstitutionInVariables(true); String replace = strSubstitutor.replace(pattern);
public static StrSubstitutor createStrSubstitutor(String prefix, String suffix, StrLookup<String> lookup) { StrSubstitutor substitutor = new StrSubstitutor(); substitutor.setEnableSubstitutionInVariables(true); substitutor.setVariablePrefix(prefix); substitutor.setVariableSuffix(suffix); substitutor.setVariableResolver(lookup); return substitutor; }
@Override public void initialize(Bootstrap<MutableSchedulerConfiguration> bootstrap) { super.initialize(bootstrap); StrSubstitutor strSubstitutor = new StrSubstitutor(new EnvironmentVariableLookup(false)); strSubstitutor.setEnableSubstitutionInVariables(true); bootstrap.addBundle(new Java8Bundle()); bootstrap.setConfigurationSourceProvider( new SubstitutingSourceProvider( bootstrap.getConfigurationSourceProvider(), strSubstitutor)); }