org.cdlib.xtf.util
Class Path

Object
  extended by Path

public class Path
extends Object

The Path class provides a number of utilities that makes working with file system paths easier. This class is effectively a "package" in that all its members are static, and do not rely on instance variables.


Field Summary
static Tester tester
           
 
Constructor Summary
Path()
           
 
Method Summary
static String calcPrefix(String longPath, String shortPath)
          Find the part of the long path that, when all symbolic links are fully resolved, maps to the short path (when the short path also is fully resolved.)
static void copyFile(File src, File dst)
          Copies a source file to the specified destination.
static boolean createPath(String path)
          Create the specified file system path.
static void deleteDir(File dir)
          Utility function to delete a specified directory and all its files and subdirectories.
static boolean deletePath(String path)
          Remove the specified path from the file system.
static String normalize(String pathOrFileName)
          Normalize a path or file name.
static String normalizeFileName(String path)
          Normalize a file name.
static String normalizePath(String path)
          Normalize the specified file system path.
static String resolveRelOrAbs(File parentDir, String childPath)
          Utility function to resolve a child path against a parent path.
static String resolveRelOrAbs(String parentDir, String childPath)
          Utility function to resolve a child path against a parent path.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

tester

public static final Tester tester
Constructor Detail

Path

public Path()
Method Detail

normalizePath

public static final String normalizePath(String path)
Normalize the specified file system path.

This function performs a number of "cleanup" operations to create a standardized (or normalized) path. These operations include:

- Stripping any leading or trailing spaces from the passed in path.

- Converts DOS/Windows paths (with backslash characters) into UNIX standard format (with forward slash characters.)

- Removes any double slash characters that may have been created when two partial path strings were concatenated.

- Converts drive letters to uppercase (in Windows) - Removes any occurrences of "./" - Removes any occurrences of "xxx/../" - Finally, if the resulting path is not an empty string, this function guarantees that the path ends in a slash character, in anticipation of a filename being tacked on to the end.

Parameters:
path - The path to normalize.

Returns:
A normalized version of the original path string passed.

normalizeFileName

public static final String normalizeFileName(String path)
Normalize a file name.

This function performs a number of "cleanup" operations to create a standardized (or normalized) file name.

Parameters:
path - The file name (optionally preceeded by a path) to normalize.

Returns:
A normalized version of the original file name string passed.
Notes:
This function does its work by calling the normalizePath() function to normalize the filename and path (if any), and then simply removes the trailing slash.

normalize

public static final String normalize(String pathOrFileName)
Normalize a path or file name.

This function performs a number of "cleanup" operations to create a standardized (or normalized) file name.

Parameters:
pathOrFileName - The path or file name (optionally preceeded by a path) to normalize.

Returns:
A normalized version of the original file name string passed.
Notes:
This function does its work by calling the normalizePath() function to normalize the filename and path (if any). If the original path ended with a slash, the new one will also. If not, the new one will not.

createPath

public static final boolean createPath(String path)
Create the specified file system path.

This function creates the specified file system path if it does not already exist.

Parameters:
path - The file system path to create.

Returns:
true - The file system path was successfully created.
false - An file system path was not created due to errors.

Notes:
This method calls the function normalizePath() to help ensure the successful creation of the specified path.

Any directories specified in the path that do not already exist are created. Thus, this function can create paths where some or none of the parent directories exist.


deletePath

public static final boolean deletePath(String path)
Remove the specified path from the file system.

This function removes directories from the specified path, starting with the lowest directory and moving up until either the entire path has been removed or a non-empty directory is encountered.

Parameters:
path - The file system path to remove.

Returns:
true - Part or all of the specified path was removed.
false - None of the specified path could be removed (either because none of the directories in the path were empty, or because of other errors.)


calcPrefix

public static final String calcPrefix(String longPath,
                                      String shortPath)
                               throws IOException
Find the part of the long path that, when all symbolic links are fully resolved, maps to the short path (when the short path also is fully resolved.)

Throws:
IOException

deleteDir

public static void deleteDir(File dir)
                      throws IOException
Utility function to delete a specified directory and all its files and subdirectories.

Throws:
IOException - if we fail to delete the entire directory and all sub-files and subdirectories.

resolveRelOrAbs

public static String resolveRelOrAbs(File parentDir,
                                     String childPath)
Utility function to resolve a child path against a parent path. Unlike the File(File,String) constructor, this first checks if the child path is absolute. If it is, the parent file is completely ignored.

Parameters:
parentDir - - Directory against which to resolve the child, if the child is a relative path.
childPath - - An absolute path, or else a relative path to resolve against parentFile.
Returns:
- The resulting fully resolved path.

resolveRelOrAbs

public static String resolveRelOrAbs(String parentDir,
                                     String childPath)
Utility function to resolve a child path against a parent path. Unlike the File(File,String) constructor, this first checks if the child path is absolute. If it is, the parent file is completely ignored.

Parameters:
parentDir - - Directory against which to resolve the child, if the child is a relative path.
childPath - - An absolute path, or else a relative path to resolve against parentFile.
Returns:
- The resulting fully resolved path.

copyFile

public static void copyFile(File src,
                            File dst)
                     throws IOException
Copies a source file to the specified destination. Creates the destination file if it doesn't exist; overwrites it otherwise.

Throws:
IOException