ConfigUtils { public static Config findReplaceStringValues(Config config, String findRegex, Object replace) { for (Map.Entry<String, ConfigValue> valueEntry : config.entrySet()) { ConfigValueType valueType = valueEntry.getValue().valueType(); if (valueType.equals(ConfigValueType.OBJECT)) { config = ConfigUtils.findReplaceStringValues(config.getConfig(valueEntry.getKey()), findRegex, replace); } else if (valueType.equals(ConfigValueType.LIST)) { @SuppressWarnings("unchecked") List<Object> valueList = (List<Object>)valueEntry.getValue().unwrapped(); if (valueList.size() > 0) { if (valueList.get(0) instanceof String) { for (int i = 0; i < valueList.size(); i++) { String found = (String)valueList.get(i); String replaced = found.replaceAll(findRegex, replace.toString()); valueList.set(i, replaced); } } } config = config.withValue(valueEntry.getKey(), ConfigValueFactory.fromAnyRef(valueList)); } else if (valueType.equals(ConfigValueType.STRING)) { String found = (String)valueEntry.getValue().unwrapped(); String replaced = found.replaceAll(findRegex, replace.toString()); config = config.withValue(valueEntry.getKey(), ConfigValueFactory.fromAnyRef(replaced)); } } return config; }  static Config configFromPath(String path); static Config configFromResource(String resource); static Config applySubstitutions(Config config); static Config applySubstitutions(Config config, String substitutionsString); static Config getApplicationConfig(Config config); static Config mergeLoadedConfiguration(Config config); static Config findReplaceStringValues(Config config, String findRegex, Object replace); static boolean canBeCoerced(Config config, String path, ConfigValueType type); static T getOrElse(Config config, String path, T orElse); static T getOrNull(Config config, String path); static final String JAR_FILE_EXCEPTION_MESSAGE; }
@Test public void testFindReplaceStringValues() { Config baseConfig = ConfigFactory.parseString("a: ${replaceme}, b: \"${replaceme}\", " + "c: [${replaceme}, \"hello\", ${replaceme}], d: [\"${replaceme}\", \"world\", " + "\"${replaceme}\"], e: { f: \"${replaceme}\", g: [\"${replaceme}\"] }"); Config resolvedConfig = baseConfig.resolveWith( ConfigFactory.empty().withValue("replaceme", ConfigValueFactory.fromAnyRef("REPLACED"))); Config replacedConfig = ConfigUtils.findReplaceStringValues( resolvedConfig, "\\$\\{replaceme\\}", "REPLACED"); assertEquals(replacedConfig.getString("a"), "REPLACED"); assertEquals(replacedConfig.getString("b"), "REPLACED"); assertEquals(replacedConfig.getStringList("c").get(0), "REPLACED"); assertEquals(replacedConfig.getStringList("c").get(1), "hello"); assertEquals(replacedConfig.getStringList("c").get(2), "REPLACED"); assertEquals(replacedConfig.getStringList("d").get(0), "REPLACED"); assertEquals(replacedConfig.getStringList("d").get(1), "world"); assertEquals(replacedConfig.getStringList("d").get(2), "REPLACED"); assertEquals(replacedConfig.getConfig("e").getString("f"), "REPLACED"); assertEquals(replacedConfig.getConfig("e").getStringList("g").get(0), "REPLACED"); }