1 package org.apache.maven.plugins.site.wagon;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.wagon.WagonConstants;
23
24 import java.io.File;
25 import java.util.StringTokenizer;
26
27
28
29
30
31
32
33
34
35
36 public final class PathUtils
37 {
38 private PathUtils()
39 {
40 }
41
42
43
44
45
46
47
48 public static String dirname( final String path )
49 {
50 final int i = path.lastIndexOf( "/" );
51
52 return ( ( i >= 0 ) ? path.substring( 0, i ) : "" );
53 }
54
55
56
57
58
59
60 public static String filename( final String path )
61 {
62 final int i = path.lastIndexOf( "/" );
63 return ( ( i >= 0 ) ? path.substring( i + 1 ) : path );
64 }
65
66 public static String[] dirnames( final String path )
67 {
68 final String dirname = PathUtils.dirname( path );
69 return split( dirname, "/", -1 );
70
71 }
72
73 private static String[] split( final String str, final String separator, final int max )
74 {
75 final StringTokenizer tok;
76
77 if ( separator == null )
78 {
79
80
81 tok = new StringTokenizer( str );
82 }
83 else
84 {
85 tok = new StringTokenizer( str, separator );
86 }
87
88 int listSize = tok.countTokens();
89
90 if ( max > 0 && listSize > max )
91 {
92 listSize = max;
93 }
94
95 final String[] list = new String[listSize];
96
97 int i = 0;
98
99 int lastTokenBegin;
100 int lastTokenEnd = 0;
101
102 while ( tok.hasMoreTokens() )
103 {
104 if ( max > 0 && i == listSize - 1 )
105 {
106
107
108
109 final String endToken = tok.nextToken();
110
111 lastTokenBegin = str.indexOf( endToken, lastTokenEnd );
112
113 list[i] = str.substring( lastTokenBegin );
114
115 break;
116
117 }
118 else
119 {
120 list[i] = tok.nextToken();
121
122 lastTokenBegin = str.indexOf( list[i], lastTokenEnd );
123
124 lastTokenEnd = lastTokenBegin + list[i].length();
125 }
126
127 i++;
128 }
129 return list;
130 }
131
132
133
134
135
136
137
138
139 public static String host( final String url )
140 {
141 String authorization = authorization( url );
142 int index = authorization.indexOf( '@' );
143 if ( index >= 0 )
144 {
145 return authorization.substring( index + 1 );
146 }
147 else
148 {
149 return authorization;
150 }
151 }
152
153
154
155
156 static String authorization( final String url )
157 {
158 if ( url == null )
159 {
160 return "localhost";
161 }
162
163 final String protocol = PathUtils.protocol( url );
164
165 if ( protocol == null || protocol.equalsIgnoreCase( "file" ) )
166 {
167 return "localhost";
168 }
169
170 String host = url;
171 if ( protocol.equalsIgnoreCase( "scm" ) )
172 {
173
174 host = host.substring( host.indexOf( ":", 4 ) + 1 ).trim();
175 }
176
177
178 host = host.substring( host.indexOf( ":" ) + 1 ).trim();
179 if ( host.startsWith( "//" ) )
180 {
181 host = host.substring( 2 );
182 }
183
184 int pos = host.indexOf( "/" );
185
186 if ( pos > 0 )
187 {
188 host = host.substring( 0, pos );
189 }
190
191 pos = host.indexOf( '@' );
192
193 if ( pos > 0 )
194 {
195 pos = host.indexOf( ':', pos );
196 }
197 else
198 {
199 pos = host.indexOf( ":" );
200 }
201
202 if ( pos > 0 )
203 {
204 host = host.substring( 0, pos );
205 }
206 return host;
207 }
208
209
210
211
212
213
214
215
216
217
218
219 public static String protocol( final String url )
220 {
221 final int pos = url.indexOf( ":" );
222
223 if ( pos == -1 )
224 {
225 return "";
226 }
227 return url.substring( 0, pos ).trim();
228 }
229
230
231
232
233
234 public static int port( String url )
235 {
236
237 final String protocol = PathUtils.protocol( url );
238
239 if ( protocol == null || protocol.equalsIgnoreCase( "file" ) )
240 {
241 return WagonConstants.UNKNOWN_PORT;
242 }
243
244 final String authorization = PathUtils.authorization( url );
245
246 if ( authorization == null )
247 {
248 return WagonConstants.UNKNOWN_PORT;
249 }
250
251 if ( protocol.equalsIgnoreCase( "scm" ) )
252 {
253
254 url = url.substring( url.indexOf( ":", 4 ) + 1 ).trim();
255 }
256
257 if ( url.regionMatches( true, 0, "file:", 0, 5 ) || url.regionMatches( true, 0, "local:", 0, 6 ) )
258 {
259 return WagonConstants.UNKNOWN_PORT;
260 }
261
262
263 url = url.substring( url.indexOf( ":" ) + 1 ).trim();
264 if ( url.startsWith( "//" ) )
265 {
266 url = url.substring( 2 );
267 }
268
269 int start = authorization.length();
270
271 if ( url.length() > start && url.charAt( start ) == ':' )
272 {
273 int end = url.indexOf( '/', start );
274
275 if ( end == start + 1 )
276 {
277
278 return WagonConstants.UNKNOWN_PORT;
279 }
280
281 if ( end == -1 )
282 {
283 end = url.length();
284 }
285
286 return Integer.parseInt( url.substring( start + 1, end ) );
287 }
288 else
289 {
290 return WagonConstants.UNKNOWN_PORT;
291 }
292
293 }
294
295
296
297
298
299
300
301
302 public static String basedir( String url )
303 {
304 String protocol = PathUtils.protocol( url );
305
306 String retValue = null;
307
308 if ( protocol.equalsIgnoreCase( "scm" ) )
309 {
310
311 if ( url.regionMatches( true, 0, "scm:svn:", 0, 8 ) )
312 {
313 url = url.substring( url.indexOf( ":", 4 ) + 1 );
314 protocol = PathUtils.protocol( url );
315 }
316 }
317
318 if ( protocol.equalsIgnoreCase( "file" ) )
319 {
320 retValue = url.substring( protocol.length() + 1 );
321 retValue = decode( retValue );
322
323 if ( retValue.startsWith( "//" ) )
324 {
325 retValue = retValue.substring( 2 );
326
327 if ( retValue.length() >= 2 && ( retValue.charAt( 1 ) == '|' || retValue.charAt( 1 ) == ':' ) )
328 {
329
330 retValue = retValue.charAt( 0 ) + ":" + retValue.substring( 2 );
331 }
332 else
333 {
334
335 int index = retValue.indexOf( "/" );
336 if ( index >= 0 )
337 {
338 retValue = retValue.substring( index + 1 );
339 }
340
341
342 if ( retValue.length() >= 2 && ( retValue.charAt( 1 ) == '|' || retValue.charAt( 1 ) == ':' ) )
343 {
344 retValue = retValue.charAt( 0 ) + ":" + retValue.substring( 2 );
345 }
346 else if ( index >= 0 )
347 {
348
349 retValue = "/" + retValue;
350 }
351 }
352 }
353
354
355 if ( retValue.length() >= 2 && retValue.charAt( 1 ) == '|' )
356 {
357 retValue = retValue.charAt( 0 ) + ":" + retValue.substring( 2 );
358 }
359 }
360 else
361 {
362 final String authorization = PathUtils.authorization( url );
363
364 final int port = PathUtils.port( url );
365
366 int pos = 0;
367
368 if ( protocol.equalsIgnoreCase( "scm" ) )
369 {
370 pos = url.indexOf( ":", 4 ) + 1;
371 pos = url.indexOf( ":", pos ) + 1;
372 }
373 else
374 {
375 int index = url.indexOf( "://" );
376 if( index != -1 ) {
377 pos = index + 3;
378 }
379 }
380
381 pos += authorization.length();
382
383 if ( port != WagonConstants.UNKNOWN_PORT )
384 {
385 pos = pos + Integer.toString( port ).length() + 1;
386 }
387
388 if ( url.length() > pos )
389 {
390 retValue = url.substring( pos );
391 if ( retValue.startsWith( ":" ) )
392 {
393
394 retValue = retValue.substring( 1 );
395 }
396
397
398 retValue = retValue.replace( ':', '/' );
399 }
400 }
401
402 if ( retValue == null )
403 {
404 retValue = "/";
405 }
406 return retValue.trim();
407 }
408
409
410
411
412
413
414
415
416 private static String decode( String url )
417 {
418 String decoded = url;
419 if ( url != null )
420 {
421 int pos = -1;
422 while ( ( pos = decoded.indexOf( '%', pos + 1 ) ) >= 0 )
423 {
424 if ( pos + 2 < decoded.length() )
425 {
426 String hexStr = decoded.substring( pos + 1, pos + 3 );
427 char ch = (char) Integer.parseInt( hexStr, 16 );
428 decoded = decoded.substring( 0, pos ) + ch + decoded.substring( pos + 3 );
429 }
430 }
431 }
432 return decoded;
433 }
434
435 public static String user( String url )
436 {
437 String host = authorization( url );
438 int index = host.indexOf( '@' );
439 if ( index > 0 )
440 {
441 String userInfo = host.substring( 0, index );
442 index = userInfo.indexOf( ':' );
443 if ( index > 0 )
444 {
445 return userInfo.substring( 0, index );
446 }
447 else if ( index < 0 )
448 {
449 return userInfo;
450 }
451 }
452 return null;
453 }
454
455 public static String password( String url )
456 {
457 String host = authorization( url );
458 int index = host.indexOf( '@' );
459 if ( index > 0 )
460 {
461 String userInfo = host.substring( 0, index );
462 index = userInfo.indexOf( ':' );
463 if ( index >= 0 )
464 {
465 return userInfo.substring( index + 1 );
466 }
467 }
468 return null;
469 }
470
471
472 public static String toRelative( File basedir, String absolutePath )
473 {
474 String relative;
475
476 absolutePath = absolutePath.replace( '\\', '/' );
477 String basedirPath = basedir.getAbsolutePath().replace( '\\', '/' );
478
479 if ( absolutePath.startsWith( basedirPath ) )
480 {
481 relative = absolutePath.substring( basedirPath.length() );
482 if ( relative.startsWith( "/" ) )
483 {
484 relative = relative.substring( 1 );
485 }
486 if ( relative.length() <= 0 )
487 {
488 relative = ".";
489 }
490 }
491 else
492 {
493 relative = absolutePath;
494 }
495
496 return relative;
497 }
498 }