KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > PathPackageManager


1 // Copyright (c) Corporation for National Research Initiatives
2
// Copyright 2000 Samuele Pedroni
3

4 package org.python.core;
5
6 import java.io.BufferedInputStream JavaDoc;
7 import java.io.File JavaDoc;
8 import java.io.FileInputStream JavaDoc;
9 import java.io.FilenameFilter JavaDoc;
10 import java.io.IOException JavaDoc;
11
12 /**
13  * Path package manager. Gathering classes info dynamically
14  * from a set of directories in path {@link #searchPath}, and
15  * statically from a set of jars, like {@link CachedJarsPackageManager}.
16  */

17 public abstract class PathPackageManager extends CachedJarsPackageManager {
18
19     public PyList searchPath;
20
21     public PathPackageManager() {
22         searchPath = new PyList();
23     }
24
25     /**
26      * Helper for {@link #packageExists(java.lang.String,java.lang.String)}.
27      * Scans for package pkg.name the directories in path.
28      */

29     protected boolean packageExists(PyList path, String JavaDoc pkg, String JavaDoc name) {
30         String JavaDoc child = pkg.replace('.', File.separatorChar) + File.separator + name;
31
32         for (int i=0; i < path.__len__(); i++) {
33             String JavaDoc dir = path.pyget(i).__str__().toString();
34             if (dir.length() == 0) dir = null;
35
36             File JavaDoc f = new File JavaDoc(dir,child);
37             if (f.isDirectory() && imp.caseok(f, name, name.length())) {
38                 /*
39                  Figure out if we have a directory a mixture of python and java
40                  or just an empty directory (which means Java) or a directory
41                  with only Python source (which means Python).
42                  */

43                 PackageExistsFileFilter m = new PackageExistsFileFilter();
44                 f.listFiles(m);
45                 boolean exists = m.packageExists();
46                 if(exists) {
47                     Py.writeComment("import", "java package as '" + f.getAbsolutePath() + "'");
48                 }
49                 return exists;
50             }
51         }
52         return false;
53     }
54
55     class PackageExistsFileFilter implements FilenameFilter JavaDoc {
56         private boolean java;
57         private boolean python;
58         public boolean accept(File JavaDoc dir, String JavaDoc name) {
59             if(name.endsWith(".py") || name.endsWith("$py.class")) {
60                 python = true;
61             } else if(name.endsWith(".class")) {
62                 java = true;
63             }
64             return false;
65         }
66
67         public boolean packageExists() {
68             if(python && !java) {
69                return false;
70             }
71             return true;
72         }
73     }
74
75     /**
76      * Helper for {@link #doDir(PyJavaPackage,boolean,boolean)}.
77      * Scans for package jpkg content over the directories in path.
78      * Add to ret the founded classes/pkgs.
79      * Filter out classes using {@link #filterByName},{@link #filterByAccess}.
80      */

81     protected void doDir(PyList path, PyList ret, PyJavaPackage jpkg,
82                          boolean instantiate,boolean exclpkgs)
83     {
84         String JavaDoc child=jpkg.__name__.replace('.',File.separatorChar);
85
86         for (int i=0; i < path.__len__(); i++) {
87             String JavaDoc dir = path.pyget(i).__str__().toString();
88             if (dir.length() == 0) dir = null;
89
90             File JavaDoc childFile = new File JavaDoc(dir,child);
91
92             String JavaDoc[] list=childFile.list();
93             if(list == null) continue;
94
95             doList:
96             for (int j=0; j < list.length; j++) {
97                 String JavaDoc jname = list[j];
98
99                 File JavaDoc cand = new File JavaDoc(childFile, jname);
100
101                 int jlen = jname.length();
102
103                 boolean pkgCand=false;
104
105                 if (cand.isDirectory()) {
106                     if (!instantiate && exclpkgs) continue;
107                     pkgCand = true;
108                 } else {
109                     if(!jname.endsWith(".class")) continue;
110                     jlen-=6;
111                 }
112
113                 jname = jname.substring(0,jlen);
114                 PyString name = new PyString(jname);
115
116                 if (filterByName(jname,pkgCand)) continue;
117
118                 // for opt maybe we should some hash-set for ret
119
if (jpkg.__dict__.has_key(name) ||
120                       jpkg.clsSet.has_key(name) ||
121                       ret.__contains__(name)) {
122                     continue;
123                 }
124
125                 if (!Character.isJavaIdentifierStart(jname.charAt(0)))
126                     continue;
127                 for (int k = 1; k < jlen; k++) {
128                     if (!Character.isJavaIdentifierPart(jname.charAt(k)))
129                         continue doList;
130                 }
131
132                 if(!pkgCand) {
133                     try {
134                         int acc = checkAccess(new BufferedInputStream JavaDoc(
135                                               new FileInputStream JavaDoc(cand)));
136                         if ((acc == -1) || filterByAccess(jname, acc))
137                             continue;
138                     } catch(IOException JavaDoc e) {
139                         continue;
140                     }
141                 }
142
143                 if (instantiate) {
144                     if (pkgCand) jpkg.addPackage(jname);
145                     else jpkg.addLazyClass(jname);
146                 }
147
148                 ret.append(name);
149
150             }
151         }
152
153     }
154
155     /**
156      * Add directory dir (if exists) to {@link #searchPath}.
157      */

158     public void addDirectory(File JavaDoc dir) {
159         try {
160             if (dir.getPath().length() == 0)
161                 searchPath.append(Py.EmptyString);
162             else
163                 searchPath.append(new PyString(dir.getCanonicalPath()));
164         } catch(IOException JavaDoc e) {
165             warning("skipping bad directory, '" +dir+ "'");
166         }
167     }
168
169
170     // ??pending:
171
// Uses simply split and not a StringTokenizer+trim to adhere to
172
// sun jvm parsing of classpath.
173
// E.g. "a;" is parsed by sun jvm as a, ""; the latter is interpreted
174
// as cwd. jview trims and cwd is per default in classpath.
175
// The logic here should work for both(...). Need to distinguish?
176
// This code does not avoid duplicates in searchPath.
177
// Should cause no problem (?).
178

179     /**
180      * Adds "classpath" entry. Calls {@link #addDirectory} if path
181      * refers to a dir, {@link #addJarToPackages(java.io.File, boolean)}
182      * with param cache true if path refers to a jar.
183      */

184     public void addClassPath(String JavaDoc path) {
185         PyList paths = new PyString(path).split(java.io.File.pathSeparator);
186
187         for (int i = 0; i < paths.__len__(); i++) {
188             String JavaDoc entry = paths.pyget(i).toString();
189             if (entry.endsWith(".jar") || entry.endsWith(".zip")) {
190                 addJarToPackages(new File JavaDoc(entry),true);
191             } else {
192                 File JavaDoc dir = new File JavaDoc(entry);
193                 if (entry.length() == 0 || dir.isDirectory())
194                     addDirectory(dir);
195             }
196         }
197     }
198
199     public PyList doDir(PyJavaPackage jpkg, boolean instantiate, boolean exclpkgs) {
200         PyList basic = basicDoDir(jpkg,instantiate,exclpkgs);
201         PyList ret = new PyList();
202
203         doDir(searchPath,ret,jpkg,instantiate,exclpkgs);
204
205         return merge(basic,ret);
206     }
207
208     public boolean packageExists(String JavaDoc pkg,String JavaDoc name) {
209         return packageExists(searchPath,pkg,name);
210     }
211
212 }
213
Popular Tags