001: // Copyright (c) Corporation for National Research Initiatives
002: // Copyright 2000 Samuele Pedroni
003:
004: package org.python.core;
005:
006: import java.util.StringTokenizer;
007:
008: /**
009: * A representation of java package.
010: */
011:
012: public class PyJavaPackage extends PyObject {
013: public String __name__;
014:
015: public PyStringMap __dict__;
016: //public String _unparsedAll;
017: /** Its keys are the names of statically known classes.
018: * E.g. from jars pre-scan.
019: */
020: public PyStringMap clsSet;
021: public String __file__;
022: //public PyList __all__;
023:
024: /** (Control) package manager whose hierarchy contains this java pkg.
025: */
026: public PackageManager __mgr__;
027:
028: public PyJavaPackage(String name) {
029: this (name, null, null);
030: }
031:
032: public PyJavaPackage(String name, String jarfile) {
033: this (name, null, jarfile);
034: }
035:
036: public PyJavaPackage(String name, PackageManager mgr) {
037: this (name, mgr, null);
038: }
039:
040: public PyJavaPackage(String name, PackageManager mgr, String jarfile) {
041: __file__ = jarfile;
042: __name__ = name;
043:
044: if (mgr == null)
045: __mgr__ = PySystemState.packageManager; // default
046: else
047: __mgr__ = mgr;
048:
049: clsSet = new PyStringMap();
050:
051: __dict__ = new PyStringMap();
052: __dict__.__setitem__("__name__", new PyString(__name__));
053: }
054:
055: public PyJavaPackage addPackage(String name) {
056: return addPackage(name, null);
057: }
058:
059: public PyJavaPackage addPackage(String name, String jarfile) {
060: int dot = name.indexOf('.');
061: String firstName = name;
062: String lastName = null;
063: if (dot != -1) {
064: firstName = name.substring(0, dot);
065: lastName = name.substring(dot + 1, name.length());
066: }
067: firstName = firstName.intern();
068: PyJavaPackage p = (PyJavaPackage) __dict__
069: .__finditem__(firstName);
070: if (p == null) {
071: String pname = __name__.length() == 0 ? firstName
072: : __name__ + '.' + firstName;
073: p = new PyJavaPackage(pname, __mgr__, jarfile);
074: __dict__.__setitem__(firstName, p);
075: } else {
076: // this code is ok here, because this is not needed for
077: // a top level package
078: if (jarfile == null || !jarfile.equals(p.__file__))
079: p.__file__ = null;
080: }
081: if (lastName != null)
082: return p.addPackage(lastName, jarfile);
083: else
084: return p;
085: }
086:
087: public PyObject addClass(String name, Class c) {
088: // xxx what to do with PyObject subclasses?
089: //PyObject ret = PyJavaClass.lookup(c); // xxx java2py?
090: // perhaps introduce class2py
091: PyObject ret = Py.java2py(c);
092: __dict__.__setitem__(name.intern(), ret);
093: return ret;
094: }
095:
096: public PyObject addLazyClass(String name) {
097: // xxx what to do with PyObject subclasses? this now fails on them
098: PyObject ret = PyJavaClass.lookup(__name__ + '.' + name,
099: __mgr__);
100: __dict__.__setitem__(name.intern(), ret);
101: return ret;
102: }
103:
104: /** Add statically known classes.
105: * @param classes their names as comma-separated string
106: */
107: public void addPlaceholders(String classes) {
108: StringTokenizer tok = new StringTokenizer(classes, ",@");
109: while (tok.hasMoreTokens()) {
110: String p = tok.nextToken();
111: String name = p.trim().intern();
112: if (clsSet.__finditem__(name) == null)
113: clsSet.__setitem__(name, Py.One);
114: }
115: }
116:
117: public PyObject __dir__() {
118: return __mgr__.doDir(this , false, false);
119: }
120:
121: /**
122: * Used for 'from xyz import *', dynamically dir pkg filling up __dict__.
123: * It uses {@link PackageManager#doDir} implementation furnished by
124: * the control package manager with instatiate true. The package
125: * manager should lazily load classes with {@link #addLazyClass} in
126: * the package.
127: *
128: * @return list of member names
129: */
130: public PyObject fillDir() {
131: return __mgr__.doDir(this , true, false);
132: }
133:
134: public PyObject __findattr__(String name) {
135:
136: PyObject ret = __dict__.__finditem__(name);
137: if (ret != null)
138: return ret;
139:
140: if (__mgr__.packageExists(__name__, name)) {
141: __mgr__.notifyPackageImport(__name__, name);
142: return addPackage(name);
143: }
144:
145: Class c = __mgr__.findClass(__name__, name);
146: if (c != null)
147: return addClass(name, c);
148:
149: if (name == "__name__")
150: return new PyString(__name__);
151: if (name == "__dict__")
152: return __dict__;
153: if (name == "__mgr__")
154: return Py.java2py(__mgr__);
155: if (name == "__file__") {
156: if (__file__ != null)
157: return new PyString(__file__);
158:
159: return Py.None;
160: }
161:
162: return null;
163: }
164:
165: public void __setattr__(String attr, PyObject value) {
166: if (attr == "__mgr__") {
167: PackageManager newMgr = (PackageManager) Py.tojava(value,
168: PackageManager.class);
169: if (newMgr == null) {
170: throw Py
171: .TypeError("cannot set java package __mgr__ to None");
172: }
173: __mgr__ = newMgr;
174: return;
175: }
176: if (attr == "__file__") {
177: __file__ = value.__str__().toString();
178: return;
179: }
180:
181: super .__setattr__(attr, value);
182: }
183:
184: public String toString() {
185: return "<java package " + __name__ + " " + Py.idstr(this ) + ">";
186: }
187:
188: /**
189: * @see org.python.core.PyObject#safeRepr()
190: */
191: public String safeRepr() throws PyIgnoreMethodTag {
192: return "java package '" + __name__ + "'";
193: }
194:
195: }
|