1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
\r
3 * The contents of this file are subject to the Netscape Public
\r
4 * License Version 1.1 (the "License"); you may not use this file
\r
5 * except in compliance with the License. You may obtain a copy of
\r
6 * the License at http://www.mozilla.org/NPL/
\r
8 * Software distributed under the License is distributed on an "AS
\r
9 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
\r
10 * implied. See the License for the specific language governing
\r
11 * rights and limitations under the License.
\r
13 * The Original Code is Rhino code, released
\r
16 * The Initial Developer of the Original Code is Netscape
\r
17 * Communications Corporation. Portions created by Netscape are
\r
18 * Copyright (C) 1999 Netscape Communications Corporation. All
\r
23 * Matthias Radestock
\r
25 * Alternatively, the contents of this file may be used under the
\r
26 * terms of the GNU Public License (the "GPL"), in which case the
\r
27 * provisions of the GPL are applicable instead of those above.
\r
28 * If you wish to allow use of your version of this file only
\r
29 * under the terms of the GPL and not to allow others to use your
\r
30 * version of this file under the NPL, indicate your decision by
\r
31 * deleting the provisions above and replace them with the notice
\r
32 * and other provisions required by the GPL. If you do not delete
\r
33 * the provisions above, a recipient may use your version of this
\r
34 * file under either the NPL or the GPL.
\r
39 package org.mozilla.javascript;
\r
41 import java.util.Vector;
\r
44 * Class ImporterTopLevel
\r
46 * This class defines a ScriptableObject that can be instantiated
\r
47 * as a top-level ("global") object to provide functionality similar
\r
48 * to Java's "import" statement.
\r
50 * This class can be used to create a top-level scope using the following code:
\r
52 * Scriptable scope = new ImporterTopLevel(cx);
\r
54 * Then JavaScript code will have access to the following methods:
\r
56 * <li>importClass - will "import" a class by making its unqualified name
\r
57 * available as a property of the top-level scope
\r
58 * <li>importPackage - will "import" all the classes of the package by
\r
59 * searching for unqualified names as classes qualified
\r
60 * by the given package.
\r
62 * The following code from the shell illustrates this use:
\r
64 * js> importClass(java.io.File)
\r
65 * js> f = new File('help.txt')
\r
67 * js> importPackage(java.util)
\r
68 * js> v = new Vector()
\r
71 * @author Norris Boyd
\r
73 public class ImporterTopLevel extends ScriptableObject {
\r
78 public ImporterTopLevel() {
\r
82 public ImporterTopLevel(Context cx) {
\r
83 cx.initStandardObjects(this);
\r
87 private void init() {
\r
88 String[] names = { "importClass", "importPackage" };
\r
91 this.defineFunctionProperties(names, ImporterTopLevel.class,
\r
92 ScriptableObject.DONTENUM);
\r
93 } catch (PropertyException e) {
\r
94 throw new Error(); // should never happen
\r
98 public String getClassName() {
\r
102 public Object get(String name, Scriptable start) {
\r
103 Object result = super.get(name, start);
\r
104 if (result != NOT_FOUND)
\r
106 if (name.equals("_packages_"))
\r
108 Object plist = ScriptableObject.getProperty(start,"_packages_");
\r
109 if (plist == NOT_FOUND)
\r
111 Context cx = Context.enter();
\r
112 Object[] elements = cx.getElements((Scriptable)plist);
\r
114 for (int i=0; i < elements.length; i++) {
\r
115 NativeJavaPackage p = (NativeJavaPackage) elements[i];
\r
116 Object v = p.getPkgProperty(name, start, false);
\r
117 if (v != null && !(v instanceof NativeJavaPackage)) {
\r
118 if (result == NOT_FOUND) {
\r
121 throw Context.reportRuntimeError2(
\r
122 "msg.ambig.import", result.toString(), v.toString());
\r
129 public static void importClass(Context cx, Scriptable thisObj,
\r
130 Object[] args, Function funObj) {
\r
131 for (int i=0; i<args.length; i++) {
\r
132 Object cl = args[i];
\r
133 if (!(cl instanceof NativeJavaClass)) {
\r
134 throw Context.reportRuntimeError1(
\r
135 "msg.not.class", Context.toString(cl));
\r
137 String s = ((NativeJavaClass) cl).getClassObject().getName();
\r
138 String n = s.substring(s.lastIndexOf('.')+1);
\r
139 Object val = thisObj.get(n, thisObj);
\r
140 if (val != NOT_FOUND && val != cl) {
\r
141 throw Context.reportRuntimeError1("msg.prop.defined", n);
\r
143 //thisObj.defineProperty(n, cl, DONTENUM);
\r
144 thisObj.put(n,thisObj,cl);
\r
148 public static void importPackage(Context cx, Scriptable thisObj,
\r
149 Object[] args, Function funObj) {
\r
150 Scriptable importedPackages;
\r
151 Object plist = thisObj.get("_packages_", thisObj);
\r
152 if (plist == NOT_FOUND) {
\r
153 importedPackages = cx.newArray(thisObj,0);
\r
154 thisObj.put("_packages_", thisObj, importedPackages);
\r
157 importedPackages = (Scriptable)plist;
\r
159 for (int i=0; i<args.length; i++) {
\r
160 Object pkg = args[i];
\r
161 if (!(pkg instanceof NativeJavaPackage)) {
\r
162 throw Context.reportRuntimeError1(
\r
163 "msg.not.pkg", Context.toString(pkg));
\r
165 Object[] elements = cx.getElements(importedPackages);
\r
166 for (int j=0; j < elements.length; j++) {
\r
167 if (pkg == elements[j]) {
\r
173 importedPackages.put(elements.length,importedPackages,pkg);
\r