public class PathUtils
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
PathUtils.PathComparator
Comparator for normalized paths.
|
Modifier and Type | Field and Description |
---|---|
static java.util.Comparator<java.lang.String> |
PATH_COMPARATOR
Singleton instance of
PathUtils.PathComparator . |
Constructor and Description |
---|
PathUtils() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
checkAndNormalizeRootPath(java.lang.String path)
Resolve all ".." elements of a path.
|
static boolean |
checkCase(java.io.File file,
java.lang.String pathToTest)
Tell if the case of the characters of the path of a file match a path,
taking into account whether the current environment is case sensitive.
|
static boolean |
containsSymbol(java.lang.String s)
Tell if a path contains a symbolic substitution.
|
static java.util.Set<java.io.File> |
fixPathFiles(java.util.Collection<java.io.File> files)
Collect the results of applying
fixPathString(File) to
a collection of files. |
static java.lang.String |
fixPathString(java.io.File absPath)
Answer the canonical path of a file.
|
static java.lang.String |
fixPathString(java.lang.String absPath)
Answer a canonical path for a target path using
fixPathString(File) . |
static java.lang.String |
getChildUnder(java.lang.String path,
java.lang.String parentPath)
Answer the first path element of a path which follows a leading sub-path.
|
static java.lang.String |
getFirstPathComponent(java.lang.String path)
Answer the first file name of a path, using the forward slash character ('/')
as the path separator character.
|
static java.util.Set<java.io.File> |
getFixedPathFiles(java.util.Collection<java.lang.String> paths)
Apply
fixPathString(File) to a collection of paths. |
static java.lang.String |
getName(java.lang.String pathAndName)
Answer the last file name of a path, using the forward slash ('/') as the
path separator character.
|
static java.lang.String |
getParent(java.lang.String path)
Answer the path with the last file name removed, using forward
slash ('/') as the path separator character.
|
static java.lang.String |
getSymbol(java.lang.String s)
Answer the first symbolic substitution within a path.
|
static boolean |
isNormalizedPathAbsolute(java.lang.String nPath)
Tell if a path, if applied to a target location, will reach above the
target location.
|
static boolean |
isSymbol(java.lang.String s)
Tell if a path starts with a symbolic substitution.
|
static boolean |
isUnixStylePathAbsolute(java.lang.String unixStylePath)
Tell if a path, if applied to a target location, will reach above
the target location.
|
static java.lang.String |
normalize(java.lang.String path)
Normalize a path.
|
static java.lang.String |
normalizeDescendentPath(java.lang.String path)
Normalize a relative path using
normalize(String) and verify that
the normalized path does not begin with an upwards path element (".."). |
static java.lang.String |
normalizeRelative(java.lang.String relativePath)
Normalize a relative path using
normalize(String) and verify that the normalized path is
an absolute path. |
static java.lang.String |
normalizeUnixStylePath(java.lang.String path)
Normalize a path, resolving as many ".." elements as possible.
|
static boolean |
pathIsAbsolute(java.lang.String normalizedPath)
Tell if a normalized path is an absolute path.
|
static java.lang.String |
replaceRestrictedCharactersInFileName(java.lang.String name)
Copy a file name, replacing each restricted character with a single period
character ('.').
|
static java.lang.String |
slashify(java.lang.String filePath)
Copy the path, replacing backward slashes ('\\') with forward slashes ('/').
|
public static final java.util.Comparator<java.lang.String> PATH_COMPARATOR
PathUtils.PathComparator
.public static java.lang.String slashify(java.lang.String filePath)
path
- The path in which to replace slashes. An exception
will be thrown if the path is null.public static java.lang.String normalizeDescendentPath(java.lang.String path)
normalize(String)
and verify that
the normalized path does not begin with an upwards path element ("..").
The path is required to be a relative path which uses forward slashes ('/').
The normalized path is tested as an absolute path according to pathIsAbsolute(String)
.relativePath
- The relative path which is to be normalized.MalformedLocationException
- Thrown if the normalized path starts with an upwards path element ("..").public static java.lang.String normalizeRelative(java.lang.String relativePath)
normalize(String)
and verify that the normalized path is
an absolute path. Answer the normalized path.
The path is required to be a relative path which uses forward slashes ('/').
The normalized path is an absolute path according to the rules implemented by pathIsAbsolute(String)
.relativePath
- The relative path which is to be normalized. An exception will
be thrown if the path is null.MalformedLocationException
- Thrown if the normalized path is not an absolute path.public static boolean pathIsAbsolute(java.lang.String normalizedPath)
isSymbol(String)
is absolute.
A true result for paths which start with a symbolic substitution is a conservative
answer: Whether the resulting path is absolute after performing symbolic substitution
depends on what value was placed by the substitution.
A normalized path which starts with a drive letter combination followed by a forward
slash character, for example, "C\:/", is absolute.
A normalized path which starts with a protocol followed by a forward slash character,
for example, "file:/", is absolute.
Otherwise, the path is not absolute.normalizedPath
- The normalized path which is to be tested.
An exception will be thrown if the path is null.public static java.lang.String normalize(java.lang.String path)
path
- The path which is to be normalized. An exception will be thrown if the path is null.public static boolean isSymbol(java.lang.String s)
path
- The path to test for a symbolic substitution.public static boolean containsSymbol(java.lang.String s)
path
- The path to test for a symbolic substitution.public static java.lang.String getSymbol(java.lang.String s)
containsSymbol(String)
for a description of a symbolic substitution.
Answer null if the path is null or if the path contains no symbolic substitution.
For example, for "leading/trailing" answer null.
For example, for "leading/\$\{A\}"/trailing" answer "\$\{A\}".
For example, for "leading/\$\{A\}"/inner/\$\{B\}/trailing" answer "\$\{A\}".
Paths with badly formed substitutions answer unpredictable values. For example,
for "leading\$\{A/inner/\$\{B\}/trailing" answer "\$\{A/inner/\$\{B\}".path
- The path from which to obtain the first symbol.public static java.lang.String getParent(java.lang.String path)
path
- The path with the last file named removed. An exception
will be thrown if the path is null.public static java.lang.String getName(java.lang.String pathAndName)
path
- The path from which to answer the last file name.public static java.lang.String getFirstPathComponent(java.lang.String path)
path
- The path from which to answer the first file name.
An exception will be thrown if the path is null.public static java.lang.String getChildUnder(java.lang.String path, java.lang.String parentPath)
path
- The path from which to obtain a path element.leadingPath
- A leading sub-path of the target path.public static boolean isUnixStylePathAbsolute(java.lang.String unixStylePath)
path
- The path which is to be tested.public static boolean isNormalizedPathAbsolute(java.lang.String nPath)
normalizedPath
- The path which is to be tested.public static java.lang.String checkAndNormalizeRootPath(java.lang.String path) throws java.lang.IllegalArgumentException
isUnixStylePathAbsolute(String)
.
Add a leading slash if the path does not have one.
Remove any trailing slash.
Verify that the resulting path is neither empty nor a single slash character.path
- The path which is to be normalized.java.lang.IllegalArgumentException
- If the resolved path is empty or has just a single slash,
or if the resolved path reaches above target locations.public static java.lang.String normalizeUnixStylePath(java.lang.String path)
path
- A path which is to be normalized.public static boolean checkCase(java.io.File file, java.lang.String pathToTest)
file
- The file which is to be tested.pathToTest
- The path which is to be tested against the file.public static java.lang.String replaceRestrictedCharactersInFileName(java.lang.String name)
< > : " / \\ | ? *(See
FILE_NAME_RESTRICTED_CHARS
.
Control characters are character with a value greater than or equal to zero
and less than or equal to 31.
The set of restricted characters was selected conservatively based on the
rules for Windows file names.
The parameter should be a file name. Both the forward slash ('/') and
the backwards slash ('\\') are restricted characters.
Answer null if all characters of the file name are replaced, or if the resulting
file name is "." or "..".name
- The file name in which to replace restricted characters. An exception
will be thrown if the file name is null.public static java.lang.String fixPathString(java.lang.String absPath)
fixPathString(File)
.targetPath
- The path for which to obtain a canonical path.public static java.lang.String fixPathString(java.io.File absPath)
File.getCanonicalPath()
. If that
fails, obtain the canonical value using File.getAbsolutePath()
. On
a non-windows environment, obtain the canonical value using File.getAbsolutePath()
.
A failure of File.getCanonicalPath()
causes an FFDC exception
report to be generated. However, processing continues with the call to File.getAbsolutePath()
.
Use of canonical paths enables the use of java case sensitive string comparisons
for file name comparisons. See IS_POSSIBLY_CASE_INSENSITIVE
.targetFile
- The file for which to answer the canonical path.public static java.util.Set<java.io.File> getFixedPathFiles(java.util.Collection<java.lang.String> paths)
fixPathString(File)
to a collection of paths. Create a file
on each of the adjusted paths. Collect and return the files created on the
adjusted paths.
Answer an empty collection if the paths collection is null.paths
- The paths to which to apply fixPathString(String)
.public static java.util.Set<java.io.File> fixPathFiles(java.util.Collection<java.io.File> files)
fixPathString(File)
to
a collection of files.
Answer an empty collection if the file collection is null.files
- The files to which to apply fixPathString(File)
.