net.sf.sfac.file
Class FilePathUtils

java.lang.Object
  extended by net.sf.sfac.file.FilePathUtils

public class FilePathUtils
extends Object

Utilities to manipulate file paths as strings.
In particular it can convert absolute to relative paths (and vice-versa). This utility class is quite platform-independent beacuse it handles indifferently slash or backslash as path separator. The only platform-specific thing is that it ignores case in path comparison (wich is mandatory on Windows system), this is detected fom the file system or can be passed as a constructor parameter.

Author:
Olivier Berlanger

Constructor Summary
FilePathUtils(File baseDir)
          Create a FilePathUtils bounded to the given local base directory.
FilePathUtils(String basePath, boolean ignoreCaseInPaths)
          Create a FilePathUtils bounded to the given base directory.
 
Method Summary
 String canonicalize(String originalPath)
          Canonicalize the given path.
static String concatPaths(String firstPath, String secondPath)
          Concatenate two paths to one path.
 String getAbsoluteFilePath(String filePath)
          Get an absolute file path for the given path.
 String getBasePath()
          Get the base path used a starting point for relative paths.
static String getDirectoryPath(String filePath)
          Get the directory part of a full file path.
static String getDrive(String filePath)
          Get the drive of a path or null if no drive is specified in the path.
static String getExtension(String fileName)
          Get the extension of the given file (ex: extension of "myIcon.gif" returns "gif").
static String getFileName(String filePath)
          Get the file name (and extension) from the gievn path.
 String getRelativeFilePath(String filePath)
          Get the given path expressed as relative to this class base directory (if possible).
 char getSparatorChar()
          Get the preferred file separator char ('/' or '\') of this FilePathUtils.
static boolean isAbsolute(String filePath)
          Check if the given file path is absolute.
static boolean startsWithSlash(String filePath)
          Check if the given file path start with a separator (usually slash or backslash).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FilePathUtils

public FilePathUtils(File baseDir)
Create a FilePathUtils bounded to the given local base directory. The FilePathUtils will use the separator char of the current system.

Parameters:
baseDir - The base directory used a starting point for relative paths.
Throws:
IllegalArgumentException - if the given base directory is not a directory of cannot be canonicalised.

FilePathUtils

public FilePathUtils(String basePath,
                     boolean ignoreCaseInPaths)
Create a FilePathUtils bounded to the given base directory. You can use this constructor to pass other parameters than the defaults of the current system, for example, if you want to calculate path for a remote system.

Parameters:
basePath - The canonicalized base path (with the drive if present). This class will use (when creating paths) the path separator given in this base reference path.
ignoreCase - true if the target file system is not case sensitive.
Throws:
IllegalArgumentException - if the given base directory cannot be canonicalised.
Method Detail

getSparatorChar

public char getSparatorChar()
Get the preferred file separator char ('/' or '\') of this FilePathUtils. Though there is one preferred separator char, both '\' and '/' are always accepted in input paths.

Returns:
the preferred file separator char ('/' or '\')

getBasePath

public String getBasePath()
Get the base path used a starting point for relative paths.
Note: this path also includes the drive (if applicable).

Returns:
the base path used a starting point for relative paths.

getAbsoluteFilePath

public String getAbsoluteFilePath(String filePath)
                           throws InvalidPathException
Get an absolute file path for the given path. If the given path is a relative one, it will be added to the base path. The returned path will be canonicalized. Returns null if the given path is null.

Parameters:
filePath - the file path to make absolute.
Returns:
an absolute file path corresponding to the given one.
Throws:
InvalidPathException - if the given path can not be combined with the base path to produce a canonicalised path.

canonicalize

public String canonicalize(String originalPath)
                    throws InvalidPathException
Canonicalize the given path.
The path can be absolute or relative, all the ".." that cannot be resolved will remain at the begin of the resulting path.

Parameters:
originalPath - a path to canonicalize.
Returns:
the given path canonicalized.
Throws:
InvalidPathException - If the path cannot be canonicalized for any reason.

getRelativeFilePath

public String getRelativeFilePath(String filePath)
                           throws InvalidPathException
Get the given path expressed as relative to this class base directory (if possible).
If the given path is already relative, it's returned without changes. Returns null if the given path is null.
Note: the result can be an absolute path, if the given path is absolute and has no common part with the base directory.

Parameters:
filePath - The path to express as relative to this class base directory.
Returns:
The given path expressed as relative to this class base directory.
Throws:
InvalidPathException

isAbsolute

public static boolean isAbsolute(String filePath)
Check if the given file path is absolute.
A file path is absolute when it starts with a drive letter on windows system, and if it starts with a separator (usually slash) on other systems.

Parameters:
filePath - the file path to check
Returns:
true iff the given file path is absolute.

startsWithSlash

public static boolean startsWithSlash(String filePath)
Check if the given file path start with a separator (usually slash or backslash).

Parameters:
filePath - the file path to check
Returns:
true iff the given file path starts with the file separator.

getDrive

public static String getDrive(String filePath)
Get the drive of a path or null if no drive is specified in the path.
The returned drive string contains the drive letter and the colon (like "C:").

Parameters:
filePath - The file path possibly starting with a drive letter.
Returns:
The drive of a path or null if no drive is specified in the path.

concatPaths

public static String concatPaths(String firstPath,
                                 String secondPath)
Concatenate two paths to one path.
The first path can be any path (relative or absolute) or can even be null. If the second path is absolute, this path is returned as is. If it is a relative path, this method concatenate the two taking care if there is a file separator to add between the two concatenated paths (and if the first path is not empty).

Parameters:
firstPath - first path to concatenate.
secondPath - second path to concatenate.
Returns:
the two paths concatenated to one path.

getExtension

public static String getExtension(String fileName)
Get the extension of the given file (ex: extension of "myIcon.gif" returns "gif").
This method simply returns the text after the last dot, or null if there is no dot.

Parameters:
fileName - a file name
Returns:
the extension of the given file.

getDirectoryPath

public static String getDirectoryPath(String filePath)
Get the directory part of a full file path.
Example: directory for "\Temp\test\MyFile.txt" is "\Temp\test".
If there is no directory, null is returned.
Example: directory for "MyFile.txt" is null.

Note: This method does not try to interpret the path, the last item of the path is assumed to be a file name (even if it is a directory). So directory for "\Temp\test" is "\Temp", and directory for "\Temp\" is "\Temp".

Parameters:
filePath - a full file path

getFileName

public static String getFileName(String filePath)
Get the file name (and extension) from the gievn path.
Example: file name for "\Temp\test\MyFile.txt" is "MyFile.txt".

Note: This method does not try to interpret the path, the last item of the path is assumed to be a file name (even if it is a directory). So file name for "\Temp\test" is "test", and file name for "\Temp\" is "".

Parameters:
filePath - a full file path


Copyright © 2012. All Rights Reserved.