1   package org.apache.maven.simian;
2   
3   /* ====================================================================
4    *   Copyright 2001-2004 The Apache Software Foundation.
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
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              // Sometimes (at least on windows) we get file:F:\bla. Convert to file:/F:/bla
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              // NOTE: This used to be here, but I've replaced it with the above single
62              //       line without understanding the ramifications. The code below needs JDK1.4
63              //try {
64              //    dir = new File( new URL( uriSpec ) );
65              //} catch (URISyntaxException e) {
66              //    System.err.println("Couldn't create URI for " + uriSpec);
67              //    throw new IllegalStateException(e.getMessage());
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  }