Class FileSystemUtilities


  • public final class FileSystemUtilities
    extends Object
    The Jaxb2 Maven Plugin needs to fiddle with the filesystem a great deal, to create and optionally prune directories or detect/create various files. This utility class contains all such algorithms, and serves as an entry point to any Plexus Utils methods.
    Since:
    2.0
    Author:
    Lennart Jörelid
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static FileFilter EXISTING_DIRECTORY
      FileFilter which accepts Files that exist and for which File.isDirectory() is true.
      static FileFilter EXISTING_FILE
      FileFilter which accepts Files that exist and for which File.isFile() is true.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void createDirectory​(File aDirectory, boolean cleanBeforeCreate)
      Convenience method to successfully create a directory - or throw an exception if failing to create it.
      static List<File> filterFiles​(File baseDir, List<String> sources, String standardDirectory, org.apache.maven.plugin.logging.Log log, String fileTypeDescription, List<Filter<File>> excludeFilters)
      Filters files found either in the sources paths (or in the standardDirectory if no explicit sources are given), and retrieves a List holding those files that do not match any of the supplied Java Regular Expression excludePatterns.
      static List<URL> filterFiles​(File baseDir, List<String> sources, List<String> standardDirectories, org.apache.maven.plugin.logging.Log log, String fileTypeDescription, List<Filter<File>> excludePatterns)
      Filters files found either in the sources paths (or in the standardDirectory if no explicit sources are given), and retrieves a List holding those files that do not match any of the supplied Java Regular Expression excludePatterns.
      static List<File> filterFiles​(List<File> files, Filter<File> acceptFilter, org.apache.maven.plugin.logging.Log log)
      Filters all supplied files using the provided acceptFilter.
      static File getCanonicalFile​(File file)
      Acquires the canonical File for the supplied file.
      static String getCanonicalPath​(File file)
      Acquires the canonical path for the supplied file.
      static File getExistingFile​(String path, File baseDir)
      Retrieves the canonical File matching the supplied path in the following order or priority:
      static File getFileFor​(URL anURL, String encoding)
      Acquires the file for a supplied URL, provided that its protocol is is either a file or a jar.
      static URL getUrlFor​(File aFile)
      Retrieves the URL for the supplied File.
      static List<File> listFiles​(File fileOrDir, List<Filter<File>> fileFilters, boolean excludeFilterOperation, org.apache.maven.plugin.logging.Log log)
      If the supplied fileOrDir is a File, it is added to the returned List if any of the filters Match.
      static List<File> listFiles​(File fileOrDir, List<Filter<File>> fileFilters, org.apache.maven.plugin.logging.Log log)
      If the supplied fileOrDir is a File, it is added to the returned List if any of the filters Match.
      static String relativize​(String path, File parentDir, boolean removeInitialFileSep)
      If the supplied path refers to a file or directory below the supplied basedir, the returned path is identical to the part below the basedir.
      static List<File> resolveRecursively​(List<File> files, List<Filter<File>> exclusionFilters, org.apache.maven.plugin.logging.Log log)
      Retrieves a List of Files containing all the existing files within the supplied files List, including all files found in directories recursive to any directories provided in the files list.
    • Field Detail

      • EXISTING_FILE

        public static final FileFilter EXISTING_FILE
        FileFilter which accepts Files that exist and for which File.isFile() is true.
      • EXISTING_DIRECTORY

        public static final FileFilter EXISTING_DIRECTORY
        FileFilter which accepts Files that exist and for which File.isDirectory() is true.
    • Method Detail

      • getCanonicalPath

        public static String getCanonicalPath​(File file)
        Acquires the canonical path for the supplied file.
        Parameters:
        file - A non-null File for which the canonical path should be retrieved.
        Returns:
        The canonical path of the supplied file.
      • getCanonicalFile

        public static File getCanonicalFile​(File file)
        Acquires the canonical File for the supplied file.
        Parameters:
        file - A non-null File for which the canonical File should be retrieved.
        Returns:
        The canonical File of the supplied file.
      • getExistingFile

        public static File getExistingFile​(String path,
                                           File baseDir)

        Retrieves the canonical File matching the supplied path in the following order or priority:

        1. Absolute path: The path is used by itself (i.e. new File(path);). If an existing file or directory matches the provided path argument, its canonical path will be returned.
        2. Relative path: The path is appended to the baseDir (i.e. new File(baseDir, path);). If an existing file or directory matches the provided path argument, its canonical path will be returned. Only in this case will be baseDir argument be considered.

        If no file or directory could be derived from the supplied path and baseDir, null is returned.

        Parameters:
        path - A non-null path which will be used to find an existing file or directory.
        baseDir - A directory to which the path will be appended to search for the existing file or directory in case the file was nonexistent when interpreted as an absolute path.
        Returns:
        either a canonical File for the path, or null if no file or directory matched the supplied path and baseDir.
      • getUrlFor

        public static URL getUrlFor​(File aFile)
                             throws IllegalArgumentException
        Retrieves the URL for the supplied File. Convenience method which hides exception handling for the operation in question.
        Parameters:
        aFile - A File for which the URL should be retrieved.
        Returns:
        The URL for the supplied aFile.
        Throws:
        IllegalArgumentException - if getting the URL yielded a MalformedURLException.
      • getFileFor

        public static File getFileFor​(URL anURL,
                                      String encoding)
        Acquires the file for a supplied URL, provided that its protocol is is either a file or a jar.
        Parameters:
        anURL - a non-null URL.
        encoding - The encoding to be used by the URLDecoder to decode the path found.
        Returns:
        The File pointing to the supplied URL, for file or jar protocol URLs and null otherwise.
      • filterFiles

        public static List<URL> filterFiles​(File baseDir,
                                            List<String> sources,
                                            List<String> standardDirectories,
                                            org.apache.maven.plugin.logging.Log log,
                                            String fileTypeDescription,
                                            List<Filter<File>> excludePatterns)
        Filters files found either in the sources paths (or in the standardDirectory if no explicit sources are given), and retrieves a List holding those files that do not match any of the supplied Java Regular Expression excludePatterns.
        Parameters:
        baseDir - The non-null basedir Directory.
        sources - The sources which should be either absolute or relative (to the given baseDir) paths to files or to directories that should be searched recursively for files.
        standardDirectories - If no sources are given, revert to searching all files under these standard directories. Each path is relativize()-d to the supplied baseDir to reach a directory path.
        log - A non-null Maven Log for logging any operations performed.
        fileTypeDescription - A human-readable short description of what kind of files are searched for, such as "xsdSources" or "xjbSources".
        excludePatterns - An optional List of patterns used to construct an ExclusionRegExpFileFilter used to identify files which should be excluded from the result.
        Returns:
        URLs to all Files under the supplied sources (or standardDirectories, if no explicit sources are given) which do not match the supplied Java Regular excludePatterns.
      • filterFiles

        public static List<File> filterFiles​(File baseDir,
                                             List<String> sources,
                                             String standardDirectory,
                                             org.apache.maven.plugin.logging.Log log,
                                             String fileTypeDescription,
                                             List<Filter<File>> excludeFilters)
        Filters files found either in the sources paths (or in the standardDirectory if no explicit sources are given), and retrieves a List holding those files that do not match any of the supplied Java Regular Expression excludePatterns.
        Parameters:
        baseDir - The non-null basedir Directory.
        sources - The sources which should be either absolute or relative (to the given baseDir) paths to files or to directories that should be searched recursively for files.
        standardDirectory - If no sources are given, revert to searching all files under this standard directory. This is the path appended to the baseDir to reach a directory.
        log - A non-null Maven Log for logging any operations performed.
        fileTypeDescription - A human-readable short description of what kind of files are searched for, such as "xsdSources" or "xjbSources".
        excludeFilters - An optional List of Filters used to identify files which should be excluded from the result.
        Returns:
        All files under the supplied sources (or standardDirectory, if no explicit sources are given) which do not match the supplied Java Regular excludePatterns.
      • filterFiles

        public static List<File> filterFiles​(List<File> files,
                                             Filter<File> acceptFilter,
                                             org.apache.maven.plugin.logging.Log log)
        Filters all supplied files using the provided acceptFilter.
        Parameters:
        files - The list of files to resolve, filter and return. If the files List contains directories, they are searched for Files recursively. Any found Files in such a search are included in the resulting File List if they match the acceptFilter supplied.
        acceptFilter - A filter matched to all files in the given List. If the acceptFilter matches a file, it is included in the result.
        log - The active Maven Log.
        Returns:
        All files in (or files in subdirectories of directories provided in) the files List, provided that each file is accepted by an ExclusionRegExpFileFilter.
      • resolveRecursively

        public static List<File> resolveRecursively​(List<File> files,
                                                    List<Filter<File>> exclusionFilters,
                                                    org.apache.maven.plugin.logging.Log log)
        Retrieves a List of Files containing all the existing files within the supplied files List, including all files found in directories recursive to any directories provided in the files list. Each file included in the result must pass an ExclusionRegExpFileFilter synthesized from the supplied exclusions pattern(s).
        Parameters:
        files - The list of files to resolve, filter and return. If the files List contains directories, they are searched for Files recursively. Any found Files in such a search are included in the resulting File List if they do not match any of the exclusionFilters supplied.
        exclusionFilters - A List of Filters which identify files to remove from the result - implying that any File matched by any of these exclusionFilters will not be included in the result.
        log - The active Maven Log.
        Returns:
        All files in (or files in subdirectories of directories provided in) the files List, provided that each file is accepted by an ExclusionRegExpFileFilter.
      • createDirectory

        public static void createDirectory​(File aDirectory,
                                           boolean cleanBeforeCreate)
                                    throws org.apache.maven.plugin.MojoExecutionException
        Convenience method to successfully create a directory - or throw an exception if failing to create it.
        Parameters:
        aDirectory - The directory to create.
        cleanBeforeCreate - if true, the directory and all its content will be deleted before being re-created. This will ensure that the created directory is really clean.
        Throws:
        org.apache.maven.plugin.MojoExecutionException - if the aDirectory could not be created (and/or cleaned).
      • relativize

        public static String relativize​(String path,
                                        File parentDir,
                                        boolean removeInitialFileSep)
        If the supplied path refers to a file or directory below the supplied basedir, the returned path is identical to the part below the basedir.
        Parameters:
        path - The path to strip off basedir path from, and return.
        parentDir - The maven project basedir.
        removeInitialFileSep - If true, an initial File#separator is removed before returning.
        Returns:
        The path relative to basedir, if it is situated below the basedir. Otherwise the supplied path.
      • listFiles

        public static List<File> listFiles​(File fileOrDir,
                                           List<Filter<File>> fileFilters,
                                           org.apache.maven.plugin.logging.Log log)
        If the supplied fileOrDir is a File, it is added to the returned List if any of the filters Match. If the supplied fileOrDir is a Directory, it is listed and any of the files immediately within the fileOrDir directory are returned within the resulting List provided that they match any of the supplied filters.
        Parameters:
        fileOrDir - A File or Directory.
        fileFilters - A List of filter of which at least one must match to add the File (or child Files, in case of a directory) to the resulting List.
        log - The active Maven Log
        Returns:
        A List holding the supplied File (or child Files, in case fileOrDir is a Directory) given that at least one Filter accepts them.
      • listFiles

        public static List<File> listFiles​(File fileOrDir,
                                           List<Filter<File>> fileFilters,
                                           boolean excludeFilterOperation,
                                           org.apache.maven.plugin.logging.Log log)
        If the supplied fileOrDir is a File, it is added to the returned List if any of the filters Match. If the supplied fileOrDir is a Directory, it is listed and any of the files immediately within the fileOrDir directory are returned within the resulting List provided that they match any of the supplied filters.
        Parameters:
        fileOrDir - A File or Directory.
        fileFilters - A List of filter of which at least one must match to add the File (or child Files, in case of a directory) to the resulting List.
        excludeFilterOperation - if true, all fileFilters are considered exclude filter, i.e. if any of the Filters match the fileOrDir, that fileOrDir will be excluded from the result.
        log - The active Maven Log
        Returns:
        A List holding the supplied File (or child Files, in case fileOrDir is a Directory) given that at least one Filter accepts them.