Package org.pentaho.platform.repository
Class RepositoryFilenameUtils
java.lang.Object
org.pentaho.platform.repository.RepositoryFilenameUtils
General filename and filepath manipulation utilities for the Hitachi Vantara Repository. NOTE: these methods will work
independently of the underlying operating system. Most methods will translate a backslash (\) to a forward slash (/)
but should be be depended upon to make that translation.
This class defines six components within a filename (example /dev/project/file.txt):
- the prefix - /
- the path - dev/project/
- the full path - /dev/project/
- the name - file.txt
- the base name - file
- the extension - txt
a/b/c.txt --> "" --> relative /a/b/c.txt --> "/" --> absoluteOrigin of code: Apache Commons IO 2.1
- Since:
- Pentaho 5.0
- Author:
- David M. Kincade
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
Concatenates a filename to a base path using normal command line style rules.static boolean
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
Performs percent-encoding (as specified inIUnifiedRepository
) on givenname
, only encoding the characters given inreservedChars
.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
Gets the name minus the path from a full filename.static String
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
Gets the prefix from a full filename.static int
getPrefixLength
(String filename) Returns the length of the filename prefix,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, 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 boolean
isExtension
(String filename, Collection extensions) Checks whether the extension of the filename is one of those specified.static String
Normalizes a path, removing double and single dot path steps.static String
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
Reverts modifications of#escape(String)
such that for allString
st
,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.
-
Method Details
-
normalize
Normalizes a path, removing double and single dot path steps. This method normalizes a path to a standard format. A trailing slash will be retained. A double slash will be merged to a single slash (but UNC names are handled). A single dot path segment will be removed. A double dot will cause that path segment and the one before to be removed. If the double dot has no parent path segment to work with,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
- Parameters:
filename
- the filename to normalize, null returns null- Returns:
- the normalized filename, or null if invalid
-
normalize
Normalizes a path, removing double and single dot path steps. This method normalizes a path to a standard format. A trailing slash will be retained. A double slash will be merged to a single slash (but UNC names are handled). A single dot path segment will be removed. A double dot will cause that path segment and the one before to be removed. If the double dot has no parent path segment to work with,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
- Parameters:
filename
- the filename to normalize, null returns nullleadingSlash
- will ensue there is a leading slash on the result iftrue
- Returns:
- the normalized filename, or null if invalid
-
normalizeNoEndSeparator
Normalizes a path, removing double and single dot path steps, and removing any final directory separator. This method normalizes a path to a standard format. A trailing slash will be removed. A double slash will be merged to a single slash (but UNC names are handled). A single dot path segment will be removed. A double dot will cause that path segment and the one before to be removed. If the double dot has no parent path segment to work with,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
- Parameters:
filename
- the filename to normalize, null returns null- Returns:
- the normalized filename, or null if invalid
-
concat
Concatenates a filename to a base path using normal command line style rules. The effect is equivalent to resultant directory after changing directory to the first argument, followed by changing directory to the second argument. The first argument is the base path, the second is the path to concatenate. The returned path is always normalized vianormalize(String)
, thus..
is handled. IfpathToAdd
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, usegetFullPath(String)
on the base path argument.- Parameters:
basePath
- the base path to attach to, always treated as a pathfullFilenameToAdd
- the filename (or path) to attach to the base- Returns:
- the concatenated path, or null if invalid
-
separatorsToRepository
Converts all separators to the Repository (Unix) separator of forward slash.- Parameters:
path
- the path to be changed, null ignored- Returns:
- the updated path
-
getPrefixLength
Returns the length of the filename prefix, The prefix length includes the first slash in the full filename if applicable. Thus, it is possible that the length returned is greater than the length of the input string.a/b/c.txt --> "" --> relative /a/b/c.txt --> "/" --> absolute
- Parameters:
filename
- the filename to find the prefix in, null returns -1- Returns:
- the length of the prefix, -1 if invalid or null
-
indexOfLastSeparator
Returns the index of the last directory separator character. The position of the last forward or backslash is returned.- Parameters:
filename
- the filename to find the last path separator in, null returns -1- Returns:
- the index of the last separator character, or -1 if there is no such character
-
indexOfExtension
Returns the index of the last extension separator character, which is a dot. This method also checks that there is no directory separator after the last dot. To do this it usesindexOfLastSeparator(String)
- Parameters:
filename
- the filename to find the last path separator in, null returns -1- Returns:
- the index of the last separator character, or -1 if there is no such character
-
getPrefix
Gets the prefix from a full filename. The prefix includes the first slash in the full filename where applicable.a/b/c.txt --> "" --> relative /a/b/c.txt --> "/" --> absolute
- Parameters:
filename
- the filename to query, null returns null- Returns:
- the prefix of the file, null if invalid
-
getPath
Gets the path from a full filename, which excludes the prefix. The method is entirely text based, and returns the text before and including the last forward or backslash.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. SeegetFullPath(String)
for the method that retains the prefix.- Parameters:
filename
- the filename to query, null returns null- Returns:
- the path of the file, an empty string if none exists, null if invalid
-
getPathNoEndSeparator
Gets the path from a full filename, which excludes the prefix, and also excluding the final directory separator. The method is entirely text based, and returns the text before the last forward or backslash.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. SeegetFullPathNoEndSeparator(String)
for the method that retains the prefix.- Parameters:
filename
- the filename to query, null returns null- Returns:
- the path of the file, an empty string if none exists, null if invalid
-
getFullPath
Gets the full path from a full filename, which is the prefix + path. The method is entirely text based, and returns the text before and including the last forward or backslash.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.- Parameters:
filename
- the filename to query, null returns null- Returns:
- the path of the file, an empty string if none exists, null if invalid
-
getFullPathNoEndSeparator
Gets the full path from a full filename, which is the prefix + path, and also excluding the final directory separator. This method will handle a file in either Unix or Windows format. The method is entirely text based, and returns the text before the last forward or backslash.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.- Parameters:
filename
- the filename to query, null returns null- Returns:
- the path of the file, an empty string if none exists, null if invalid
-
getName
Gets the name minus the path from a full filename. The text after the last forward or backslash is returned.a/b/c.txt --> c.txt a.txt --> a.txt a/b/c --> c a/b/c/ --> ""
- Parameters:
filename
- the filename to query, null returns null- Returns:
- the name of the file without the path, or an empty string if none exists
-
getBaseName
Gets the base name, minus the full path and extension, from a full filename. The text after the last forward or backslash and before the last dot is returned.a/b/c.txt --> c a.txt --> a a/b/c --> c a/b/c/ --> ""
- Parameters:
filename
- the filename to query, null returns null- Returns:
- the name of the file without the path, or an empty string if none exists
-
getExtension
Gets the extension of a filename. This method returns the textual part of the filename after the last dot. There must be no directory separator after the dot.foo.txt --> "txt" a/b/c.jpg --> "jpg" a/b.txt/c --> "" a/b/c --> ""
- Parameters:
filename
- the filename to retrieve the extension of.- Returns:
- the extension of the file or an empty string if none exists.
-
removeExtension
Removes the extension from a filename. This method returns the textual part of the filename before the last dot. There must be no directory separator after the dot.foo.txt --> foo a/b/c.jpg --> a/b/c a/b/c --> a/b/c a.b/c --> a.b/c
- Parameters:
filename
- the filename to query, null returns null- Returns:
- the filename minus the extension
-
equals
Checks whether two filenames are equal exactly. No processing is performed on the filenames other than comparison, thus this is merely a null-safe case-sensitive equals.- Parameters:
filename1
- the first filename to query, may be nullfilename2
- the second filename to query, may be null- Returns:
- true if the filenames are equal, null equals null
- See Also:
-
IOCase.SENSITIVE
-
equalsNormalized
Checks whether two filenames are equal after both have been normalized. Both filenames are first passed tonormalize(String)
. The check is then performed in a case-sensitive manner.- Parameters:
filename1
- the first filename to query, may be nullfilename2
- the second filename to query, may be null- Returns:
- true if the filenames are equal, null equals null
- See Also:
-
IOCase.SENSITIVE
-
isExtension
Checks whether the extension of the filename is that specified. This method obtains the extension as the textual part of the filename after the last dot. There must be no directory separator after the dot. The extension check is case-sensitive on all platforms.- Parameters:
filename
- the filename to query, null returns falseextension
- the extension to check for, null or empty checks for no extension- Returns:
- true if the filename has the specified extension
-
isExtension
Checks whether the extension of the filename is one of those specified. This method obtains the extension as the textual part of the filename after the last dot. There must be no directory separator after the dot. The extension check is case-sensitive on all platforms.- Parameters:
filename
- the filename to query, null returns falseextensions
- the extensions to check for, null checks for no extension- Returns:
- true if the filename is one of the extensions
-
isExtension
Checks whether the extension of the filename is one of those specified. This method obtains the extension as the textual part of the filename after the last dot. There must be no directory separator after the dot. The extension check is case-sensitive on all platforms.- Parameters:
filename
- the filename to query, null returns falseextensions
- the extensions to check for, null checks for no extension- Returns:
- true if the filename is one of the extensions
-
wildcardMatch
Checks a filename to see if it matches the specified wildcard matcher, always testing case-sensitive. The wildcard matcher uses the characters '?' and '*' to represent a single or multiple wildcard characters. This is the same as often found on Dos/Unix command lines. The check is case-sensitive always.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
- Parameters:
filename
- the filename to match onwildcardMatcher
- the wildcard string to match against- Returns:
- true if the filename matches the wildcard string
- See Also:
-
IOCase.SENSITIVE
-
escape
Performs percent-encoding (as specified inIUnifiedRepository
) on givenname
, only encoding the characters given inreservedChars
. Assumes only ASCII characters in reservedChars.- Parameters:
name
- name to escapereservedChars
- chars within name to escape- Returns:
- escaped name
-
unescape
Reverts modifications of#escape(String)
such that for allString
st
,t.equals(unescape(escape(t)))
. Assumes only ASCII characters have been escaped.- Parameters:
name
- name to unescape- Returns:
- unescaped name
-