3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.Iterator;
9
* @author Kohsuke Kawaguchi
11
public class URIUtil {
13
* Eliminates "." and ".." in the path.
14
* So that this method can be used for any string that looks like an URI,
15
* this method preserves the leading and trailing '/'.
17
static String canonicalPath(String path) {
18
List r = new ArrayList(Arrays.asList(path.split("/+")));
19
for (int i=0; i<r.size(); ) {
20
String cur = (String)r.get(i);
21
if (cur.length()==0 || cur.equals(".")) {
22
// empty token occurs for example, "".split("/+") is [""]
25
if (cur.equals("..")) {
26
// i==0 means this is a broken URI.
37
StringBuilder buf = new StringBuilder();
38
if (path.startsWith("/"))
41
for (Iterator itr = r.iterator(); itr.hasNext();) {
42
String token = (String) itr.next();
43
if (!first) buf.append('/');
47
// translation: if (path.endsWith("/") && !buf.endsWith("/"))
48
if (path.endsWith("/") && (buf.length()==0 || buf.charAt(buf.length()-1)!='/'))
50
return buf.toString();