Module id.xfunction

Class Substitutor

Object
Substitutor

public class Substitutor extends Object
Performs string substitution according to the given mapping.
  • Constructor Details

    • Substitutor

      public Substitutor()
  • Method Details

    • withRegexpSupport

      public Substitutor withRegexpSupport()
      Allow to specify regexps in mapping
    • withBackup

      public Substitutor withBackup()
      Create backup of the original file when doing any changes. If no changes are done, backup file is not created. If backup file already exists then it is replaced.
    • substitutePerLine

      public List<String> substitutePerLine(List<String> lines, Map<String,String> mapping)
      Creates and returns a new list by iterating over the input list and performing line by line string substitution as defined in the mapping.
    • substitutePerLine

      public List<Path> substitutePerLine(Path target, Map<String,String> mapping) throws IOException
      Performs inplace line by line substitution in a given directory or file.
      Throws:
      IOException
    • substitutePerLine

      public List<Path> substitutePerLine(Path target, Predicate<Path> fileFilter, Map<String,String> mapping) throws IOException
      Equivalent to substitutePerLine(Path, Map) except allows to substitute only files which satisfy given filter.
      Throws:
      IOException
    • restoreBackup

      public static boolean restoreBackup(Path file) throws IOException
      Throws:
      IOException
    • substitute

      public String substitute(String text, Map<String,String> mapping)
      Substitutes all values and return new string
    • substitute

      public List<Path> substitute(Path target, Predicate<Path> fileFilter, Map<String,String> mapping) throws IOException
      Equivalent to substitute(Path, Map) except allows to substitute only files which satisfy given filter.
      Throws:
      IOException
    • substitute

      public List<Path> substitute(Path target, Map<String,String> mapping) throws IOException
      Performs inplace substitution in a given file or in case of directory, in all files inside it. It differs from substitutePerLine(Path, Map) that it reads entire file in memory as a single string and performs substitution on such string. This allows to create multiline mappings and perform multiline substitutions (of course this requires appropriate Java pattern which should support multiline matching, this can be done by prepending "(?s)(?m)" to it).
      Throws:
      IOException
    • maskSecrets

      public String maskSecrets(String text, List<String> secrets)
      Finds all secrets in the input text and replaces them with a mask
    • maskSecretsFunc

      public Function<String,String> maskSecretsFunc(List<String> secrets)
      Returns:
      function which accepts input text and masks all secrets in it