public class RepositoryFilenameUtils extends Object
a/b/c.txt --> "" --> relative /a/b/c.txt --> "/" --> absoluteOrigin of code: Apache Commons IO 2.1
Modifier and Type | Method and Description |
---|---|
static String |
concat(String basePath,
String fullFilenameToAdd)
Concatenates a filename to a base path using normal command line style rules.
|
static boolean |
equals(String filename1,
String filename2)
Checks whether two filenames are equal exactly.
|
static boolean |
equalsNormalized(String filename1,
String filename2)
Checks whether two filenames are equal after both have been normalized.
|
static String |
escape(String name,
List<Character> reservedChars)
Performs percent-encoding (as specified in
IUnifiedRepository ) on given name , only encoding
the characters given in reservedChars . |
static String |
getBaseName(String filename)
Gets the base name, minus the full path and extension, from a full filename.
|
static String |
getExtension(String filename)
Gets the extension of a filename.
|
static String |
getFullPath(String filename)
Gets the full path from a full filename, which is the prefix + path.
|
static String |
getFullPathNoEndSeparator(String filename)
Gets the full path from a full filename, which is the prefix + path, and also excluding the final directory
separator.
|
static String |
getName(String filename)
Gets the name minus the path from a full filename.
|
static String |
getPath(String filename)
Gets the path from a full filename, which excludes the prefix.
|
static String |
getPathNoEndSeparator(String filename)
Gets the path from a full filename, which excludes the prefix, and also excluding the final directory
separator.
|
static String |
getPrefix(String filename)
Gets the prefix from a full filename.
|
static int |
getPrefixLength(String filename)
Returns the length of the filename prefix,
The prefix length includes the first slash in the full filename if applicable.
|
static int |
indexOfExtension(String filename)
Returns the index of the last extension separator character, which is a dot.
|
static int |
indexOfLastSeparator(String filename)
Returns the index of the last directory separator character.
|
static boolean |
isExtension(String filename,
Collection extensions)
Checks whether the extension of the filename is one of those specified.
|
static boolean |
isExtension(String filename,
String extension)
Checks whether the extension of the filename is that specified.
|
static boolean |
isExtension(String filename,
String[] extensions)
Checks whether the extension of the filename is one of those specified.
|
static String |
normalize(String filename)
Normalizes a path, removing double and single dot path steps.
|
static String |
normalize(String filename,
boolean leadingSlash)
Normalizes a path, removing double and single dot path steps.
|
static String |
normalizeNoEndSeparator(String filename)
Normalizes a path, removing double and single dot path steps, and removing any final directory separator.
|
static String |
removeExtension(String filename)
Removes the extension from a filename.
|
static String |
separatorsToRepository(String path)
Converts all separators to the Repository (Unix) separator of forward slash.
|
static String |
unescape(String name)
Reverts modifications of
#escape(String) such that for all String s t ,
t.equals(unescape(escape(t))) . |
static boolean |
wildcardMatch(String filename,
String wildcardMatcher)
Checks a filename to see if it matches the specified wildcard matcher, always testing case-sensitive.
|
public static String normalize(String filename)
null
is
returned.
The output will be the same on both Unix and Windows except for the separator character.
/foo// --> /foo/ /foo/./ --> /foo/ /foo/../bar --> /bar /foo/../bar/ --> /bar/ /foo/../bar/../baz --> /baz //foo//./bar --> /foo/bar /../ --> null ../foo --> null foo/bar/.. --> foo/ foo/../../bar --> null foo/../bar --> bar
filename
- the filename to normalize, null returns nullpublic static String normalize(String filename, boolean leadingSlash)
null
is
returned.
The output will be the same on both Unix and Windows except for the separator character.
/foo// --> /foo/ /foo/./ --> /foo/ /foo/../bar --> /bar /foo/../bar/ --> /bar/ /foo/../bar/../baz --> /baz //foo//./bar --> /foo/bar /../ --> null ../foo --> null foo/bar/.. --> foo/ foo/../../bar --> null foo/../bar --> bar
filename
- the filename to normalize, null returns nullleadingSlash
- will ensue there is a leading slash on the result if true
public static String normalizeNoEndSeparator(String filename)
null
is returned.
The output will be the same on both Unix and Windows except for the separator character.
/foo// --> /foo /foo/./ --> /foo /foo/../bar --> /bar /foo/../bar/ --> /bar /foo/../bar/../baz --> /baz //foo//./bar --> /foo/bar /../ --> null ../foo --> null foo/bar/.. --> foo foo/../../bar --> null foo/../bar --> bar
filename
- the filename to normalize, null returns nullpublic static String concat(String basePath, String fullFilenameToAdd)
normalize(String)
, thus ..
is handled.
If pathToAdd
is absolute (has an absolute prefix), then it will be normalized and returned.
Otherwise, the paths will be joined, normalized and returned.
/foo/ + bar --> /foo/bar /foo + bar --> /foo/bar /foo + /bar --> /bar /foo/a/ + ../bar --> foo/bar /foo/ + ../../bar --> null /foo/ + /bar --> /bar /foo/.. + /bar --> /bar /foo + bar/c.txt --> /foo/bar/c.txt /foo/c.txt + bar --> /foo/c.txt/bar (!)(!) Note that the first parameter must be a path. If it ends with a name, then the name will be built into the concatenated path. If this might be a problem, use
getFullPath(String)
on the base path argument.basePath
- the base path to attach to, always treated as a pathfullFilenameToAdd
- the filename (or path) to attach to the basepublic static String separatorsToRepository(String path)
path
- the path to be changed, null ignoredpublic static int getPrefixLength(String filename)
a/b/c.txt --> "" --> relative /a/b/c.txt --> "/" --> absolute
filename
- the filename to find the prefix in, null returns -1public static int indexOfLastSeparator(String filename)
filename
- the filename to find the last path separator in, null returns -1public static int indexOfExtension(String filename)
indexOfLastSeparator(String)
filename
- the filename to find the last path separator in, null returns -1public static String getPrefix(String filename)
a/b/c.txt --> "" --> relative /a/b/c.txt --> "/" --> absolute
filename
- the filename to query, null returns nullpublic static String getPath(String filename)
a.txt --> "" a/b/c --> a/b/ a/b/c/ --> a/b/c/ /a.txt --> "" /a/b/c --> a/b/ /a/b/c/ --> a/b/c/This method drops the prefix from the result. See
getFullPath(String)
for the method that retains the
prefix.filename
- the filename to query, null returns nullpublic static String getPathNoEndSeparator(String filename)
a.txt --> "" a/b/c --> a/b a/b/c/ --> a/b/c /a.txt --> "" /a/b/c --> a/b /a/b/c/ --> a/b/cThis method drops the prefix from the result. See
getFullPathNoEndSeparator(String)
for the method
that retains the prefix.filename
- the filename to query, null returns nullpublic static String getFullPath(String filename)
a.txt --> "" a/b/c --> a/b/ a/b/c/ --> a/b/c/ /a.txt --> / /a/b/c --> /a/b/ /a/b/c/ --> /a/b/c/The output will be the same irrespective of the machine that the code is running on.
filename
- the filename to query, null returns nullpublic static String getFullPathNoEndSeparator(String filename)
a.txt --> "" a/b/c --> a/b a/b/c/ --> a/b/c /a.txt --> / /a/b/c --> /a/b /a/b/c/ --> /a/b/cThe output will be the same irrespective of the machine that the code is running on.
filename
- the filename to query, null returns nullpublic static String getName(String filename)
a/b/c.txt --> c.txt a.txt --> a.txt a/b/c --> c a/b/c/ --> ""
filename
- the filename to query, null returns nullpublic static String getBaseName(String filename)
a/b/c.txt --> c a.txt --> a a/b/c --> c a/b/c/ --> ""
filename
- the filename to query, null returns nullpublic static String getExtension(String filename)
foo.txt --> "txt" a/b/c.jpg --> "jpg" a/b.txt/c --> "" a/b/c --> ""
filename
- the filename to retrieve the extension of.public static String removeExtension(String filename)
foo.txt --> foo a/b/c.jpg --> a/b/c a/b/c --> a/b/c a.b/c --> a.b/c
filename
- the filename to query, null returns nullpublic static boolean equals(String filename1, String filename2)
filename1
- the first filename to query, may be nullfilename2
- the second filename to query, may be nullIOCase.SENSITIVE
public static boolean equalsNormalized(String filename1, String filename2)
normalize(String)
. The check is then performed in a case-sensitive
manner.filename1
- the first filename to query, may be nullfilename2
- the second filename to query, may be nullIOCase.SENSITIVE
public static boolean isExtension(String filename, String extension)
filename
- the filename to query, null returns falseextension
- the extension to check for, null or empty checks for no extensionpublic static boolean isExtension(String filename, String[] extensions)
filename
- the filename to query, null returns falseextensions
- the extensions to check for, null checks for no extensionpublic static boolean isExtension(String filename, Collection extensions)
filename
- the filename to query, null returns falseextensions
- the extensions to check for, null checks for no extensionpublic static boolean wildcardMatch(String filename, String wildcardMatcher)
wildcardMatch("c.txt", "*.txt") --> true wildcardMatch("c.txt", "*.jpg") --> false wildcardMatch("a/b/c.txt", "a/b/*") --> true wildcardMatch("c.txt", "*.???") --> true wildcardMatch("c.txt", "*.????") --> false
filename
- the filename to match onwildcardMatcher
- the wildcard string to match againstIOCase.SENSITIVE
public static String escape(String name, List<Character> reservedChars)
IUnifiedRepository
) on given name
, only encoding
the characters given in reservedChars
. Assumes only ASCII characters in reservedChars.name
- name to escapereservedChars
- chars within name to escape