Initial Commit
[packages] / xemacs-packages / jde / java / src / jde / util / ClassPathDir.java
1 package jde.util;
2
3 import java.io.File;
4
5 /**
6  * A ClassPathEntry that represents a directory in which classes are
7  * stored.  This is scanned recursively for classes at load time.
8  *
9  * Copyright (C) 2001, 2002, 2003 Eric D. Friedman (eric@hfriedman.rdsl.lmi.net)
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  * Created: Tue Aug 14 19:46:52 2001
26  *
27  * @author Eric D. Friedman
28  * @version $Id: ClassPathDir.java,v 1.4 2003/10/20 03:55:49 paulk Exp $
29  */
30
31 class ClassPathDir extends ClassPathEntry {
32     private File directory;
33     
34     /**
35      * Create an instance of ClassPathDir representing
36      * <code>directory</code>
37      *
38      * @param directory a <code>File</code> value
39      * @return a <code>ClassPathDir</code> value
40      */
41     ClassPathDir (File directory) {
42         super();
43         this.directory = directory;
44     }
45
46     /**
47      * Perform a recursive scan of the directory and set the loaded
48      * flag to true.
49      *
50      */
51     void load() {
52         addRecursively(directory,directory);
53         setLoaded(true);
54     }
55
56     /**
57      * Search for classes in <code>directory</code> rooted at
58      * <code>rootDir</code>
59      *
60      * @param directory a <code>File</code> value
61      * @param rootDir a <code>File</code> value
62      */
63     void addRecursively(File directory, File rootDir) {
64         String [] files = directory.list();
65
66         if (files == null) {
67             System.err.println("Cannot read contents of " + directory + ".");
68             return;
69         } // end of if ()
70
71         String current;
72         String rootPath = rootDir.getPath();
73         String currentPath = directory.getPath();
74         String packageName = currentPath.substring(rootPath.length());
75         StringBuffer buf = new StringBuffer();
76
77         if (packageName.length() > 0) {
78            // Not the current directory
79            packageName = packageName.replace('\\', '.');
80            packageName = packageName.replace('/', '.');
81            packageName = packageName.substring(1);
82         }
83     
84         for (int i = 0; i < files.length; i++) {
85             current = files[i];
86             if (current.toLowerCase().endsWith(".class")) {
87                 current = current.substring(0, current.length() - 6);
88                 current = current.replace('$', '.'); // To handle inner-class .class files
89                 if (currentPath.indexOf(rootPath) != 0) {
90                     System.err.println("currentPath doesn't start with rootPath!\n"
91                                        + "rootPath: " + rootPath + "\n"
92                                        + "currentPath: " + currentPath + "\n");
93                 } else if (packageName.length() > 0) {
94                     // not the default package
95                     buf.append(packageName);
96                     buf.append('.');
97                     buf.append(current);
98                     addClass(buf.toString());
99                     buf.setLength(0);
100                 } else {
101                     // The default package
102                     addClass(current);
103                 }
104             } else {
105                 // Check if it's a directory to recurse into
106                 File currentFile = new File(directory, current);
107                 if (currentFile.isDirectory()) {
108                     addRecursively(currentFile,rootDir);
109                 }
110             }
111         }
112     }
113
114     /**
115      * return the directory as our string value.
116      *
117      * @return a <code>String</code> value
118      */
119     public String toString() {
120         return directory.toString();
121     }
122 }// ClassPathDir
123
124 /*
125  * $Log: ClassPathDir.java,v $
126  * Revision 1.4  2003/10/20 03:55:49  paulk
127  * Fix to handle inner classes.
128  *
129  *
130  *
131  */
132
133 // End of ClassPathDir.java