Class TextReplacer

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.lang.Readable

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

      • Fields inherited from class java.io.Reader

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      TextReplacer​(java.util.Map<java.lang.String,​java.lang.String> fields, java.io.Reader input)
      Create an object where we'll filter an input stream replacing variables with values.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      static void main​(java.lang.String[] args)
      Usage: TextReplacer filename key=val [key=val] ...
      int read()  
      int read​(char[] buf)  
      int read​(char[] buf, int offset, int len)  
      java.lang.String readLine()  
      boolean ready()  
      java.lang.String replace​(java.lang.String input)
      Replace all settings variables with their values.
      long skip​(long val)  
      • Methods inherited from class java.io.Reader

        mark, markSupported, nullReader, read, reset, transferTo
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TextReplacer

        public TextReplacer​(java.util.Map<java.lang.String,​java.lang.String> fields,
                            java.io.Reader input)
                     throws java.io.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:
        java.io.IOException
    • Method Detail

      • readLine

        public java.lang.String readLine()
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • ready

        public boolean ready()
                      throws java.io.IOException
        Overrides:
        ready in class java.io.Reader
        Throws:
        java.io.IOException
      • read

        public int read()
                 throws java.io.IOException
        Overrides:
        read in class java.io.Reader
        Throws:
        java.io.IOException
      • skip

        public long skip​(long val)
                  throws java.io.IOException
        Overrides:
        skip in class java.io.Reader
        Throws:
        java.io.IOException
      • read

        public int read​(char[] buf)
                 throws java.io.IOException
        Overrides:
        read in class java.io.Reader
        Throws:
        java.io.IOException
      • read

        public int read​(char[] buf,
                        int offset,
                        int len)
                 throws java.io.IOException
        Specified by:
        read in class java.io.Reader
        Throws:
        java.io.IOException
      • replace

        public java.lang.String replace​(java.lang.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​(java.lang.String[] args)
                         throws java.lang.Exception
        Usage: TextReplacer filename key=val [key=val] ...
        Throws:
        java.lang.Exception
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in class java.io.Reader
        Throws:
        java.io.IOException