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:
Path 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: 2020-03-03 08:48:26 +0100 (Tue, 03 Mar 2020) $
  • Field Details

    • INVALID_CHARACTERS

      public static final String INVALID_CHARACTERS
      These characters are not valid within a file or directory name: "~\\/:;*?<>|\""
      See Also:
    • 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 Details

    • 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 Details

    • 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)
      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