2002/08/07 05:08:49
[org.ibex.core.git] / src / org / xwt / Static.java
1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt;
3
4 import org.xwt.util.*;
5 import org.mozilla.javascript.*;
6
7 /** implements objects in the xwt.static.* namespace */
8 public class Static extends JSObject {
9
10     public static Static getStatic(String resourcename) {
11         Template t = Template.getTemplate(resourcename, null);
12         if (t != null) t.link();
13         return (Static)cache.get(resourcename);
14     }
15
16     public static Static createStatic(String resourcename, boolean isPackage) {
17         Static ret = (Static)cache.get(resourcename);
18         if (ret != null) return ret;
19         if (resourcename.indexOf('.') != -1)
20             createStatic(resourcename.substring(0, resourcename.lastIndexOf('.')), true);
21         ret = new Static(resourcename, false);
22         cache.put(resourcename, ret);
23         return ret;
24     }
25
26     private static Hash cache = new Hash();
27
28     /** the resource name that this Static object corresponds to */
29     private String resourcename = null;
30
31     /** true iff this represents a directory (rather than an actual xwt) */
32     public boolean ispackage = false;
33
34     private Static(String resourcename, boolean ispackage) {
35         super(true);
36         cache.put(resourcename, this);
37         this.resourcename = resourcename;
38         this.ispackage = ispackage;
39         setSeal(ispackage);
40     }
41     
42     /** creates a new static representing a package */
43     public Static(String resourcename) { this(resourcename, true); }
44
45     public Object get(String name, Scriptable start) {
46         if (name == null) return null;
47
48         // hack since Rhino needs to be able to grab these functions to create new objects
49         if (name.equals("Object")) return JSObject.defaultObjects.get("Object", null);
50         if (name.equals("Array")) return JSObject.defaultObjects.get("Array", null);
51         if (name.equals("Function")) return JSObject.defaultObjects.get("Function", null);
52         if (name.equals("TypeError")) return JSObject.defaultObjects.get("TypeError", null);
53
54         if ("xwt".equals(name))
55             for(Scriptable cur = Context.enter().currentFunction; cur != null; cur = cur.getParentScope())
56                 if (cur == this) return XWT.singleton;
57
58         if (!ispackage) return super.get(name, start);
59         return getStatic(resourcename + (resourcename.length() == 0 ? "" : ".") + name);
60     }
61
62     static { createStatic("", true); }
63
64 }