java.lang.Object
ru.vyarus.dropwizard.guice.module.installer.util.PathUtils

public final class PathUtils extends 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 Details

  • Method Details

    • path

      public static String path(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 String normalize(String path)
      Cleanup duplicate slashes and replace backward slashes.
      Parameters:
      path - path to cleanup
      Returns:
      path with canonical slashes
    • leadingSlash

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

      public static String trailingSlash(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 String trimStars(String path)
      Method used to cleanup wildcard paths like "/*" into "/".
      Parameters:
      path - path
      Returns:
      path without leading / trailing stars
    • trimSlashes

      public static String trimSlashes(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 String trimLeadingSlash(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 String trimTrailingSlash(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 String relativize(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 String packagePath(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 String normalizeRelativePath(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
    • normalizeAbsolutePath

      public static String normalizeAbsolutePath(String path)
      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

      public static String normalizeClasspathPath(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