com.marringtons.file
Class Path

java.lang.Object
  extended bycom.marringtons.file.Path

public class Path
extends Object

Static helper class for working with file paths - holding home, temporary and user paths with methods to add, retrieve, make absolute, make relative and split paths.

Author:
Paul Marrington

Field Summary
static String homeDir
          User home directory.
static String tempDir
          Directory for creating temporary files.
static String userDir
          Current directory when application started.
 
Method Summary
static boolean add(String path)
          add paths to the datapath used to get files.
static void addPaths(String pathString)
          add paths to the datapath used to get files.
static String get(String name)
          Retrieve the path from a path/name string.
static String[] getAll()
          retrieve data paths in ascending order of importance.
static String[] getBases(String path)
          Return a list of base paths for a directory or zip/jar on the datapath.
static boolean isAbsolute(String path)
          See if the path os absolute or relative.
static String localiseFileSeparator(String namePath)
          Change / to system dependant file separator.
static String makeAbsolute(String path)
          Given an unknown path make it absolute if it is not alread so.
static String makeRelative(String fullName)
          Return a path relative to the base directory being used.
static String normaliseFileSeparator(String namePath)
          Change system dependant file separators to /.
static String removeRelative(String namePath)
          Use after concatenating root path to a relative path, such that "/tmp/mydir/tmpdir" + "..
static String removeRelative(String root, String relative)
          Use after concatenating root path to a relative path, such that "/tmp/mydir/tmpdir" + "..
static String[] split(String fullName)
          Split a full file name (including path) into a string for the data path used, a String for the relative path and a third for the file name component only.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

homeDir

public static String homeDir
User home directory.


userDir

public static String userDir
Current directory when application started.


tempDir

public static String tempDir
Directory for creating temporary files.

Method Detail

get

public static String get(String name)
Retrieve the path from a path/name string.

Parameters:
name - full path to a file.
Returns:
path without file name

isAbsolute

public static boolean isAbsolute(String path)
See if the path os absolute or relative.

Parameters:
path - to check for absolute or relative type
Returns:
true of path is file system absolute

makeAbsolute

public static String makeAbsolute(String path)
Given an unknown path make it absolute if it is not alread so. Uses ~ as home, otherwise relative paths are looked for on all the paths. If not found the file will be added to the user directory.

Parameters:
path - relative path to be made into an absolute path.
Returns:
absolute name given the relative path name.

removeRelative

public static String removeRelative(String root,
                                    String relative)
                             throws InvalidNameException
Use after concatenating root path to a relative path, such that "/tmp/mydir/tmpdir" + "../test" == "/tmp/test"

Parameters:
root - absolute base path
relative - path relative to root
Returns:
absolute path without ../ or //
Throws:
InvalidNameException - if ../ would go lower then lowest in provided path

removeRelative

public static String removeRelative(String namePath)
                             throws InvalidNameException
Use after concatenating root path to a relative path, such that "/tmp/mydir/tmpdir" + "../test" == "/tmp/test".

Parameters:
namePath - path to a file (name include .. and . elements
Returns:
pure path (.. and . elements removed)
Throws:
InvalidNameException - if ../ would go lower then lowest in provided path

normaliseFileSeparator

public static String normaliseFileSeparator(String namePath)
Change system dependant file separators to /.

Parameters:
namePath - path to be converted to a standard directory separator.
Returns:
copy where all directories are separated by /.

localiseFileSeparator

public static String localiseFileSeparator(String namePath)
Change / to system dependant file separator.

Parameters:
namePath - path to be converted to a standard OS Dependant separator.
Returns:
copy where all directories are separated by OS dependant character (/ or \\).

addPaths

public static void addPaths(String pathString)
add paths to the datapath used to get files. Paths are searched from the most recent addition back. Like CLASSPATH they may include JAR and ZIP files.

Parameters:
pathString - list of directory paths separated by : or ; (system dependant)

add

public static boolean add(String path)
add paths to the datapath used to get files. Paths are searched from the most recent addition back. Like CLASSPATH they may include JAR and ZIP files.

Parameters:
path - directory path, jar or zip file to add.
Returns:
true if OK, false if path is empty or does not exist.

getAll

public static String[] getAll()
retrieve data paths in ascending order of importance. It is marked synchronised so dataPaths cannot be set by multiple threads. When a file is opened for read, it's existence is searched for in: Data paths are lists of directories and zip files. For each entry in the list, search using the base paths as well as the raw file name.

Returns:
Array of databaths in ascending order of importance.

getBases

public static String[] getBases(String path)
Return a list of base paths for a directory or zip/jar on the datapath. These were read from META-INF/PATH-BASE.TXT, one base path per line. They are prepended to a file name for searching for that file in the matching zip or directory. It is also used for loading classes the same way. If, for example a basePath was com.marringtons, then looking for util/myfile.dat will also find com/marringtons/util/myfile.dat.

Parameters:
path - that represents a data path in the directory or zip file.
Returns:
path bases.

split

public static String[] split(String fullName)
Split a full file name (including path) into a string for the data path used, a String for the relative path and a third for the file name component only.

Parameters:
fullName - being file name including path.
Returns:
String array containing data path, relative path and stripped file name.

makeRelative

public static String makeRelative(String fullName)
Return a path relative to the base directory being used.

Parameters:
fullName - to parse
Returns:
File path relative to user or home directories.


Copyright © 2005 Paul Marrington http://library.marringtons.com