Here you can find the source of canonicalPath(String orig)
"."
or ".."
.
Parameter | Description |
---|---|
orig | non-null; the original path |
null
if the original is bogus in some way
static public String canonicalPath(String orig)
//package com.java2s; // the rights to use, copy, modify, merge, publish, distribute, sublicense, public class Main { /**//from w w w. j a v a 2s .c o m * Canonicalize a request path, making sure it starts with a slash, and * getting rid of double slashes and components with the values * <code>"."</code> or <code>".."</code>. This will return * <code>null</code> if the given path is somehow perniciously * erroneous (such as referring to <code>".."</code> at the beginning * of the path). * * @param orig non-null; the original path * @return null-ok; the canonicalized form, or <code>null</code> if * the original is bogus in some way */ static public String canonicalPath(String orig) { if (orig.length() == 0) { return "/"; } if (orig.charAt(0) != '/') { orig = '/' + orig; } for (;;) { int foundAt = orig.indexOf("//"); if (foundAt == -1) { break; } orig = orig.substring(0, foundAt) + orig.substring(foundAt + 1); } for (;;) { int foundAt = orig.indexOf("/./"); if (foundAt == -1) { break; } orig = orig.substring(0, foundAt) + orig.substring(foundAt + 2); } while (orig.endsWith("/.")) { orig = orig.substring(0, orig.length() - 2); } for (;;) { int foundAt = orig.indexOf("/../"); if (foundAt == -1) { break; } int prevSlash = orig.lastIndexOf('/', foundAt - 1); if (prevSlash == -1) { // can't take .. at the beginning of the document return null; } orig = orig.substring(0, prevSlash) + orig.substring(foundAt + 3); } while (orig.endsWith("/..")) { int prevSlash = orig.lastIndexOf('/', orig.length() - 3); if (prevSlash <= 0) { // can't take .. at the beginning of the document return null; } orig = orig.substring(0, prevSlash); } return orig; } }