Class PathUtils
java.lang.Object
ru.vyarus.dropwizard.guice.module.installer.util.PathUtils
Path and URL utils. Cover common cases when path must be cleaned (remove duplicate slashes and round backslashes),
trim leading/trailing slashes etc.
- Since:
- 06.12.2018
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic StringleadingSlash(String path) static StringCleanup duplicate slashes and replace backward slashes.static StringnormalizeAbsolutePath(String path) Normalize path by removing duplicate slashes and replacing backslashes.static StringnormalizeClasspathPath(String path) Normalization for classpath resource path.static StringnormalizeRelativePath(String path) Normalization for sub section (sub folder) url path.static StringpackagePath(Class cls) Returned location path does not contain leading slash because its not needed for direct classpath resource loading.static StringCombine parts into correct path avoiding duplicate slashes and replacing backward slashes.static Stringrelativize(String path) Method used to normalize and remove leading slash to convert path into relative if it starts from slash.static StringtrailingSlash(String path) Exception: slash is not applied to empty string because in this case it would become leading slash too (may not be desired behaviour).static StringtrimLeadingSlash(String path) Note: assumed that leading slash is not backslash (in this case usenormalize(String)first).static StringtrimSlashes(String path) Note: assumed that leading and trailing slashed are not backslashes (in this case usenormalize(String)first).static StringMethod used to cleanup wildcard paths like "/*" into "/".static StringtrimTrailingSlash(String path) Note: assumed that trailing slash is not backslash (in this case usenormalize(String)first).
-
Field Details
-
SLASH
Slash.- See Also:
-
-
Method Details
-
path
Combine parts into correct path avoiding duplicate slashes and replacing backward slashes. Null and empty parts are ignored. No leading or trailing slash appended.May contain base url as first part, e.g. "http://localhost/" and double slash there would not be replaced.
- Parameters:
parts- path parts- Returns:
- combined path from supplied parts
-
normalize
Cleanup duplicate slashes and replace backward slashes.- Parameters:
path- path to cleanup- Returns:
- path with canonical slashes
-
leadingSlash
- Parameters:
path- path- Returns:
- path started with slash (original path if it already starts with slash)
-
trailingSlash
Exception: slash is not applied to empty string because in this case it would become leading slash too (may not be desired behaviour).- Parameters:
path- path- Returns:
- path ended with slash (original path if it already ends with slash)
-
trimStars
Method used to cleanup wildcard paths like "/*" into "/".- Parameters:
path- path- Returns:
- path without leading / trailing stars
-
trimSlashes
Note: assumed that leading and trailing slashed are not backslashes (in this case usenormalize(String)first).- Parameters:
path- path- Returns:
- path without leading / trailing slashes
-
trimLeadingSlash
Note: assumed that leading slash is not backslash (in this case usenormalize(String)first).- Parameters:
path- path- Returns:
- path without leading slash
-
trimTrailingSlash
Note: assumed that trailing slash is not backslash (in this case usenormalize(String)first).- Parameters:
path- path- Returns:
- path without trailing slash
-
relativize
Method used to normalize and remove leading slash to convert path into relative if it starts from slash. This is important for html pages with base tag declared: relative paths correctly resolved relative to application root.- Parameters:
path- path to make relative- Returns:
- relative path (without leading slash)
-
packagePath
Returned location path does not contain leading slash because its not needed for direct classpath resource loading.- Parameters:
cls- class- Returns:
- class location path
-
normalizeRelativePath
Normalization for sub section (sub folder) url path. Rules:- Backslashes replaced with '/'
- Url must not starts with '/'
- Url must end with '/' (to prevent false sub-string matches when path used for matches)
The difference with
relativize(String)is that trailing slash is applied (which is wrong for urls!).IMPORTANT: '/' will become '' (here leading and trailing slash rules conflict, but leading rule wins as more important in this case).
- Parameters:
path- path- Returns:
- normalized url
-
normalizeAbsolutePath
Normalize path by removing duplicate slashes and replacing backslashes. Non-http paths will always start with "/". Trailing slash is removed. Special cases are "/" and "" which will be returned as "/".For example, "some/path" normalized to "/some/path" and "http://localhost/some/path" preserved as is "http://localhost/some/path".
Method mainly assumed to be used to format rest paths. With the slash at the beginning and no slash at the end makes them easy to concatenate.
- Parameters:
path- path to normalize- Returns:
- normalized path
-
normalizeClasspathPath
Normalization for classpath resource path. Rules:- Path use '/' as separator
- Backslashes replaced with '/'
- Path must not start with '/'
- Path ends with '/'
- Parameters:
path- classpath path (with '.' or '/')- Returns:
- normalized classpath path
-