Package org.jorigin.lang
Class PathUtil
- java.lang.Object
-
- org.jorigin.lang.PathUtil
-
public class PathUtil extends java.lang.ObjectThis class provide methods for processing resource pathes (files, URL, URI, ...).- Since:
- 1.0.0
- Version:
- "1.0.9" - b201903071030L
- Author:
- Julien Seinturier - COMEX S.A. - contact@jorigin.org - https://github.com/jorigin/jeometry
-
-
Field Summary
Fields Modifier and Type Field Description static intMYSQLA constant symbolizing a mysql path.static intSMBA constant symbolizing a samba pathstatic intSYSTEMA constant symbolizing a system path.static intUNKNOWNA constant symbolizing an unknown pathstatic intURL_FILEA constant symbolizing a URL path followin the file schemestatic intURL_FTPA constant symbolizing a URL path followin the http schemestatic intURL_HTTPA constant symbolizing a URL path followin the ftp schemestatic intURL_SFTPA constant symbolizing a URL path followin the sftp scheme
-
Constructor Summary
Constructors Constructor Description PathUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringgetDirectory(java.lang.String path)Get the directory of the path given in parameter.static java.lang.StringgetExtension(java.lang.String path)Get the extension of the file located by the path given in parameter.static java.lang.StringgetFileName(java.lang.String path)Return the file name pointed by thepathgiven in parameter.static intgetProtocol(java.lang.String path)Return the protocol of a path.static java.lang.StringgetStrictFileName(java.lang.String path)Return the file name pointed by the path without its extension.static booleanisAbsolutePath(java.lang.String path)Return true if the path given in parameter is absolute.static booleanisAbsoluteURL(java.lang.String url)Return true if the URL given in parameter is absolutestatic java.net.URIpathToURI(java.lang.String path)Create a well formed URI from a path.static java.net.URLpathToURL(java.lang.String path)Return a Java URL representing the path given in parameter.static java.lang.Stringrelativize(java.lang.String path, java.lang.String root)Relativize given path against the given rootstatic java.lang.Stringrelativize(java.net.URI uri, java.net.URI root)Relativize given uri from the given root.static java.lang.StringremoveRoot(java.lang.String path)Remove the root of a Path.static java.net.URIresolve(java.lang.String path, java.lang.String root)Resolve the path given in parameter against the root path given.static java.lang.StringURIToPath(java.lang.String uri)Return a system path from a non constrained path (uri, url, relative system path, ...)static java.lang.StringURIToPath(java.net.URI uri)Convert a uri into a valid system path or a distant url path (http, ftp, ...)
-
-
-
Field Detail
-
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:
- Constant Field Values
-
URL_FILE
public static final int URL_FILE
A constant symbolizing a URL path followin the file scheme- See Also:
- Constant Field Values
-
URL_HTTP
public static final int URL_HTTP
A constant symbolizing a URL path followin the ftp scheme- See Also:
- Constant Field Values
-
URL_FTP
public static final int URL_FTP
A constant symbolizing a URL path followin the http scheme- See Also:
- Constant Field Values
-
URL_SFTP
public static final int URL_SFTP
A constant symbolizing a URL path followin the sftp scheme- See Also:
- Constant Field Values
-
SMB
public static final int SMB
A constant symbolizing a samba path- See Also:
- Constant Field Values
-
MYSQL
public static final int MYSQL
A constant symbolizing a mysql path.- See Also:
- Constant Field Values
-
UNKNOWN
public static final int UNKNOWN
A constant symbolizing an unknown path- See Also:
- Constant Field Values
-
-
Method Detail
-
pathToURI
public static java.net.URI pathToURI(java.lang.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 java.lang.String URIToPath(java.net.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 java.lang.String URIToPath(java.lang.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(java.lang.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(java.lang.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(java.lang.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 java.lang.String relativize(java.net.URI uri, java.net.URI root)Relativize given uri from the given root.- Parameters:
uri- URI the uri to relativizeroot- URI the root against the relativization is done- Returns:
- String the relative path computed
-
resolve
public static java.net.URI resolve(java.lang.String path, java.lang.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 callPathUtil.URIToPath(resolve(uri, root))return the system path result of the resolution.- Parameters:
path- String The path to resolveroot- String The root path used in the resolution- Returns:
- URI the well formed uri, result of the resolution.
-
relativize
public static java.lang.String relativize(java.lang.String path, java.lang.String root)Relativize given path against the given root- Parameters:
path- String the path to relativizeroot- String the root against the relativization is done- Returns:
- String the relative path computed
-
getFileName
public static java.lang.String getFileName(java.lang.String path)
Return the file name pointed by thepathgiven 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 java.lang.String getExtension(java.lang.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 java.lang.String getDirectory(java.lang.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
nullif the path is invalid or have no directory.
-
getStrictFileName
public static java.lang.String getStrictFileName(java.lang.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 methodgetFileName(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:
getFileName(String)
-
pathToURL
public static java.net.URL pathToURL(java.lang.String path)
Return a Java URL representing the path given in parameter. Thepathgiven 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 java.lang.String removeRoot(java.lang.String path)
Remove the root of a Path. For example, the methodremoveRoot("C:\path")return the string"path". Under linux, the methodremoveRoot("/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.
-
-