Class PathUtils


  • public final class PathUtils
    extends java.lang.Object
    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 
      Modifier and Type Field Description
      static java.lang.String SLASH  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String leadingSlash​(java.lang.String path)  
      static java.lang.String normalize​(java.lang.String path)
      Cleanup duplicate slashes and replace backward slashes.
      static java.lang.String normalizeClasspathPath​(java.lang.String path)
      Normalization for classpath resource path.
      static java.lang.String normalizeRelativePath​(java.lang.String path)
      Normalization for sub section (sub folder) url path.
      static java.lang.String packagePath​(java.lang.Class cls)
      Returned location path does not contain leading slash because its not needed for direct classpath resource loading.
      static java.lang.String path​(java.lang.String... parts)
      Combine parts into correct path avoiding duplicate slashes and replacing backward slashes.
      static java.lang.String relativize​(java.lang.String path)
      Method used to normalize and remove leading slash to convert path into relative if it starts from slash.
      static java.lang.String trailingSlash​(java.lang.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 java.lang.String trimLeadingSlash​(java.lang.String path)
      Note: assumed that leading slash is not backslash (in this case use normalize(String) first).
      static java.lang.String trimSlashes​(java.lang.String path)
      Note: assumed that leading and trailing slashed are not backslashes (in this case use normalize(String) first).
      static java.lang.String trimStars​(java.lang.String path)
      Method used to cleanup wildcard paths like "/*" into "/".
      static java.lang.String trimTrailingSlash​(java.lang.String path)
      Note: assumed that trailing slash is not backslash (in this case use normalize(String) first).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • path

        public static java.lang.String path​(java.lang.String... parts)
        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

        public static java.lang.String normalize​(java.lang.String path)
        Cleanup duplicate slashes and replace backward slashes.
        Parameters:
        path - path to cleanup
        Returns:
        path with canonical slashes
      • leadingSlash

        public static java.lang.String leadingSlash​(java.lang.String path)
        Parameters:
        path - path
        Returns:
        path started with slash (original path if it already starts with slash)
      • trailingSlash

        public static java.lang.String trailingSlash​(java.lang.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).
        Parameters:
        path - path
        Returns:
        path ended with slash (original path if it already ends with slash)
      • trimStars

        public static java.lang.String trimStars​(java.lang.String path)
        Method used to cleanup wildcard paths like "/*" into "/".
        Parameters:
        path - path
        Returns:
        path without leading / trailing stars
      • trimSlashes

        public static java.lang.String trimSlashes​(java.lang.String path)
        Note: assumed that leading and trailing slashed are not backslashes (in this case use normalize(String) first).
        Parameters:
        path - path
        Returns:
        path without leading / trailing slashes
      • trimLeadingSlash

        public static java.lang.String trimLeadingSlash​(java.lang.String path)
        Note: assumed that leading slash is not backslash (in this case use normalize(String) first).
        Parameters:
        path - path
        Returns:
        path without leading slash
      • trimTrailingSlash

        public static java.lang.String trimTrailingSlash​(java.lang.String path)
        Note: assumed that trailing slash is not backslash (in this case use normalize(String) first).
        Parameters:
        path - path
        Returns:
        path without trailing slash
      • relativize

        public static java.lang.String relativize​(java.lang.String path)
        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

        public static java.lang.String packagePath​(java.lang.Class cls)
        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

        public static java.lang.String normalizeRelativePath​(java.lang.String path)
        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
      • normalizeClasspathPath

        public static java.lang.String normalizeClasspathPath​(java.lang.String path)
        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