/** * Obtain a function that will replace variables in the supplied value with values from the supplied lookup function. * <p> * Variables may appear anywhere within a string value, and multiple variables can be used within the same value. Variables * take the form: * * <pre> * variable := '${' variableNames [ ':' defaultValue ] '}' * variableNames := variableName [ ',' variableNames ] * variableName := // any characters except ',' and ':' and '}' * defaultValue := // any characters except '}' * </pre> * * Note that <i>variableName</i> is the name used to look up a the property. * </p> * Notice that the syntax supports multiple <i>variables</i>. The logic will process the <i>variables</i> from let to right, * until an existing property is found. And at that point, it will stop and will not attempt to find values for the other * <i>variables</i>. * <p> * * @param replacementsByVariableName the function used to find the replacements for variable names; may not be null * @return the function that will replace variables in supplied strings; never null */ public static Function<String, String> replaceVariablesWith(Function<String, String> replacementsByVariableName) { return (value) -> { return replaceVariables(value, replacementsByVariableName); }; }
return Strings.replaceVariables(value, valuesByVariableName); });
protected void assertReplacement(String before, Map<String, String> replacements, String after) { String result = Strings.replaceVariables(before, replacements::get); assertThat(result).isEqualTo(after); }
/** * Read the contents of the supplied {@link InputStream}, replace all variables found in the content, and write the * result to a temporary file. * * @param stream the input stream containing zero or more {@link Strings#replaceVariables(String, java.util.function.Function) * variable expressions} * @param variables the variables * @return the temporary file that exists in the data directory * @throws IOException if there is a problem reading the input stream or writing to the temporary file */ protected static File replaceVariables(InputStream stream, AvailableVariables variables) throws IOException { File tmpFile = Testing.Files.createTestingFile(); try (OutputStream ostream = new FileOutputStream(tmpFile)) { IoUtil.readLines(stream, (line) -> { String newLine = Strings.replaceVariables(line, variables::variableForName); try { ostream.write(newLine.getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { throw new RuntimeException("Error writing to file '" + tmpFile + "'", e); } }, StandardCharsets.UTF_8); } return tmpFile; } }
/** * Obtain a function that will replace variables in the supplied value with values from the supplied lookup function. * <p> * Variables may appear anywhere within a string value, and multiple variables can be used within the same value. Variables * take the form: * * <pre> * variable := '${' variableNames [ ':' defaultValue ] '}' * variableNames := variableName [ ',' variableNames ] * variableName := // any characters except ',' and ':' and '}' * defaultValue := // any characters except '}' * </pre> * * Note that <i>variableName</i> is the name used to look up a the property. * </p> * Notice that the syntax supports multiple <i>variables</i>. The logic will process the <i>variables</i> from let to right, * until an existing property is found. And at that point, it will stop and will not attempt to find values for the other * <i>variables</i>. * <p> * * @param replacementsByVariableName the function used to find the replacements for variable names; may not be null * @return the function that will replace variables in supplied strings; never null */ public static Function<String, String> replaceVariablesWith(Function<String, String> replacementsByVariableName) { return (value) -> { return replaceVariables(value, replacementsByVariableName); }; }
return Strings.replaceVariables(value, valuesByVariableName); });
/** * Read the contents of the supplied {@link InputStream}, replace all variables found in the content, and write the * result to a temporary file. * * @param stream the input stream containing zero or more {@link Strings#replaceVariables(String, java.util.function.Function) * variable expressions} * @param variables the variables * @return the temporary file that exists in the data directory * @throws IOException if there is a problem reading the input stream or writing to the temporary file */ protected static File replaceVariables(InputStream stream, AvailableVariables variables) throws IOException { File tmpFile = Testing.Files.createTestingFile(); try (OutputStream ostream = new FileOutputStream(tmpFile)) { IoUtil.readLines(stream, (line) -> { String newLine = Strings.replaceVariables(line, variables::variableForName); try { ostream.write(newLine.getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { throw new RuntimeException("Error writing to file '" + tmpFile + "'", e); } }, StandardCharsets.UTF_8); } return tmpFile; } }