+++ /dev/null
-/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-\r
- *\r
- * The contents of this file are subject to the Netscape Public\r
- * License Version 1.1 (the "License"); you may not use this file\r
- * except in compliance with the License. You may obtain a copy of\r
- * the License at http://www.mozilla.org/NPL/\r
- *\r
- * Software distributed under the License is distributed on an "AS\r
- * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr\r
- * implied. See the License for the specific language governing\r
- * rights and limitations under the License.\r
- *\r
- * The Original Code is Rhino code, released\r
- * May 6, 1999.\r
- *\r
- * The Initial Developer of the Original Code is Netscape\r
- * Communications Corporation. Portions created by Netscape are\r
- * Copyright (C) 1999 Netscape Communications Corporation. All\r
- * Rights Reserved.\r
- *\r
- * Contributor(s): \r
- * Norris Boyd\r
- * Matthias Radestock\r
- *\r
- * Alternatively, the contents of this file may be used under the\r
- * terms of the GNU Public License (the "GPL"), in which case the\r
- * provisions of the GPL are applicable instead of those above.\r
- * If you wish to allow use of your version of this file only\r
- * under the terms of the GPL and not to allow others to use your\r
- * version of this file under the NPL, indicate your decision by\r
- * deleting the provisions above and replace them with the notice\r
- * and other provisions required by the GPL. If you do not delete\r
- * the provisions above, a recipient may use your version of this\r
- * file under either the NPL or the GPL.\r
- */\r
-\r
-// API class\r
-\r
-package org.mozilla.javascript;\r
-\r
-import java.util.Vector;\r
-\r
-/**\r
- * Class ImporterTopLevel\r
- * \r
- * This class defines a ScriptableObject that can be instantiated \r
- * as a top-level ("global") object to provide functionality similar\r
- * to Java's "import" statement.\r
- * <p>\r
- * This class can be used to create a top-level scope using the following code: \r
- * <pre>\r
- * Scriptable scope = new ImporterTopLevel(cx);\r
- * </pre>\r
- * Then JavaScript code will have access to the following methods:\r
- * <ul>\r
- * <li>importClass - will "import" a class by making its unqualified name \r
- * available as a property of the top-level scope\r
- * <li>importPackage - will "import" all the classes of the package by \r
- * searching for unqualified names as classes qualified\r
- * by the given package.\r
- * </ul>\r
- * The following code from the shell illustrates this use:\r
- * <pre>\r
- * js> importClass(java.io.File)\r
- * js> f = new File('help.txt')\r
- * help.txt\r
- * js> importPackage(java.util)\r
- * js> v = new Vector()\r
- * []\r
- * \r
- * @author Norris Boyd\r
- */\r
-public class ImporterTopLevel extends ScriptableObject {\r
- \r
- /**\r
- * @deprecated\r
- */\r
- public ImporterTopLevel() {\r
- init();\r
- }\r
-\r
- public ImporterTopLevel(Context cx) {\r
- cx.initStandardObjects(this);\r
- init();\r
- }\r
- \r
- private void init() {\r
- String[] names = { "importClass", "importPackage" };\r
-\r
- try {\r
- this.defineFunctionProperties(names, ImporterTopLevel.class,\r
- ScriptableObject.DONTENUM);\r
- } catch (PropertyException e) {\r
- throw new Error(); // should never happen\r
- }\r
- }\r
-\r
- public String getClassName() { \r
- return "global";\r
- }\r
- \r
- public Object get(String name, Scriptable start) {\r
- Object result = super.get(name, start);\r
- if (result != NOT_FOUND) \r
- return result;\r
- if (name.equals("_packages_")) \r
- return result;\r
- Object plist = ScriptableObject.getProperty(start,"_packages_");\r
- if (plist == NOT_FOUND) \r
- return result;\r
- Context cx = Context.enter();\r
- Object[] elements = cx.getElements((Scriptable)plist);\r
- Context.exit();\r
- for (int i=0; i < elements.length; i++) {\r
- NativeJavaPackage p = (NativeJavaPackage) elements[i];\r
- Object v = p.getPkgProperty(name, start, false);\r
- if (v != null && !(v instanceof NativeJavaPackage)) {\r
- if (result == NOT_FOUND) {\r
- result = v;\r
- } else {\r
- throw Context.reportRuntimeError2(\r
- "msg.ambig.import", result.toString(), v.toString());\r
- }\r
- }\r
- }\r
- return result;\r
- }\r
- \r
- public static void importClass(Context cx, Scriptable thisObj,\r
- Object[] args, Function funObj) {\r
- for (int i=0; i<args.length; i++) {\r
- Object cl = args[i];\r
- if (!(cl instanceof NativeJavaClass)) {\r
- throw Context.reportRuntimeError1(\r
- "msg.not.class", Context.toString(cl));\r
- }\r
- String s = ((NativeJavaClass) cl).getClassObject().getName();\r
- String n = s.substring(s.lastIndexOf('.')+1);\r
- Object val = thisObj.get(n, thisObj);\r
- if (val != NOT_FOUND && val != cl) {\r
- throw Context.reportRuntimeError1("msg.prop.defined", n);\r
- }\r
- //thisObj.defineProperty(n, cl, DONTENUM);\r
- thisObj.put(n,thisObj,cl);\r
- }\r
- }\r
- \r
- public static void importPackage(Context cx, Scriptable thisObj,\r
- Object[] args, Function funObj) {\r
- Scriptable importedPackages;\r
- Object plist = thisObj.get("_packages_", thisObj);\r
- if (plist == NOT_FOUND) {\r
- importedPackages = cx.newArray(thisObj,0);\r
- thisObj.put("_packages_", thisObj, importedPackages);\r
- }\r
- else {\r
- importedPackages = (Scriptable)plist;\r
- }\r
- for (int i=0; i<args.length; i++) {\r
- Object pkg = args[i];\r
- if (!(pkg instanceof NativeJavaPackage)) {\r
- throw Context.reportRuntimeError1(\r
- "msg.not.pkg", Context.toString(pkg));\r
- }\r
- Object[] elements = cx.getElements(importedPackages);\r
- for (int j=0; j < elements.length; j++) {\r
- if (pkg == elements[j]) {\r
- pkg = null;\r
- break;\r
- }\r
- }\r
- if (pkg != null)\r
- importedPackages.put(elements.length,importedPackages,pkg);\r
- }\r
- }\r
-}\r