Sharkysoft home

lava.string
Class PathToolbox

java.lang.Object
  |
  +--lava.string.PathToolbox

public class PathToolbox
extends java.lang.Object

Path string manipulation.

Details: PathToolbox is a collection of functions for manipulating path strings. All paths manipulated by functions in this class are expected to be in Unix format, and all paths are returned in Unix format.

Since:
2000.02
Version:
2000.02
Author:
Sharky

Constructor Summary
PathToolbox()
           
 
Method Summary
static java.lang.String concatenatePaths(java.lang.String path1, java.lang.String path2)
          Concatenates paths.
static java.lang.String getFilename(java.lang.String path)
          Returns last path component.
static java.lang.String getParentPath(java.lang.String path)
          Returns parent.
static boolean isAbsolutePath(java.lang.String path)
          Determines whether path is absolute.
static boolean isRelativePath(java.lang.String path)
          Determines whether path is relative.
static java.lang.String resolveRelativePath(java.lang.String wd, java.lang.String rel)
          Resolves relative path from working directory.
static java.lang.String simplifyPath(java.lang.String path)
          Generates shortest equivalent path..
static java.lang.String toRelativePath(java.lang.String from, java.lang.String to)
          Computes relative path leading from one directory to another.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PathToolbox

public PathToolbox()
Method Detail

simplifyPath

public static java.lang.String simplifyPath(java.lang.String path)
Generates shortest equivalent path..

Details: simplifyPath modifies the given path name to represent the path's most natural form. For example,

/usr//bin/../local/./bin/

is transformed into

/usr/local/bin .

Parent directory path components ("..") and current directory path components (".") are eliminated wherever possible, and trailing slashes and redundant slashes are always removed (except where the path is simply "/"). Also, the parent of the root directory is assumed to be itself. If the empty string is supplied, the returned path is "."

Parameters:
path - the unnormalized path name
Returns:
the normamlized path name

getParentPath

public static java.lang.String getParentPath(java.lang.String path)
Returns parent.

Details: getParentPath returns the parent directory of the given path (path). The root directory is its own parent. The returned path is simplified according to the rules described in simplifyPath.

Parameters:
path - the path
Returns:
the path's parent

getFilename

public static java.lang.String getFilename(java.lang.String path)
Returns last path component.

Details: getFilename returns the last component of the given path (path). If the empty string is supplied, the empty string is returned.

Examples:

input output
"/usr/local/bin""bin"
"/usr/local/." "."
"../.." ".."
Parameters:
path - the path
Returns:
the last component

isAbsolutePath

public static boolean isAbsolutePath(java.lang.String path)
Determines whether path is absolute.

Details: isAbsolutePath determines whether the given path (path) is an absolute path (in other words, whether the first character is '/').

Parameters:
path - the path
Returns:
true iff the path is absolute

isRelativePath

public static boolean isRelativePath(java.lang.String path)
Determines whether path is relative.

Details: isRelativePath determines whether the given path (path) is an relative path (in other words, whether the first character is not '/').

Parameters:
path - the path
Returns:
true iff the path is relative

concatenatePaths

public static java.lang.String concatenatePaths(java.lang.String path1,
                                                java.lang.String path2)
Concatenates paths.

Details: concatenatePaths concatenates the two path strings together, inserting a slash inbetween them if necessary.

Parameters:
path1 - the first path string
path2 - the second path string
Returns:
the concatenated result

resolveRelativePath

public static java.lang.String resolveRelativePath(java.lang.String wd,
                                                   java.lang.String rel)
Resolves relative path from working directory.

Details: resolves the given relative path (rel) in the context of the given working directory (wd). Specifically, resolveRelativePath returns rel if rel is an absolute path, or the concatenation (by concatenatePaths) of wd and rel otherwise.

Parameters:
wd - the working directory
rel - the relative path
Returns:
the resolve path

toRelativePath

public static java.lang.String toRelativePath(java.lang.String from,
                                              java.lang.String to)
Computes relative path leading from one directory to another.

Details: toRelativePath finds the shortest relative path leading from one directory (from) to another (to). If to is already a relative path, then to is simplified and returned. Otherwise, the relative path from from to to is computed and returned. from is treated as a directory and must be absolute.

Parameters:
from - the starting directory
to - the target directory
Returns:
the relative path

Sharkysoft home