Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: mijava/PackageScanner.java


1   /*
2     @(#) $Id: PackageScanner.java,v 1.4 2002/03/25 20:42:53 hobb0001 Exp $
3     Copyright 2002 Michael Hobbs
4   
5     This file is part of MIJava.
6   
7     MIJava is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11  
12    MIJava is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16  
17    You should have received a copy of the GNU General Public License
18    along with MIJava; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21  
22  package mijava;
23  
24  import java.lang.Class;   // instead of mijava.Class
25  import java.io.File;
26  import java.io.InputStream;
27  import java.io.FileInputStream;
28  import java.io.IOException;
29  import java.util.Collection;
30  import java.util.List;
31  import java.util.Enumeration;
32  import java.util.ArrayList;
33  import java.util.zip.ZipFile;
34  import java.util.zip.ZipEntry;
35  
36  
37  
38  class PackageScanner
39  {
40  
41  
42    public static Collection getClasses(String packageName)
43    {
44      Collection result = new ArrayList();
45      String[] classPaths = MIJava.getClassPath().split(File.pathSeparator+"+");
46      for (int i=0; i<classPaths.length; ++i) {
47        File file = new File(classPaths[i]);
48        if (file.isDirectory()) {
49          result.addAll(scanDirectory(file, packageName));
50        } else if (file.isFile()) {
51          // Assume the file is an archive.
52          result.addAll(scanArchive(file, packageName));
53        }
54      }
55      return result;
56    }
57  
58    public static Class getClass(String packageName, String className)
59      throws ClassNotFoundException
60    {
61      String standardClassName = "";
62      if (packageName.length() > 0) {
63        standardClassName = packageName + ".";
64      }
65      standardClassName += className.replace('.', '$');
66      return Class.forName(standardClassName);
67    }
68  
69    public static Class getClass(Parser.ImportInfo importInfo)
70      throws ClassNotFoundException
71    {
72      return getClass(importInfo.packageName, importInfo.className);
73    }
74  
75    private static Collection scanDirectory(File dir, String packageName)
76    {
77      Collection result = new ArrayList();
78      File packageDir = findPackageDir(dir, packageName);
79      if (packageDir == null) return result;
80      String classNamePrepend = "";
81      if (packageName.length() > 0) {
82        classNamePrepend = packageName + ".";
83      }
84      String[] files = packageDir.list();
85      for (int i=0; i<files.length; ++i) {
86        if (files[i].endsWith(".class")) {
87          String className = files[i].substring(0, files[i].length() - 6);
88          Class clazz = MIJava.findClass(packageName, className);
89          if (clazz != null) result.add(clazz);
90        }
91      }
92      return result;
93    }
94  
95    private static File findPackageDir(File dir, String packageName)
96    {
97      if (packageName.length() == 0) return dir;
98      String dirName;
99      int dotIdx = packageName.indexOf('.');
100     if (dotIdx >= 0) {
101       dirName = packageName.substring(0, dotIdx);
102       packageName = packageName.substring(dotIdx + 1);
103     } else {
104       dirName = packageName;
105       packageName = "";
106     }
107     File subdir = new File(dir, dirName);
108     if (!subdir.isDirectory()) {
109       return null;
110     }
111     return findPackageDir(subdir, packageName);
112   }
113 
114   private static Collection scanArchive(File archive, String packageName)
115   {
116     Collection result = new ArrayList();
117     String archiveEntryName = "";
118     if (packageName.length() > 0) {
119       archiveEntryName = packageName.replace('.', '/') + "/";
120     }
121     ZipFile zipFile;
122     try {
123       zipFile = new ZipFile(archive);
124     } catch (IOException e) {
125       return result;
126     }    
127     for (Enumeration i=zipFile.entries(); i.hasMoreElements(); ) {
128       ZipEntry entry = (ZipEntry) i.nextElement();
129       String entryName = entry.getName();
130       if (!entry.isDirectory() &&
131           entryName.startsWith(archiveEntryName) &&
132           entryName.endsWith(".class") &&
133           entryName.indexOf('/', archiveEntryName.length()) == -1) {
134         int slashPos = entryName.lastIndexOf('/');
135         String className = 
136           entryName.substring(slashPos+1, entryName.length()-6);
137         Class clazz = MIJava.findClass(packageName, className);
138         if (clazz != null) result.add(clazz);
139       }
140     }
141     try {
142       zipFile.close();
143     } catch (IOException e) {}
144     return result;
145   }
146 
147   private static final String _ID_ =
148     "@(#) $Id: PackageScanner.java,v 1.4 2002/03/25 20:42:53 hobb0001 Exp $";
149 }