1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.transport.file;
20
21
22
23
24 final class PathUtils {
25
26 private PathUtils() {}
27
28
29
30
31
32
33
34
35 public static String protocol(final String url) {
36 final int pos = url.indexOf(":");
37
38 if (pos == -1) {
39 return "";
40 }
41 return url.substring(0, pos).trim();
42 }
43
44
45
46
47
48
49
50 public static String basedir(String url) {
51 String protocol = PathUtils.protocol(url);
52
53 String retValue = null;
54
55 if (protocol.length() > 0) {
56 retValue = url.substring(protocol.length() + 1);
57 } else {
58 retValue = url;
59 }
60 retValue = decode(retValue);
61
62 if (retValue.startsWith("//")) {
63 retValue = retValue.substring(2);
64
65 if (retValue.length() >= 2 && (retValue.charAt(1) == '|' || retValue.charAt(1) == ':')) {
66
67 retValue = retValue.charAt(0) + ":" + retValue.substring(2);
68 } else {
69
70 int index = retValue.indexOf("/");
71 if (index >= 0) {
72 retValue = retValue.substring(index + 1);
73 }
74
75
76 if (retValue.length() >= 2 && (retValue.charAt(1) == '|' || retValue.charAt(1) == ':')) {
77 retValue = retValue.charAt(0) + ":" + retValue.substring(2);
78 } else if (index >= 0) {
79
80 retValue = "/" + retValue;
81 }
82 }
83 }
84
85
86 if (retValue.length() >= 2 && retValue.charAt(1) == '|') {
87 retValue = retValue.charAt(0) + ":" + retValue.substring(2);
88 }
89
90 return retValue.trim();
91 }
92
93
94
95
96
97
98
99
100 static String decode(String url) {
101 String decoded = url;
102 if (url != null) {
103 int pos = -1;
104 while ((pos = decoded.indexOf('%', pos + 1)) >= 0) {
105 if (pos + 2 < decoded.length()) {
106 String hexStr = decoded.substring(pos + 1, pos + 3);
107 char ch = (char) Integer.parseInt(hexStr, 16);
108 decoded = decoded.substring(0, pos) + ch + decoded.substring(pos + 3);
109 }
110 }
111 }
112 return decoded;
113 }
114 }