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 aDirectory
orFile
item. Some examples:Path 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: 2015-04-21 09:59:42 +0200 (ti, 21 apr 2015) $
-
-
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
Constructors Constructor Description Path(String userlogin, String[] directories, String filename)
Create a newPath
object.Path(String path, Path.Type type)
Create a newPath
object by parsing the given string.Path(Path directoryPath, String filename)
Create a newPath
object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods 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 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 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
Path(String userlogin, String[] directories, String filename)
Create a newPath
object.- Parameters:
userlogin
- The userlogin part of the pathdirectories
- The directories in the pathfilename
- 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 inINVALID_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 theINVALID_CHARACTERS
charactes with the specified string.- Parameters:
originalName
- The original namereplaceWith
- 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
-
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 andgetDirectoryCount-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.
-
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
-
-