Class TextReplacer

  • All Implemented Interfaces:
    Closeable, AutoCloseable, Readable

    public class TextReplacer
    extends Reader
    This class provides a utility function that transforms strings to replace embedded settings with the current values of the setting.
    • Constructor Detail

      • TextReplacer

        public TextReplacer​(Map<String,​String> fields,
                            Reader input)
                     throws IOException
        Create an object where we'll filter an input stream replacing variables with values.
        Parameters:
        fields - The map from variable keys to output values.
        input - The input Reader to be filtered.
        Throws:
        IOException
    • Method Detail

      • replace

        public String replace​(String input)
        Replace all settings variables with their values. This function takes the current line and looks for all variables that are embedded in the string and replaces those variables with their value. E.g., given a string "The survey is ${survey} and the scale is ${scale}." the method will replace the string ${survey} with the specified value of survey and ${scale} with the specified value of scale. If an unmatched variable is found, the entire line is returned as a "". This allows one to control which lines are returned by the presence or absence of variables.
      • main

        public static void main​(String[] args)
                         throws Exception
        Usage: TextReplacer filename key=val [key=val] ...
        Throws:
        Exception