Class PathUtils

java.lang.Object
org.apache.jackrabbit.oak.commons.PathUtils

public final class PathUtils extends Object
Utility methods to parse a path.

Each method validates the input, except if the system property {packageName}.SKIP_VALIDATION is set, in which case only minimal validation takes place within this function, so when the parameter is an illegal path, the the result of this method is undefined.

  • Field Details

  • Method Details

    • denotesRoot

      public static boolean denotesRoot(String path)
      Whether the path is the root path ("/").
      Parameters:
      path - the path
      Returns:
      whether this is the root
    • denotesCurrent

      public static boolean denotesCurrent(String element)
      Parameters:
      element - The path segment to check for being the current element
      Returns:
      true if the specified element equals "."; false otherwise.
    • denotesParent

      public static boolean denotesParent(String element)
      Parameters:
      element - The path segment to check for being the parent element
      Returns:
      true if the specified element equals ".."; false otherwise.
    • isAbsolute

      public static boolean isAbsolute(String path)
      Whether the path is absolute (starts with a slash) or not.
      Parameters:
      path - the path
      Returns:
      true if it starts with a slash
    • getParentPath

      @NotNull public static @NotNull String getParentPath(String path)
      Get the parent of a path. The parent of the root path ("/") is the root path.
      Parameters:
      path - the path
      Returns:
      the parent path
    • getAncestorPath

      @NotNull public static @NotNull String getAncestorPath(String path, int nth)
      Get the nth ancestor of a path. The 1st ancestor is the parent path, 2nd ancestor the grandparent path, and so on...

      If nth <= 0, the path argument is returned as is.

      Parameters:
      path - the path
      nth - indicates the ancestor level for which the path should be calculated.
      Returns:
      the ancestor path
    • getName

      @NotNull public static @NotNull String getName(String path)
      Get the last element of the (absolute or relative) path. The name of the root node ("/") and the name of the empty path ("") is the empty path.
      Parameters:
      path - the complete path
      Returns:
      the last element
    • dropIndexFromName

      @NotNull public static @NotNull String dropIndexFromName(@NotNull @NotNull String name)
      Returns the given name without the possible SNS index suffix. If the name does not contain an SNS index, then it is returned as-is.
      Parameters:
      name - name with a possible SNS index suffix
      Returns:
      name without the SNS index suffix
    • getDepth

      public static int getDepth(String path)
      Calculate the number of elements in the path. The root path has zero elements.
      Parameters:
      path - the path
      Returns:
      the number of elements
    • elements

      @NotNull public static @NotNull Iterable<String> elements(String path)
      Returns an Iterable for the path elements. The root path ("/") and the empty path ("") have zero elements.
      Parameters:
      path - the path
      Returns:
      an Iterable for the path elements
    • concat

      @NotNull public static @NotNull String concat(String parentPath, String... relativePaths)
      Concatenate path elements.
      Parameters:
      parentPath - the parent path
      relativePaths - the relative path elements to add
      Returns:
      the concatenated path
    • concat

      @NotNull public static @NotNull String concat(String parentPath, String subPath)
      Concatenate path elements.
      Parameters:
      parentPath - the parent path
      subPath - the subPath path to add
      Returns:
      the concatenated path
    • concatRelativePaths

      @Nullable public static @Nullable String concatRelativePaths(String... relativePaths)
      Relative path concatenation.
      Parameters:
      relativePaths - relative paths
      Returns:
      the concatenated path or null if the resulting path is empty.
    • isAncestor

      public static boolean isAncestor(String ancestor, String path)
      Check if a path is a (direct or indirect) ancestor of another path.
      Parameters:
      ancestor - the ancestor path
      path - the potential offspring path
      Returns:
      true if the path is an offspring of the ancestor
    • relativize

      @NotNull public static @NotNull String relativize(String parentPath, String path)
      Relativize a path wrt. a parent path such that relativize(parentPath, concat(parentPath, path)) == paths holds.
      Parameters:
      parentPath - parent pth
      path - path to relativize
      Returns:
      relativized path
    • getNextSlash

      public static int getNextSlash(String path, int index)
      Get the index of the next slash.
      Parameters:
      path - the path
      index - the starting index
      Returns:
      the index of the next slash (possibly the starting index), or -1 if not found
    • validate

      public static void validate(String path)
      Check if the path is valid, and throw an IllegalArgumentException if not. A valid path is absolute (starts with a '/') or relative (doesn't start with '/'), and contains none or more elements. A path may not end with '/', except for the root path. Elements itself must be at least one character long.
      Parameters:
      path - the path
    • isValid

      public static boolean isValid(String path)
      Check if the path is valid. A valid path is absolute (starts with a '/') or relative (doesn't start with '/'), and contains none or more elements. A path may not end with '/', except for the root path. Elements itself must be at least one character long.
      Parameters:
      path - the path
      Returns:
      true iff the path is valid.
    • unifyInExcludes

      public static void unifyInExcludes(Set<String> includePaths, Set<String> excludedPaths)
      Unify path inclusions and exclusions.
      • A path in includePaths is only retained if includePaths contains none of its ancestors and excludePaths contains neither of its ancestors nor that path itself.
      • A path in excludePaths is only retained if includePaths contains an ancestor of that path.
      When a set of paths is filtered wrt. includePaths and excludePaths by first excluding all paths that have an ancestor or are contained in excludePaths and then including all paths that have an ancestor or are contained in includePaths then the result is the same regardless whether the includePaths and excludePaths sets have been run through this method or not.
      Parameters:
      includePaths - set of paths to be included
      excludedPaths - set of paths to be excluded