Package org.codehaus.mojo.jaxb2.shared
Class FileSystemUtilities
java.lang.Object
org.codehaus.mojo.jaxb2.shared.FileSystemUtilities
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
FieldsModifier and TypeFieldDescriptionstatic final FileFilter
FileFilter which accepts Files that exist and for whichFile.isDirectory()
istrue
.static final FileFilter
FileFilter which accepts Files that exist and for whichFile.isFile()
istrue
.private static final String[]
Non-valid Characters for naming files, folders under Windows:":", "*", "?", "\"", "invalid input: '<'", ">", "|"
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static void
checkAndAdd
(List<File> toPopulate, File current, List<Filter<File>> fileFilters, boolean excludeFilterOperation, org.apache.maven.plugin.logging.Log log) static void
createDirectory
(File aDirectory, boolean cleanBeforeCreate) Convenience method to successfully create a directory - or throw an exception if failing to create it.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.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.filterFiles
(List<File> files, Filter<File> acceptFilter, org.apache.maven.plugin.logging.Log log) Filters all supplied files using the providedacceptFilter
.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
Retrieves the URL for the supplied 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 File, it is added to the returned List if any of the filters Match.private static void
recurseAndPopulate
(List<File> toPopulate, List<Filter<File>> fileFilters, File aDirectory, boolean excludeOperation, org.apache.maven.plugin.logging.Log log) 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.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.private static void
validateFileOrDirectoryName
(File fileOrDir)
-
Field Details
-
EXISTING_FILE
FileFilter which accepts Files that exist and for whichFile.isFile()
istrue
. -
EXISTING_DIRECTORY
FileFilter which accepts Files that exist and for whichFile.isDirectory()
istrue
. -
INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
Non-valid Characters for naming files, folders under Windows:":", "*", "?", "\"", "invalid input: '<'", ">", "|"
- See Also:
-
-
Constructor Details
-
FileSystemUtilities
private FileSystemUtilities()
-
-
Method Details
-
getCanonicalPath
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
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
Retrieves the canonical File matching the supplied path in the following order or priority:
- 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. - 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.
- Absolute path: The path is used by itself (i.e.
-
getUrlFor
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
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 isrelativize()
-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.
-
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 thefiles
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
- iftrue
, 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
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 initialFile#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
- iftrue
, 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
-
validateFileOrDirectoryName
-
recurseAndPopulate
-