Class FileUtils


  • public final class FileUtils
    extends Object
    Common file-related logic used when performing a migration.
    • Method Detail

      • replaceLiteralInFile

        public static boolean replaceLiteralInFile​(File file,
                                                   String toReplace,
                                                   String replacement)
                                            throws IOException
        Evaluates a file against a literal string and replaces each match with a specified replacement.
        Parameters:
        file - the File
        toReplace - a string representing the text to replace
        replacement - the replacement text to substitute the toReplace string
        Returns:
        a boolean set to true if at least one replacement was performed in the file
        Throws:
        IOException
      • replaceInFile

        public static boolean replaceInFile​(File file,
                                            String regex,
                                            String replacement)
                                     throws IOException
        Evaluates a file against a regex pattern and replaces the captured string(s) of each regex match with a specified replacement
        Parameters:
        file - the File
        regex - a regex representing the text to replace, as a String
        replacement - the replacement text to substitute the regex
        Returns:
        a boolean set to true if at least one replacement was performed in the file
        Throws:
        IOException
      • modifyRegexMatchInFile

        public static boolean modifyRegexMatchInFile​(File file,
                                                     String regex,
                                                     String substring,
                                                     String replacement)
        Evaluates a file against a regex pattern and replaces a substring of each regex match with a specified replacement.
        Parameters:
        file - the File
        regex - a regex representing the text to replace a substring of
        substring - the substring of the regex match that will be replaced
        replacement - the replacement of the match substring
        Returns:
        a boolean set to true if at least one modification was performed in the file
      • getRegExCaptureGroups

        public static List<String> getRegExCaptureGroups​(String regex,
                                                         File file)
                                                  throws IOException
        Retrieves capture groups from a regular expression match inside a file.
        Parameters:
        regex - a regex containing capture groups, as a String
        file - the file to search for matching capture groups
        Returns:
        An ArrayList of Strings representing each capture group in the regex that was matched
        Throws:
        IOException
        See Also:
        getRegExCaptureGroups(String, String)
      • getRegExCaptureGroups

        public static List<String> getRegExCaptureGroups​(String regex,
                                                         String input)
        Retrieves capture groups from a regular expression match inside a String.
        Parameters:
        regex - a regex containing capture groups, as a String
        input - the string to search for matching capture groups
        Returns:
        An ArrayList of Strings representing each capture group in the regex that was matched
      • hasRegExMatch

        public static boolean hasRegExMatch​(String regex,
                                            File file)
                                     throws IOException
        Evaluates a regex pattern against a file to determine if at least one regex match exists
        Parameters:
        regex - a regex pattern, as a String
        file - the file to search for matching substrings
        Returns:
        true if there is at least one regex match, otherwise false
        Throws:
        IOException
      • getIndent

        public static String getIndent​(String line,
                                       int level)
        Infers the indentation style from the given line.
        Parameters:
        line - the line to infer the indentation style from
        level - the level of indentation of the line
        Returns:
        a single indent in the inferred style
      • indentValues

        public static void indentValues​(List<String> values,
                                        int numSpaces)
        Indent the values the desired number of tabs with a variable tab size.
        Parameters:
        values - List of String values to indent
        numSpaces - number of spaces to indent