org.apache.jackrabbit.oak.commons
Class PathUtils

java.lang.Object
  extended by 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.


Method Summary
static String concat(String parentPath, String... relativePaths)
          Concatenate path elements.
static String concat(String parentPath, String subPath)
          Concatenate path elements.
static boolean denotesCurrent(String element)
           
static boolean denotesParent(String element)
           
static boolean denotesRoot(String path)
          Whether the path is the root path ("/").
static String dropIndexFromName(String name)
          Returns the given name without the possible SNS index suffix.
static Iterable<String> elements(String path)
          Returns an Iterable for the path elements.
static String getAncestorPath(String path, int nth)
          Get the nth ancestor of a path.
static int getDepth(String path)
          Calculate the number of elements in the path.
static String getName(String path)
          Get the last element of the (absolute or relative) path.
static int getNextSlash(String path, int index)
          Get the index of the next slash.
static String getParentPath(String path)
          Get the parent of a path.
static boolean isAbsolute(String path)
          Whether the path is absolute (starts with a slash) or not.
static boolean isAncestor(String ancestor, String path)
          Check if a path is a (direct or indirect) ancestor of another path.
static boolean isValid(String path)
          Check if the path is valid.
static String relativize(String parentPath, String path)
          Relativize a path wrt.
static void validate(String path)
          Check if the path is valid, and throw an IllegalArgumentException if not.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

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

@Nonnull
public static 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

@Nonnull
public static 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

@Nonnull
public static 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

@Nonnull
public static String dropIndexFromName(@Nonnull
                                               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

@Nonnull
public static 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

@Nonnull
public static 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

@Nonnull
public static String concat(String parentPath,
                                    String subPath)
Concatenate path elements.

Parameters:
parentPath - the parent path
subPath - the subPath path to add
Returns:
the concatenated path

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

@Nonnull
public static 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.


Copyright © 2012-2014 The Apache Software Foundation. All Rights Reserved.