Package net.sf.basedb.core
Class Path
java.lang.Object
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 |
/filename | A file in the root diretory |
/directory | A directory in root diretory |
/directory/filename | A file in a directory |
/directory/subdirectory | A subdirectory |
/directory/subdirectory/filename | A file in a subdirectory |
~ | The currently logged in user's home directory |
~/filename | A file in the current user's home directory |
~/directory | A directory in the current user's home directory |
~login | The home directory of the user with the specified login |
~login/filename | A file in the home directory of the user with the specified login |
~login/directory | A directory in the home directory of the user with the specified login |
- Version:
- 2.0
- Author:
- enell, nicklas
- Last modified
- $Date: 2020-03-03 08:48:26 +0100 (Tue, 03 Mar 2020) $
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Path.Type
An enum that describes what type the path is -
Field Summary
Fields Modifier and Type Field Description 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
-
Method Summary
Modifier and Type Method Description 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 inINVALID_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.
-
Field Details
-
INVALID_CHARACTERS
These characters are not valid within a file or directory name: "~\\/:;*?<>|\""- See Also:
- Constant Field Values
-
invalid
A regexp checking for invalid characters. -
type
The type of the path. -
userlogin
The userlogin part of the path. -
directories
The directory part of the path. -
filename
The filename part of the path.
-
-
Constructor Details
-
Path
Create a newPath
object by parsing the given string.- Parameters:
path
- The string representation of a pathtype
- If the path represents aPath.Type.FILE
orPath.Type.DIRECTORY
- Throws:
InvalidPathException
- If the path is not valid
-
Path
Create a newPath
object.- Parameters:
userlogin
- The userlogin part of the pathdirectories
- The directories in the pathfilename
- The filename part of the path
-
Path
Create a newPath
object.- Parameters:
directoryPath
- The directory pathfilename
- The filename part of the path
-
-
Method Details
-
isValidName
Check if a name is valid as a partial path name, ie. it must not contain any of the characters inINVALID_CHARACTERS
.- Parameters:
name
- The string to be tested.- Returns:
- TRUE if this name is valid.
-
makeSafeFilename
Convert a string to a name that is safe to use as a filename. It will replace all characters mathing any of theINVALID_CHARACTERS
charactes with the specified string.- Parameters:
originalName
- The original namereplaceWith
- The string to replace all unsafe characters with- Since:
- 2.2
-
getParent
Get the parent path.- Returns:
- The path to the parent directory, or null if this is the root directory
-
getType
Get the type of the path.- Returns:
- The
Path.Type
of the Path
-
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
Get the name of the i:th directory on the path, starting with 0.- Parameters:
i
- Should be between 0 andgetDirectoryCount-1
- Returns:
- The name of the directory
-
getFilename
Get the filename part of the path.- Returns:
- The name of the file
-
toString
Get the string representation of the path. -
toURLString
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.
- Since:
- 2.11.1
-