Class PathUtils
- java.lang.Object
-
- org.apache.jackrabbit.oak.commons.PathUtils
-
public final class PathUtils extends java.lang.ObjectUtility 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.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static @NotNull java.lang.Stringconcat(java.lang.String parentPath, java.lang.String subPath)Concatenate path elements.static @NotNull java.lang.Stringconcat(java.lang.String parentPath, java.lang.String... relativePaths)Concatenate path elements.static @Nullable java.lang.StringconcatRelativePaths(java.lang.String... relativePaths)Relative path concatenation.static booleandenotesCurrent(java.lang.String element)static booleandenotesParent(java.lang.String element)static booleandenotesRoot(java.lang.String path)Whether the path is the root path ("/").static @NotNull java.lang.StringdropIndexFromName(@NotNull java.lang.String name)Returns the given name without the possible SNS index suffix.static @NotNull java.lang.Iterable<java.lang.String>elements(java.lang.String path)Returns anIterablefor the path elements.static @NotNull java.lang.StringgetAncestorPath(java.lang.String path, int nth)Get the nth ancestor of a path.static intgetDepth(java.lang.String path)Calculate the number of elements in the path.static @NotNull java.lang.StringgetName(java.lang.String path)Get the last element of the (absolute or relative) path.static intgetNextSlash(java.lang.String path, int index)Get the index of the next slash.static @NotNull java.lang.StringgetParentPath(java.lang.String path)Get the parent of a path.static booleanisAbsolute(java.lang.String path)Whether the path is absolute (starts with a slash) or not.static booleanisAncestor(java.lang.String ancestor, java.lang.String path)Check if a path is a (direct or indirect) ancestor of another path.static booleanisValid(java.lang.String path)Check if the path is valid.static @NotNull java.lang.Stringrelativize(java.lang.String parentPath, java.lang.String path)Relativize a path wrt.static voidunifyInExcludes(java.util.Set<java.lang.String> includePaths, java.util.Set<java.lang.String> excludedPaths)Unify path inclusions and exclusions.static voidvalidate(java.lang.String path)Check if the path is valid, and throw an IllegalArgumentException if not.
-
-
-
Field Detail
-
ROOT_PATH
public static final java.lang.String ROOT_PATH
- See Also:
- Constant Field Values
-
ROOT_NAME
public static final java.lang.String ROOT_NAME
- See Also:
- Constant Field Values
-
-
Method Detail
-
denotesRoot
public static boolean denotesRoot(java.lang.String path)
Whether the path is the root path ("/").- Parameters:
path- the path- Returns:
- whether this is the root
-
denotesCurrent
public static boolean denotesCurrent(java.lang.String element)
- Parameters:
element- The path segment to check for being the current element- Returns:
trueif the specified element equals ".";falseotherwise.
-
denotesParent
public static boolean denotesParent(java.lang.String element)
- Parameters:
element- The path segment to check for being the parent element- Returns:
trueif the specified element equals "..";falseotherwise.
-
isAbsolute
public static boolean isAbsolute(java.lang.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 java.lang.String getParentPath(java.lang.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 java.lang.String getAncestorPath(java.lang.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 pathnth- indicates the ancestor level for which the path should be calculated.- Returns:
- the ancestor path
-
getName
@NotNull public static @NotNull java.lang.String getName(java.lang.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 java.lang.String dropIndexFromName(@NotNull @NotNull java.lang.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(java.lang.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 java.lang.Iterable<java.lang.String> elements(java.lang.String path)
Returns anIterablefor 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 java.lang.String concat(java.lang.String parentPath, java.lang.String... relativePaths)Concatenate path elements.- Parameters:
parentPath- the parent pathrelativePaths- the relative path elements to add- Returns:
- the concatenated path
-
concat
@NotNull public static @NotNull java.lang.String concat(java.lang.String parentPath, java.lang.String subPath)Concatenate path elements.- Parameters:
parentPath- the parent pathsubPath- the subPath path to add- Returns:
- the concatenated path
-
concatRelativePaths
@Nullable public static @Nullable java.lang.String concatRelativePaths(java.lang.String... relativePaths)
Relative path concatenation.- Parameters:
relativePaths- relative paths- Returns:
- the concatenated path or
nullif the resulting path is empty.
-
isAncestor
public static boolean isAncestor(java.lang.String ancestor, java.lang.String path)Check if a path is a (direct or indirect) ancestor of another path.- Parameters:
ancestor- the ancestor pathpath- the potential offspring path- Returns:
- true if the path is an offspring of the ancestor
-
relativize
@NotNull public static @NotNull java.lang.String relativize(java.lang.String parentPath, java.lang.String path)Relativize a path wrt. a parent path such thatrelativize(parentPath, concat(parentPath, path)) == pathsholds.- Parameters:
parentPath- parent pthpath- path to relativize- Returns:
- relativized path
-
getNextSlash
public static int getNextSlash(java.lang.String path, int index)Get the index of the next slash.- Parameters:
path- the pathindex- the starting index- Returns:
- the index of the next slash (possibly the starting index), or -1 if not found
-
validate
public static void validate(java.lang.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(java.lang.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:
trueiff the path is valid.
-
unifyInExcludes
public static void unifyInExcludes(java.util.Set<java.lang.String> includePaths, java.util.Set<java.lang.String> excludedPaths)Unify path inclusions and exclusions.- A path in
includePathsis only retained ifincludePathscontains none of its ancestors andexcludePathscontains neither of its ancestors nor that path itself. - A path in
excludePathsis only retained ifincludePathscontains an ancestor of that path.
includePathsandexcludePathsby first excluding all paths that have an ancestor or are contained inexcludePathsand then including all paths that have an ancestor or are contained inincludePathsthen the result is the same regardless whether theincludePathsandexcludePathssets have been run through this method or not.- Parameters:
includePaths- set of paths to be includedexcludedPaths- set of paths to be excluded
- A path in
-
-