use of org.apache.commons.lang.text.StrSubstitutor in project ddf by codice.
the class AbstractIntegrationTest method getFileContent.
/**
* Variables to be replaced in a resource file should be in the format: $variableName$ The
* variable to replace in the file should also also match the parameter names of the method
* calling getFileContent.
*
* @param filePath
* @param params
* @param classRelativeToResource
* @return
*/
@SuppressWarnings({ "squid:S00112" /* A generic RuntimeException is perfectly reasonable in this case. */
})
public static String getFileContent(String filePath, ImmutableMap<String, String> params, Class classRelativeToResource) {
StrSubstitutor strSubstitutor = new StrSubstitutor(params);
strSubstitutor.setVariablePrefix(RESOURCE_VARIABLE_DELIMETER);
strSubstitutor.setVariableSuffix(RESOURCE_VARIABLE_DELIMETER);
String fileContent;
try {
fileContent = IOUtils.toString(classRelativeToResource.getClassLoader().getResourceAsStream(filePath), "UTF-8");
} catch (IOException e) {
throw new RuntimeException("Failed to read filepath: " + filePath);
}
return strSubstitutor.replace(fileContent);
}
use of org.apache.commons.lang.text.StrSubstitutor in project coprhd-controller by CoprHD.
the class LocalCassandraService method createConfig.
/**
* Generates a configuration for running a local cassandra instance.
*
* @param configResource
* the configuration resource name.
* @param rootDir
* the root directory for the cassandra instance.
* @return the configuration location.
*
* @throws IOException
* if an I/O error occurs.
*/
private static String createConfig(String configResource, String rootDir) throws IOException {
URL configURL = LocalCassandraService.class.getResource(configResource);
if (configURL == null) {
throw new IllegalStateException("Could not find " + configResource);
}
File path = FileUtils.toFile(configURL);
String data = FileUtils.readFileToString(path, "UTF-8");
StrSubstitutor substitutor = new StrSubstitutor(Collections.singletonMap("rootDir", rootDir));
String contents = substitutor.replace(data);
File configFile = File.createTempFile("config", ".yaml");
configFile.deleteOnExit();
FileUtils.writeStringToFile(configFile, contents, "UTF-8");
return "file:" + configFile.getAbsolutePath();
}
use of org.apache.commons.lang.text.StrSubstitutor in project coprhd-controller by CoprHD.
the class TestDbService method createConfig.
private static String createConfig(String rootDir) throws IOException {
URL configURL = TestDbService.class.getResource("db-test.yaml");
if (configURL == null) {
throw new IllegalStateException("Could not find cassandra.yaml");
}
File path = FileUtils.toFile(configURL);
String data = FileUtils.readFileToString(path, "UTF-8");
StrSubstitutor substitutor = new StrSubstitutor(Collections.singletonMap("rootDir", rootDir));
String contents = substitutor.replace(data);
File configFile = File.createTempFile("db-test", ".yaml");
configFile.deleteOnExit();
FileUtils.writeStringToFile(configFile, contents, "UTF-8");
return "file:" + configFile.getAbsolutePath();
}
use of org.apache.commons.lang.text.StrSubstitutor in project mica2 by obiba.
the class MailService method getSubject.
public String getSubject(String subjectFormat, Map<String, String> ctx, String defaultSubject) {
StrSubstitutor sub = new StrSubstitutor(ctx, "${", "}");
String temp = //
Optional.ofNullable(subjectFormat).filter(//
s -> !s.isEmpty()).orElse(defaultSubject);
return sub.replace(temp);
}
use of org.apache.commons.lang.text.StrSubstitutor in project kylo by Teradata.
the class PropertyExpressionResolver method resolveVariables.
/**
* Resolve the str with values from the supplied {@code properties} This will recursively fill out the str looking back at the properties to get the correct value. NOTE the property values will be
* overwritten if replacement is found!
*/
public ResolvedVariables resolveVariables(String str, List<NifiProperty> properties) {
ResolvedVariables variables = new ResolvedVariables(str);
StrLookup resolver = new StrLookup() {
@Override
public String lookup(String key) {
Optional<NifiProperty> optional = properties.stream().filter(prop -> key.equals(prop.getKey())).findFirst();
if (optional.isPresent()) {
NifiProperty property = optional.get();
String value = StringUtils.isNotBlank(property.getValue()) ? property.getValue().trim() : "";
variables.getResolvedVariables().put(property.getKey(), value);
return value;
} else {
return null;
}
}
};
StrSubstitutor ss = new StrSubstitutor(resolver);
variables.setResolvedString(ss.replace(str));
Map<String, String> map = variables.getResolvedVariables();
Map<String, String> vars = map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> ss.replace(entry.getValue())));
variables.getResolvedVariables().putAll(vars);
return variables;
}
Aggregations