Class FileSystemUtilities


  • public final class FileSystemUtilities
    extends java.lang.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
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.io.FileFilter EXISTING_DIRECTORY
      FileFilter which accepts Files that exist and for which File.isDirectory() is true.
      static java.io.FileFilter EXISTING_FILE
      FileFilter which accepts Files that exist and for which File.isFile() is true.
      private static java.lang.String[] INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
      Non-valid Characters for naming files, folders under Windows: ":", "*", "?", "\"", "<", ">", "|"
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private FileSystemUtilities()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static void checkAndAdd​(java.util.List<java.io.File> toPopulate, java.io.File current, java.util.List<Filter<java.io.File>> fileFilters, boolean excludeFilterOperation, org.apache.maven.plugin.logging.Log log)  
      static void createDirectory​(java.io.File aDirectory, boolean cleanBeforeCreate)
      Convenience method to successfully create a directory - or throw an exception if failing to create it.
      static java.util.List<java.io.File> filterFiles​(java.io.File baseDir, java.util.List<java.lang.String> sources, java.lang.String standardDirectory, org.apache.maven.plugin.logging.Log log, java.lang.String fileTypeDescription, java.util.List<Filter<java.io.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 java.util.List<java.net.URL> filterFiles​(java.io.File baseDir, java.util.List<java.lang.String> sources, java.util.List<java.lang.String> standardDirectories, org.apache.maven.plugin.logging.Log log, java.lang.String fileTypeDescription, java.util.List<Filter<java.io.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 java.util.List<java.io.File> filterFiles​(java.util.List<java.io.File> files, Filter<java.io.File> acceptFilter, org.apache.maven.plugin.logging.Log log)
      Filters all supplied files using the provided acceptFilter.
      static java.io.File getCanonicalFile​(java.io.File file)
      Acquires the canonical File for the supplied file.
      static java.lang.String getCanonicalPath​(java.io.File file)
      Acquires the canonical path for the supplied file.
      static java.io.File getExistingFile​(java.lang.String path, java.io.File baseDir)
      Retrieves the canonical File matching the supplied path in the following order or priority:
      static java.io.File getFileFor​(java.net.URL anURL, java.lang.String encoding)
      Acquires the file for a supplied URL, provided that its protocol is is either a file or a jar.
      static java.net.URL getUrlFor​(java.io.File aFile)
      Retrieves the URL for the supplied File.
      static java.util.List<java.io.File> listFiles​(java.io.File fileOrDir, java.util.List<Filter<java.io.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 java.util.List<java.io.File> listFiles​(java.io.File fileOrDir, java.util.List<Filter<java.io.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.
      private static void recurseAndPopulate​(java.util.List<java.io.File> toPopulate, java.util.List<Filter<java.io.File>> fileFilters, java.io.File aDirectory, boolean excludeOperation, org.apache.maven.plugin.logging.Log log)  
      static java.lang.String relativize​(java.lang.String path, java.io.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 java.util.List<java.io.File> resolveRecursively​(java.util.List<java.io.File> files, java.util.List<Filter<java.io.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.
      private static void validateFileOrDirectoryName​(java.io.File fileOrDir)  
      • Methods inherited from class java.lang.Object

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

      • EXISTING_FILE

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

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

      • FileSystemUtilities

        private FileSystemUtilities()
    • Method Detail

      • getCanonicalPath

        public static java.lang.String getCanonicalPath​(java.io.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 java.io.File getCanonicalFile​(java.io.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 java.io.File getExistingFile​(java.lang.String path,
                                                   java.io.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 java.net.URL getUrlFor​(java.io.File aFile)
                                      throws java.lang.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:
        java.lang.IllegalArgumentException - if getting the URL yielded a MalformedURLException.
      • getFileFor

        public static java.io.File getFileFor​(java.net.URL anURL,
                                              java.lang.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 java.util.List<java.net.URL> filterFiles​(java.io.File baseDir,
                                                               java.util.List<java.lang.String> sources,
                                                               java.util.List<java.lang.String> standardDirectories,
                                                               org.apache.maven.plugin.logging.Log log,
                                                               java.lang.String fileTypeDescription,
                                                               java.util.List<Filter<java.io.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 java.util.List<java.io.File> filterFiles​(java.io.File baseDir,
                                                               java.util.List<java.lang.String> sources,
                                                               java.lang.String standardDirectory,
                                                               org.apache.maven.plugin.logging.Log log,
                                                               java.lang.String fileTypeDescription,
                                                               java.util.List<Filter<java.io.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 java.util.List<java.io.File> filterFiles​(java.util.List<java.io.File> files,
                                                               Filter<java.io.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 java.util.List<java.io.File> resolveRecursively​(java.util.List<java.io.File> files,
                                                                      java.util.List<Filter<java.io.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​(java.io.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 java.lang.String relativize​(java.lang.String path,
                                                  java.io.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 java.util.List<java.io.File> listFiles​(java.io.File fileOrDir,
                                                             java.util.List<Filter<java.io.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 java.util.List<java.io.File> listFiles​(java.io.File fileOrDir,
                                                             java.util.List<Filter<java.io.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.
      • checkAndAdd

        private static void checkAndAdd​(java.util.List<java.io.File> toPopulate,
                                        java.io.File current,
                                        java.util.List<Filter<java.io.File>> fileFilters,
                                        boolean excludeFilterOperation,
                                        org.apache.maven.plugin.logging.Log log)
      • validateFileOrDirectoryName

        private static void validateFileOrDirectoryName​(java.io.File fileOrDir)
      • recurseAndPopulate

        private static void recurseAndPopulate​(java.util.List<java.io.File> toPopulate,
                                               java.util.List<Filter<java.io.File>> fileFilters,
                                               java.io.File aDirectory,
                                               boolean excludeOperation,
                                               org.apache.maven.plugin.logging.Log log)