View Javadoc

1   package org.apache.maven.ashkelon;
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  import java.io.FileFilter;
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  /**
27   * @author <a href="bwalding@jakarta.org">Ben Walding</a>
28   * @version $Id: PackageTool.java 170200 2005-05-15 06:24:19Z brett $
29   */
30  public class PackageTool
31  {
32  
33      /**
34       * Count the number of java files in a directory
35       * @param dir
36       * @return
37       */
38      public static int countJavaFiles(File dir)
39      {
40          FileFilter ff = new FileFilter()
41          {
42              public boolean accept(File pathname)
43              {
44                  String path = pathname.getName().toLowerCase();
45                  if (path.endsWith(".java"))
46                  {
47                      return true;
48                  }
49  
50                  return false;
51              }
52          };
53  
54          File[] f = dir.listFiles(ff);
55  
56          return f.length;
57  
58      }
59      /**
60       * 
61       * @param startDir
62       * @return a List of String object representing the packages
63       */
64      public List findPackages(File startDir)
65      {
66          List results = internalFindPackages(new ArrayList(), startDir);
67          //      Now post process them
68          List packages = new ArrayList(results.size());
69  
70          Iterator resultsIter = results.iterator();
71          while (resultsIter.hasNext())
72          {
73              File f = (File) resultsIter.next();
74              String n = f.getAbsolutePath();
75  
76              if (n.equals(startDir.getAbsolutePath()))
77              {
78                  //Default package
79                  packages.add("");
80              }
81              else
82              {
83  
84                  n = n.substring(startDir.getAbsolutePath().length() + 1);
85                  n = n.replace('/', '.');
86                  n = n.replace('\\', '.');
87                  packages.add(n);
88              }
89          }
90          return packages;
91      }
92  
93      public File[] getSubDir(File dir)
94      {
95  
96          FileFilter ff = new FileFilter()
97          {
98              public boolean accept(File pathname)
99              {
100                 return pathname.isDirectory();
101             }
102         };
103         return dir.listFiles(ff);
104     }
105 
106     protected List internalFindPackages(List results, File startDir)
107     {
108         if (countJavaFiles(startDir) != 0)
109         {
110 
111             results.add(startDir);
112         }
113         File subdirs[] = getSubDir(startDir);
114         for (int i = 0; i < subdirs.length; i++)
115         {
116             internalFindPackages(results, subdirs[i]);
117         }
118 
119         return results;
120     }
121 }