Path parses the paths, converts the parameters to the standard URI format, makes judgments about the parameters of the path, standardizes, Fu Hua, and so on. To facilitate understanding of path,
You can first see the detailed URI, link http://www.cnblogs.com/springside5/archive/2012/05/06/2486245.html
Import java.net.*;
Import java.io.*;
Import org.apache.hadoop.conf.Configuration;
/** Names a file or directory in a {@link filesystem}.
* Path strings use slash as the directory separator. A Path string is
* Absolute if it begins with a slash.
*/
public class Path implements comparable {
/** the directory separator, a slash. */
public static final String SEPARATOR = "/";
public static final Char Separator_char = '/';
public static final String Cur_dir = ".";
Static Final Boolean WINDOWS
= System.getproperty ("Os.name"). StartsWith ("Windows");
Private URI Uri; A hierarchical URI//defines the private variable URI in the path class, the path is represented in the URI format, and subsequent operations on the path are implemented primarily through the operation of the URI.
/** Resolve A child path against a parent path. */
Public Path (string parent, String child) {//constructor, we can find the constructor that eventually converts to path (Path,path).
This (new path (parent), New Path (child));
}
/** Resolve A child path against a parent path. */
Public path (path parent, String child) {
This is (parent, new Path (child));
}
/** Resolve A child path against a parent path. */
Public Path (String parent, Path child) {
This (new Path (parent), child);
}
/** Resolve A child path against a parent path. */
Public path (path parent, path child) {//Normalization of parent and child
Add a slash to parent ' s path so resolution are compatible with URI ' s
URI Parenturi = Parent.uri;
String Parentpath = Parenturi.getpath ();
if (!) ( Parentpath.equals ("/") | | Parentpath.equals (""))//reconstruct Parenturi, add a "/" at the end of the path to allow Parenturi to be compatible with the URI
try {
Parenturi = new URI (Parenturi.getscheme (), parenturi.getauthority (),
Parenturi.getpath () + "/", NULL, Parenturi.getfragment ());
catch (URISyntaxException e) {
throw new IllegalArgumentException (e);
}
URI resolved = Parenturi.resolve (Child.uri);
Initialize (Resolved.getscheme (), resolved.getauthority (),
Normalizepath (Resolved.getpath ()), resolved.getfragment ()); Re-parsing Child.uri based on Parenturi
}
private void Checkpatharg (String path) {
Disallow construction of a Path from empty string
if (path = = null) {
throw New IllegalArgumentException (
"Can not create a Path from a null string");
}
if (path.length () = = 0) {
throw New IllegalArgumentException (
"Can not create a Path from a empty string");
}
}
/** construct a path from a String. Path strings are URIs, but with
* unescaped elements and some additional normalization. */
Public Path (String pathstring) { Resolves a string type to a URI format
Checkpatharg (pathstring);
We can ' t use ' new URI (String) ' directly, since it assumes things are
Escaped, which we don ' t require of Paths.
Add a slash in front of paths with Windows drive letters
if (Haswindowsdrive (PathString, False))
PathString = "/" +pathstring;
Parse URI components
String scheme = NULL;
String authority = NULL;
int start = 0;
Parse URI scheme, if any
int colon = pathstring.indexof (': ');
int slash = Pathstring.indexof ('/');
if ((colon!=-1) &&
((slash = = 1) | | (Colon < slash)) {//has a scheme
Scheme = pathstring.substring (0, colon);
start = colon+1;
}
Parse URI Authority, if any
if (Pathstring.startswith ("//", start) &&
(Pathstring.length ()-start > 2)) {//Has authority
int nextslash = Pathstring.indexof ('/', start+2);
int authend = nextslash > 0? NextSlash:pathString.length ();
authority = pathstring.substring (start+2, authend);
start = Authend;
}
URI path is the rest of the string--query & fragment not supported
String Path = pathstring.substring (Start, pathstring.length ());
Initialize (scheme, authority, PATH, NULL);
}
/** construct a Path from components. */
Public Path (string scheme, string authority, string Path) {
Checkpatharg (path);
Initialize (scheme, authority, PATH, NULL);
}
/**
* Construct a path from a URI
*/
Public Path (URI Auri) {
URI = Auri;
}
private void Initialize (string scheme, string authority, string path,
String fragment) {
try {
This.uri = new URI (scheme, authority, Normalizepath (path), NULL, fragment)
. normalize ();
catch (URISyntaxException e) {
throw new IllegalArgumentException (e);
}
}
private string Normalizepath (string path) {//Standardized output path,
Remove double slashes & backslashes
if (Path.indexof ("//")!=-1) {
Path = Path.replace ("//", "/");
}
if (Path.indexof ("\")!=-1) {
Path = Path.replace ("\", "/");
}
Trim trailing slash from Non-root path (ignoring Windows drive)
int minlength = haswindowsdrive (path, true)? 4:1;
if (Path.length () > MinLength && path.endswith ("/")) {
Path = path.substring (0, Path.length ()-1);
}
return path;
}
Private Boolean haswindowsdrive (String path, Boolean slashed) {
if (! WINDOWS) return false;
int start = slashed? 1:0;
Return
Path.length () >= start+2 &&
(slashed Path.charat (0) = = ': true) &&
Path.charat (start+1) = ': ' &&
((Path.charat (start) >= ' A ' && path.charat (start) <= ' Z ') | |
(Path.charat (start) >= ' a ' && path.charat (start) <= ' z ');
}
/** Convert this to a URI. */
Public URI Touri () {return URI;}
/** return the filesystem so owns this Path. */
Public filesystem Getfilesystem (Configuration conf) throws IOException {
Return Filesystem.get (This.touri (), Conf);
}
/** True If the directory is absolute. */
public Boolean Isabsolute () {
int start = Haswindowsdrive (Uri.getpath (), true)? 3:0;
Return Uri.getpath (). StartsWith (SEPARATOR, start);
}
/** Returns The final component of this path.*/
Public String GetName () {
String path = Uri.getpath ();
int slash = Path.lastindexof (SEPARATOR);
Return path.substring (slash+1);
}
/** Returns The parent of a path or null if at root. */
Public Path GetParent () {
String path = Uri.getpath ();
int lastslash = Path.lastindexof ('/');
int start = haswindowsdrive (path, true)? 3:0;
if ((path.length () = start) | | Empty path
(Lastslash = = Start && path.length () = = start+1)) {//at root
return null;
}
String parent;
if (lastslash==-1) {
parent = Cur_dir;
} else {
int end = Haswindowsdrive (path, true)? 3:0;
Parent = path.substring (0, Lastslash==end?end+1:lastslash);
}
Return to New Path (Uri.getscheme (), uri.getauthority (), parent);
}
/** Adds a suffix to the final name in the path.*/
Public Path suffix (String suffix) {
Return to New Path (GetParent (), GetName () +suffix);
}
Public String toString () {
We can ' t use Uri.tostring (), which escapes everything, because we want
Illegal characters unescaped in the string, for glob processing, etc.
StringBuffer buffer = new StringBuffer ();
if (uri.getscheme ()!= null) {
Buffer.append (Uri.getscheme ());
Buffer.append (":");
}
if (uri.getauthority ()!= null) {
Buffer.append ("//");
Buffer.append (Uri.getauthority ());
}
if (Uri.getpath ()!= null) {
String path = Uri.getpath ();
if (Path.indexof ('/') ==0 &&
Haswindowsdrive (path, true) &&//has Windows drive
Uri.getscheme () = = null &&//But no scheme
Uri.getauthority () = = null)//or authority
Path = path.substring (1); Remove Slash before drive
Buffer.append (path);
}
if (uri.getfragment ()!= null) {
Buffer.append ("#");
Buffer.append (Uri.getfragment ());
}
return buffer.tostring ();
}
public boolean equals (Object o) {
if (!) ( o instanceof Path)) {
return false;
}
path that = (path) o;
Return This.uri.equals (That.uri);
}
public int hashcode () {
return Uri.hashcode ();
}
public int compareTo (Object o) {
path that = (path) o;
Return This.uri.compareTo (That.uri);
}
/** return the number of elements in this path. */
public int Depth () {
String path = Uri.getpath ();
int depth = 0;
int slash = Path.length () ==1 && path.charat (0) = = '/'? -1:0;
while (slash!=-1) {
depth++;
Slash = Path.indexof (SEPARATOR, slash+1);
}
return depth;
}
/** Returns a qualified path object. */
Public Path makequalified (filesystem FS) {
Path PATH = this;
if (!isabsolute ()) {
Path = new Path (Fs.getworkingdirectory (), this);
}
URI Pathuri = Path.touri ();
URI Fsuri = Fs.geturi ();
String scheme = Pathuri.getscheme ();
String authority = pathuri.getauthority ();
String fragment = Pathuri.getfragment ();
If scheme!= null &&
(Authority!= NULL | | | fsuri.getauthority () = null))
return path;
if (scheme = = null) {
Scheme = Fsuri.getscheme ();
}
if (authority = = null) {
authority = fsuri.getauthority ();
if (authority = = null) {
authority = "";
}
}
URI Newuri = null;
try {
Newuri = new URI (scheme, authority,
Normalizepath (Pathuri.getpath ()), null, fragment);
catch (URISyntaxException e) {
throw new IllegalArgumentException (e);
}
return new Path (Newuri);
}
/** Returns a qualified path object. */
Public Path makequalified (URI defaulturi, path Workingdir) {
Path PATH = this;
if (!isabsolute ()) {
Path = new Path (workingdir, this);
}
URI Pathuri = Path.touri ();
String scheme = Pathuri.getscheme ();
String authority = pathuri.getauthority ();
String fragment = Pathuri.getfragment ();
If scheme!= null &&
(Authority!= NULL | | | defaulturi.getauthority () = null))
return path;
if (scheme = = null) {
Scheme = Defaulturi.getscheme ();
}
if (authority = = null) {
authority = defaulturi.getauthority ();
if (authority = = null) {
authority = "";
}
}
URI Newuri = null;
try {
Newuri = new URI (scheme, authority,
Normalizepath (Pathuri.getpath ()), null, fragment);
catch (URISyntaxException e) {
throw new IllegalArgumentException (e);
}
return new Path (Newuri);
}
}