Class PathUtil

java.lang.Object
org.jorigin.lang.PathUtil

public class PathUtil extends Object
This class provide methods for processing resource pathes (files, URL, URI, ...).
Since:
1.0.0
Version:
"1.0.14" - b202111241200L
Author:
Julien SEINTURIER - Université de Toulon / CNRS LIS umr 7020 - github.com/jorigin/jcommon (contact@jorigin.org)
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    A constant symbolizing a mysql path.
    static final int
    A constant symbolizing a samba path
    static final int
    A constant symbolizing a system path.
    static final int
    A constant symbolizing an unknown path
    static final int
    A constant symbolizing a URL path followin the file scheme
    static final int
    A constant symbolizing a URL path followin the http scheme
    static final int
    A constant symbolizing a URL path followin the ftp scheme
    static final int
    A constant symbolizing a URL path followin the sftp scheme
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Get the directory of the path given in parameter.
    static String
    Get the extension of the file located by the path given in parameter.
    static String
    Return the file name pointed by the path given in parameter.
    static int
    Return the protocol of a path.
    static String
    Return the file name pointed by the path without its extension.
    static boolean
    Return true if the path given in parameter is absolute.
    static boolean
    Return true if the URL given in parameter is absolute
    static URI
    Create a well formed URI from a path.
    static URL
    Return a Java URL representing the path given in parameter.
    static String
    relativize(String path, String root)
    Relativize given path against the given root
    static String
    relativize(URI uri, URI root)
    Relativize given uri from the given root.
    static String
    Remove the root of a Path.
    static URI
    resolve(String path, String root)
    Resolve the path given in parameter against the root path given.
    static String
    Return a system path from a non constrained path (uri, url, relative system path, ...)
    static String
    Convert a uri into a valid system path or a distant url path (http, ftp, ...)

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SYSTEM

      public static final int SYSTEM
      A constant symbolizing a system path. A system path is a absolute or relative exploitation system expressed path
      See Also:
    • URL_FILE

      public static final int URL_FILE
      A constant symbolizing a URL path followin the file scheme
      See Also:
    • URL_HTTP

      public static final int URL_HTTP
      A constant symbolizing a URL path followin the ftp scheme
      See Also:
    • URL_FTP

      public static final int URL_FTP
      A constant symbolizing a URL path followin the http scheme
      See Also:
    • URL_SFTP

      public static final int URL_SFTP
      A constant symbolizing a URL path followin the sftp scheme
      See Also:
    • SMB

      public static final int SMB
      A constant symbolizing a samba path
      See Also:
    • MYSQL

      public static final int MYSQL
      A constant symbolizing a mysql path.
      See Also:
    • UNKNOWN

      public static final int UNKNOWN
      A constant symbolizing an unknown path
      See Also:
  • Constructor Details

    • PathUtil

      public PathUtil()
  • Method Details

    • pathToURI

      public static URI pathToURI(String path)
      Create a well formed URI from a path. The path can be an absolute or relative system path, an URL (ftp, http, ...). The head of the uri is: file:// for a local uri, http:// and ftp:// for distants URI.
      Parameters:
      path - String the path to convert into URI.
      Returns:
      URI the uri created from the path
    • URIToPath

      public static String URIToPath(URI uri)
      Convert a uri into a valid system path or a distant url path (http, ftp, ...)
      Parameters:
      uri - URI uri the uri to convert
      Returns:
      String path corresponding to the uri
    • URIToPath

      public static String URIToPath(String uri)
      Return a system path from a non constrained path (uri, url, relative system path, ...)
      Parameters:
      uri - String the path to trasform in system path
      Returns:
      String the system path (absolute or relative)
    • getProtocol

      public static int getProtocol(String path)
      Return the protocol of a path. A protocol can be:
      PathUtil.SYSTEM if the path is a system dependent path.
      PathUtil.URL_FILE if the path is a system url (file:/...).
      PathUtil.URL_HTTP if the path is a web path (http://...).
      PathUtil.URL_FTP if the path is a web file path (ftp://...).
      Parameters:
      path - String the path to check.
      Returns:
      int one of the integer value described above.
    • isAbsoluteURL

      public static boolean isAbsoluteURL(String url)
      Return true if the URL given in parameter is absolute
      Parameters:
      url - String the url string to test
      Returns:
      boolean true if the url is absolute, false otherwise
    • isAbsolutePath

      public static boolean isAbsolutePath(String path)
      Return true if the path given in parameter is absolute. A path is absolute if:
      On linux system
      The path start with /
      On windows system
      The path start with [A-Z]:
      Parameters:
      path - String the path to test
      Returns:
      boolean true if the path is absolute, false otherwise.
    • relativize

      public static String relativize(URI uri, URI root)
      Relativize given uri from the given root.
      Parameters:
      uri - URI the uri to relativize
      root - URI the root against the relativization is done
      Returns:
      String the relative path computed
    • resolve

      public static URI resolve(String path, String root)
      Resolve the path given in parameter against the root path given. The resolution produce a well formed uri. If a path is needed, the call PathUtil.URIToPath(resolve(uri, root)) return the system path result of the resolution.
      Parameters:
      path - String The path to resolve
      root - String The root path used in the resolution
      Returns:
      URI the well formed uri, result of the resolution.
    • relativize

      public static String relativize(String path, String root)
      Relativize given path against the given root
      Parameters:
      path - String the path to relativize
      root - String the root against the relativization is done
      Returns:
      String the relative path computed
    • getFileName

      public static String getFileName(String path)
      Return the file name pointed by the path given in parameter. The file name is part of the path folowing the last separator.
      Parameters:
      path - the path from which the file name have to be extracted.
      Returns:
      the file name.
    • getExtension

      public static String getExtension(String path)
      Get the extension of the file located by the path given in parameter. The extension of the file is the set of digits behind the last "." in the path.
      Parameters:
      path - the path of the file
      Returns:
      the extension of the file.
    • getDirectory

      public static String getDirectory(String path)
      Get the directory of the path given in parameter. If the path point a file or a resource, this method return the directory of the file or resource.
      Parameters:
      path - the path which the directory has to be extracted.
      Returns:
      the directory of the path, or null if the path is invalid or have no directory.
    • getStrictFileName

      public static String getStrictFileName(String path)
      Return the file name pointed by the path without its extension. A file name is the part of tje path folowing the last separator. This method compute the file name of the path using the method getFileName(String) and remove the extension of the file name if it exists.
      Parameters:
      path - the path from which the file name have to be extracted without extension.
      Returns:
      the strict file name
      See Also:
    • pathToURL

      public static URL pathToURL(String path)
      Return a Java URL representing the path given in parameter. The path given is normalized to match URL prerequisite (simple slash after scheme, ...). If the path is not normalized, the URL may involve errors in resource processing.
      Parameters:
      path - the path used to build a valid URL
      Returns:
      a valid URL
    • removeRoot

      public static String removeRoot(String path)
      Remove the root of a Path. For example, the method removeRoot("C:\path") return the string "path". Under linux, the method removeRoot("/path") return the string "path". If the parameter got a scheme (file://, http://, ...) it is removed.
      Parameters:
      path - the path with a root to remove.
      Returns:
      the path without a root.