1 4 package org.python.core; 5 6 import java.util.*; 7 import java.io.File ; 8 9 12 13 public class PyJavaPackage extends PyObject { 14 public String __name__; 15 16 17 public PyStringMap __dict__; 18 22 public PyStringMap clsSet; 23 public String __file__; 24 26 28 public PackageManager __mgr__; 29 30 public PyJavaPackage(String name) { 31 this(name, null, null); 32 } 33 34 public PyJavaPackage(String name,String jarfile) { 35 this(name, null, jarfile); 36 } 37 38 public PyJavaPackage(String name,PackageManager mgr) { 39 this(name, mgr, null); 40 } 41 42 43 public PyJavaPackage(String name,PackageManager mgr,String jarfile) { 44 __file__ = jarfile; 45 __name__ = name; 46 47 if( mgr == null ) 48 __mgr__ = PySystemState.packageManager; else 50 __mgr__ = mgr; 51 52 clsSet= new PyStringMap(); 53 54 __dict__ = new PyStringMap(); 55 __dict__.__setitem__("__name__", new PyString(__name__)); 56 } 57 58 public PyJavaPackage addPackage(String name) { 59 return addPackage(name, null); 60 } 61 62 public PyJavaPackage addPackage(String name, String jarfile) { 63 int dot = name.indexOf('.'); 64 String firstName=name; 65 String lastName=null; 66 if (dot != -1) { 67 firstName = name.substring(0,dot); 68 lastName = name.substring(dot+1, name.length()); 69 } 70 firstName = firstName.intern(); 71 PyJavaPackage p = (PyJavaPackage)__dict__.__finditem__(firstName); 72 if (p == null) { 73 String pname = __name__.length() == 0 ? 74 firstName : __name__+'.'+firstName; 75 p = new PyJavaPackage(pname, __mgr__, jarfile); 76 __dict__.__setitem__(firstName, p); 77 } else { 78 if (jarfile == null || !jarfile.equals(p.__file__)) 81 p.__file__ = null; 82 } 83 if (lastName != null) return p.addPackage(lastName, jarfile); 84 else return p; 85 } 86 87 public PyObject addClass(String name,Class c) { 88 PyObject ret = Py.java2py(c); 92 __dict__.__setitem__(name.intern(), ret); 93 return ret; 94 } 95 96 public PyObject addLazyClass(String name) { 97 PyObject ret = PyJavaClass.lookup(__name__+'.'+name,__mgr__); 99 __dict__.__setitem__(name.intern(), ret); 100 return ret; 101 } 102 103 106 public void addPlaceholders(String classes) { 107 StringTokenizer tok = new StringTokenizer(classes, ",@"); 108 while (tok.hasMoreTokens()) { 109 String p = tok.nextToken(); 110 String name = p.trim().intern(); 111 if (clsSet.__finditem__(name) == null) 112 clsSet.__setitem__(name, Py.One); 113 } 114 } 115 116 public PyObject __dir__() { 117 return __mgr__.doDir(this,false,false); 118 } 119 120 129 public PyObject fillDir() { 130 return __mgr__.doDir(this,true,false); 131 } 132 133 134 public PyObject __findattr__(String name) { 135 136 PyObject ret = __dict__.__finditem__(name); 137 if (ret != null) return ret; 138 139 if (__mgr__.packageExists(__name__,name)) { 140 __mgr__.notifyPackageImport(__name__,name); 141 return addPackage(name); 142 } 143 144 Class c = __mgr__.findClass(__name__,name); 145 if (c != null) return addClass(name,c); 146 147 if (name == "__name__") return new PyString(__name__); 148 if (name == "__dict__") return __dict__; 149 if (name == "__mgr__") return Py.java2py(__mgr__); 150 if (name == "__file__") { 151 if (__file__ != null) return new PyString(__file__); 152 153 return Py.None; 154 } 155 156 return null; 157 } 158 159 public void __setattr__(String attr, PyObject value) { 160 if (attr == "__mgr__") { 161 PackageManager newMgr = (PackageManager)Py.tojava(value, 162 PackageManager.class); 163 if (newMgr == null) { 164 throw Py.TypeError("cannot set java package __mgr__ to None"); 165 } 166 __mgr__ = newMgr; 167 return; 168 } 169 if (attr == "__file__") { 170 __file__ = value.__str__().toString(); 171 return; 172 } 173 174 super.__setattr__(attr,value); 175 } 176 177 public String toString() { 178 return "<java package "+__name__+" "+Py.idstr(this)+">"; 179 } 180 181 182 185 public String safeRepr() throws PyIgnoreMethodTag { 186 return "java package '"+__name__+"'"; 187 } 188 189 } 190 | Popular Tags |