org.apache.jackrabbit.mk.util
Class PathUtils

java.lang.Object
  extended by org.apache.jackrabbit.mk.util.PathUtils

public class PathUtils
extends Object

Utility methods to parse a JCR 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.


Constructor Summary
PathUtils()
           
 
Method Summary
static String concat(String parentPath, String... relativePaths)
          Concatenate path elements.
static String concat(String parentPath, String subPath)
          Concatenate path elements.
static boolean denotesRoot(String path)
          Whether the path is the root path ("/").
static Iterable<String> elements(String path)
          Split a path into 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 String relativize(String parentPath, String path)
          Relativize a path wrt.
static String[] split(String path)
          Split a path into elements.
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
 

Constructor Detail

PathUtils

public PathUtils()
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

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

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

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
Returns:
the ancestor path

getName

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

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

split

public static String[] split(String path)
Split a path into elements. The root path ("/") and the empty path ("") is zero elements.

Parameters:
path - the path
Returns:
the path elements

elements

public static Iterable<String> elements(String path)
Split a path into elements. The root path ("/") and the empty path ("") is zero elements.

Parameters:
path - the path
Returns:
an Iterable for the path elements

concat

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

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

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


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