1 package org.apache.maven.simian;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import java.io.File;
21
22 import java.net.URL;
23
24 import java.util.StringTokenizer;
25
26 /**
27 *
28 * @author Aslak Hellesoy
29 * @version $Revision: 371231 $
30 */
31 public class FileUtils
32 {
33 /**
34 * Returns the root directory of the package hierarchy where this class is
35 * located. This will either be a directory or a jar file.
36 *
37 * @return the root directory or jar file where the class is located.
38 */
39 public static File getRoot( Class clazz )
40 {
41 File dir;
42 URL classURL =
43 clazz.getResource( "/" + clazz.getName().replace( '.', '/' )
44 + ".class" );
45 String classFile = classURL.getFile();
46
47 if ( classFile.indexOf( '!' ) != -1 )
48 {
49
50 if ( classFile.charAt( 5 ) != '/' )
51 {
52 classFile = "file:/" + classFile.substring( 5 );
53 }
54
55 classFile = classFile.replace( '\\', '/' );
56
57 String uriSpec = classFile.substring( 0, classFile.indexOf( '!' ) );
58
59 dir = new File( uriSpec );
60
61
62
63
64
65
66
67
68
69 }
70 else
71 {
72 dir = new File( classFile ).getParentFile();
73
74 StringTokenizer st = new StringTokenizer( clazz.getName(), "." );
75
76 for ( int i = 0; i < ( st.countTokens() - 1 ); i++ )
77 {
78 dir = dir.getParentFile();
79 }
80 }
81
82 return dir;
83 }
84 }