1 4 package org.python.core; 5 6 import java.io.BufferedInputStream ; 7 import java.io.File ; 8 import java.io.FileInputStream ; 9 import java.io.FilenameFilter ; 10 import java.io.IOException ; 11 12 17 public abstract class PathPackageManager extends CachedJarsPackageManager { 18 19 public PyList searchPath; 20 21 public PathPackageManager() { 22 searchPath = new PyList(); 23 } 24 25 29 protected boolean packageExists(PyList path, String pkg, String name) { 30 String child = pkg.replace('.', File.separatorChar) + File.separator + name; 31 32 for (int i=0; i < path.__len__(); i++) { 33 String dir = path.pyget(i).__str__().toString(); 34 if (dir.length() == 0) dir = null; 35 36 File f = new File (dir,child); 37 if (f.isDirectory() && imp.caseok(f, name, name.length())) { 38 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 { 56 private boolean java; 57 private boolean python; 58 public boolean accept(File dir, String 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 81 protected void doDir(PyList path, PyList ret, PyJavaPackage jpkg, 82 boolean instantiate,boolean exclpkgs) 83 { 84 String child=jpkg.__name__.replace('.',File.separatorChar); 85 86 for (int i=0; i < path.__len__(); i++) { 87 String dir = path.pyget(i).__str__().toString(); 88 if (dir.length() == 0) dir = null; 89 90 File childFile = new File (dir,child); 91 92 String [] list=childFile.list(); 93 if(list == null) continue; 94 95 doList: 96 for (int j=0; j < list.length; j++) { 97 String jname = list[j]; 98 99 File cand = new File (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 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 ( 135 new FileInputStream (cand))); 136 if ((acc == -1) || filterByAccess(jname, acc)) 137 continue; 138 } catch(IOException 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 158 public void addDirectory(File 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 e) { 165 warning("skipping bad directory, '" +dir+ "'"); 166 } 167 } 168 169 170 179 184 public void addClassPath(String path) { 185 PyList paths = new PyString(path).split(java.io.File.pathSeparator); 186 187 for (int i = 0; i < paths.__len__(); i++) { 188 String entry = paths.pyget(i).toString(); 189 if (entry.endsWith(".jar") || entry.endsWith(".zip")) { 190 addJarToPackages(new File (entry),true); 191 } else { 192 File dir = new File (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 pkg,String name) { 209 return packageExists(searchPath,pkg,name); 210 } 211 212 } 213 | Popular Tags |