KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package org.python.core;
5
6 import java.util.*;
7 import java.io.File JavaDoc;
8
9 /**
10  * A representation of java package.
11  */

12
13 public class PyJavaPackage extends PyObject {
14     public String JavaDoc __name__;
15
16
17     public PyStringMap __dict__;
18     //public String _unparsedAll;
19
/** Its keys are the names of statically known classes.
20      * E.g. from jars pre-scan.
21      */

22     public PyStringMap clsSet;
23     public String JavaDoc __file__;
24     //public PyList __all__;
25

26     /** (Control) package manager whose hierarchy contains this java pkg.
27      */

28     public PackageManager __mgr__;
29
30     public PyJavaPackage(String JavaDoc name) {
31         this(name, null, null);
32     }
33
34     public PyJavaPackage(String JavaDoc name,String JavaDoc jarfile) {
35         this(name, null, jarfile);
36     }
37
38     public PyJavaPackage(String JavaDoc name,PackageManager mgr) {
39         this(name, mgr, null);
40     }
41
42
43     public PyJavaPackage(String JavaDoc name,PackageManager mgr,String JavaDoc jarfile) {
44         __file__ = jarfile;
45         __name__ = name;
46
47         if( mgr == null )
48            __mgr__ = PySystemState.packageManager; // default
49
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 JavaDoc name) {
59         return addPackage(name, null);
60     }
61
62     public PyJavaPackage addPackage(String JavaDoc name, String JavaDoc jarfile) {
63         int dot = name.indexOf('.');
64         String JavaDoc firstName=name;
65         String JavaDoc 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 JavaDoc pname = __name__.length() == 0 ?
74                            firstName : __name__+'.'+firstName;
75             p = new PyJavaPackage(pname, __mgr__, jarfile);
76             __dict__.__setitem__(firstName, p);
77         } else {
78             // this code is ok here, because this is not needed for
79
// a top level package
80
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 JavaDoc name,Class JavaDoc c) {
88         // xxx what to do with PyObject subclasses?
89
//PyObject ret = PyJavaClass.lookup(c); // xxx java2py?
90
// perhaps introduce class2py
91
PyObject ret = Py.java2py(c);
92         __dict__.__setitem__(name.intern(), ret);
93         return ret;
94     }
95
96     public PyObject addLazyClass(String JavaDoc name) {
97         // xxx what to do with PyObject subclasses? this now fails on them
98
PyObject ret = PyJavaClass.lookup(__name__+'.'+name,__mgr__);
99         __dict__.__setitem__(name.intern(), ret);
100         return ret;
101     }
102
103     /** Add statically known classes.
104      * @param classes their names as comma-separated string
105      */

106     public void addPlaceholders(String JavaDoc classes) {
107         StringTokenizer tok = new StringTokenizer(classes, ",@");
108         while (tok.hasMoreTokens()) {
109             String JavaDoc p = tok.nextToken();
110             String JavaDoc 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     /**
121      * Used for 'from xyz import *', dynamically dir pkg filling up __dict__.
122      * It uses {@link PackageManager#doDir} implementation furnished by
123      * the control package manager with instatiate true. The package
124      * manager should lazily load classes with {@link #addLazyClass} in
125      * the package.
126      *
127      * @return list of member names
128      */

129     public PyObject fillDir() {
130         return __mgr__.doDir(this,true,false);
131     }
132
133
134     public PyObject __findattr__(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc toString() {
178         return "<java package "+__name__+" "+Py.idstr(this)+">";
179     }
180     
181     
182     /**
183      * @see org.python.core.PyObject#safeRepr()
184      */

185     public String JavaDoc safeRepr() throws PyIgnoreMethodTag {
186         return "java package '"+__name__+"'";
187     }
188
189 }
190
Popular Tags