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