2.17.2: 2011-06-17

net.sf.basedb.core
Class Path

java.lang.Object
  extended by net.sf.basedb.core.Path

public class Path
extends Object

This class is used represent the path to a Directory or File item. Some examples:

/The root diretory
/filenameA file in the root diretory
/directoryA directory in root diretory
/directory/filenameA file in a directory
/directory/subdirectoryA subdirectory
/directory/subdirectory/filenameA file in a subdirectory
~The currently logged in user's home directory
~/filenameA file in the current user's home directory
~/directoryA directory in the current user's home directory
~loginThe home directory of the user with the specified login
~login/filenameA file in the home directory of the user with the specified login
~login/directoryA directory in the home directory of the user with the specified login

Version:
2.0
Author:
enell, nicklas
Last modified
$Date: 2009-05-27 12:07:18 +0200 (Wed, 27 May 2009) $

Nested Class Summary
static class Path.Type
          An enum that describes what type the path is
 
Field Summary
private  String[] directories
          The directory part of the path.
private  String filename
          The filename part of the path.
private static Pattern invalid
          A regexp checking for invalid characters.
static String INVALID_CHARACTERS
          These characters are not valid within a file or directory name: "~\\/:;*?<>|\""
private  Path.Type type
          The type of the path.
private  String userlogin
          The userlogin part of the path.
 
Constructor Summary
Path(Path directoryPath, String filename)
          Create a new Path object.
Path(String path, Path.Type type)
          Create a new Path object by parsing the given string.
Path(String userlogin, String[] directories, String filename)
          Create a new Path object.
 
Method Summary
 String getDirectory(int i)
          Get the name of the i:th directory on the path, starting with 0.
 int getDirectoryCount()
          Get the number of directories on the path.
 String getFilename()
          Get the filename part of the path.
 Path getParent()
          Get the parent path.
 Path.Type getType()
          Get the type of the path.
 String getUserLogin()
          Get the userlogin part of the path.
static boolean isValidName(String name)
          Check if a name is valid as a partial path name, ie. it must not contain any of the characters in INVALID_CHARACTERS.
static String makeSafeFilename(String originalName, String replaceWith)
          Convert a string to a name that is safe to use as a filename.
 String toString()
          Get the string representation of the path.
 String toURLString(String enc)
          Get this Path's URL-string encoded with a given character encoding.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

INVALID_CHARACTERS

public static final String INVALID_CHARACTERS
These characters are not valid within a file or directory name: "~\\/:;*?<>|\""

See Also:
Constant Field Values

invalid

private static final Pattern invalid
A regexp checking for invalid characters.


type

private Path.Type type
The type of the path.


userlogin

private String userlogin
The userlogin part of the path.


directories

private String[] directories
The directory part of the path.


filename

private String filename
The filename part of the path.

Constructor Detail

Path

public Path(String path,
            Path.Type type)
     throws InvalidPathException
Create a new Path object by parsing the given string.

Parameters:
path - The string representation of a path
type - If the path represents a Path.Type.FILE or Path.Type.DIRECTORY
Throws:
InvalidPathException - If the path is not valid

Path

Path(String userlogin,
     String[] directories,
     String filename)
Create a new Path object.

Parameters:
userlogin - The userlogin part of the path
directories - The directories in the path
filename - The filename part of the path

Path

Path(Path directoryPath,
     String filename)
Create a new Path object.

Parameters:
directoryPath - The directory path
filename - The filename part of the path
Method Detail

isValidName

public static boolean isValidName(String name)
Check if a name is valid as a partial path name, ie. it must not contain any of the characters in INVALID_CHARACTERS.

Parameters:
name - The string to be tested.
Returns:
TRUE if this name is valid.

makeSafeFilename

public static String makeSafeFilename(String originalName,
                                      String replaceWith)
Convert a string to a name that is safe to use as a filename. It will replace all characters mathing any of the INVALID_CHARACTERS charactes with the specified string.

Parameters:
originalName - The original name
replaceWith - The string to replace all unsafe characters with
Since:
2.2

getParent

public Path getParent()
Get the parent path.

Returns:
The path to the parent directory, or null if this is the root directory

getType

public Path.Type getType()
Get the type of the path.

Returns:
The Path.Type of the Path

getUserLogin

public String getUserLogin()
Get the userlogin part of the path. An empty string represents the currently logged in user. Null is returned if the path doesn't contain a user login.

Returns:
The login of the user

getDirectoryCount

public int getDirectoryCount()
Get the number of directories on the path.

Returns:
The number of directories

getDirectory

public String getDirectory(int i)
Get the name of the i:th directory on the path, starting with 0.

Parameters:
i - Should be between 0 and getDirectoryCount-1
Returns:
The name of the directory

getFilename

public String getFilename()
Get the filename part of the path.

Returns:
The name of the file

toString

public String toString()
Get the string representation of the path.

Overrides:
toString in class Object
Returns:
The string representation of this path.

toURLString

public String toURLString(String enc)
                   throws UnsupportedEncodingException
Get this Path's URL-string encoded with a given character encoding.

Parameters:
enc - The encoding to use. UTF-8 is used is this is null.
Returns:
The encoded URL-string.
Throws:
UnsupportedEncodingException - If the give encoding is not supported
Since:
2.11.1

2.17.2: 2011-06-17