001/* 002 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. 003 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 004 */ 005 006package netscape.javascript; 007 008 009// FIXME: need URL on java.sun.com for new LiveConnect spec 010 011/** 012 * <P> Allows Java code to manipulate JavaScript objects. </P> 013 * 014 * <P> When a JavaScript object is passed or returned to Java code, it 015 * is wrapped in an instance of <CODE>JSObject</CODE>. When a 016 * <CODE>JSObject</CODE> instance is passed to the JavaScript engine, 017 * it is unwrapped back to its original JavaScript object. The 018 * <CODE>JSObject</CODE> class provides a way to invoke JavaScript 019 * methods and examine JavaScript properties. </P> 020 * 021 * <P> Any data returned from the JavaScript engine to Java is 022 * converted to Java data types. Certain data passed to the JavaScript 023 * engine is converted to JavaScript data types. See the section on <A 024 * HREF="http://jdk6.java.net/plugin2/liveconnect/index.html#JAVA_JS_CONVERSIONS"> 025 * Data Type Conversions</A> in the <A 026 * HREF="http://jdk6.java.net/plugin2/liveconnect">LiveConnect Specification</A> 027 * for details on how values are converted. </P> 028 * 029 */ 030public abstract class JSObject { 031 032 /** 033 * Constructs a new JSObject. Users should not call this method 034 * nor subclass JSObject. 035 */ 036 protected JSObject() { 037 } 038 039 /** 040 * <p> Calls a JavaScript method. Equivalent to 041 * "this.methodName(args[0], args[1], ...)" in JavaScript. 042 * </p> 043 * 044 * @param methodName The name of the JavaScript method to be invoked. 045 * @param args An array of Java object to be passed as arguments to the method. 046 * @return Result of the method. 047 */ 048 public abstract Object call(String methodName, Object... args) throws JSException; 049 050 /** 051 * <p> Evaluates a JavaScript expression. The expression is a string of 052 * JavaScript source code which will be evaluated in the context given by 053 * "this". 054 * </p> 055 * 056 * @param s The JavaScript expression. 057 * @return Result of the JavaScript evaluation. 058 */ 059 public abstract Object eval(String s) throws JSException; 060 061 /** 062 * <p> Retrieves a named member of a JavaScript object. Equivalent to 063 * "this.name" in JavaScript. 064 * </p> 065 * 066 * @param name The name of the JavaScript property to be accessed. 067 * @return The value of the propery. 068 */ 069 public abstract Object getMember(String name) throws JSException; 070 071 /** 072 * <p> Sets a named member of a JavaScript object. Equivalent to 073 * "this.name = value" in JavaScript. 074 * </p> 075 * 076 * @param name The name of the JavaScript property to be accessed. 077 * @param value The value of the propery. 078 */ 079 public abstract void setMember(String name, Object value) throws JSException; 080 081 /** 082 * <p> Removes a named member of a JavaScript object. Equivalent 083 * to "delete this.name" in JavaScript. 084 * </p> 085 * 086 * @param name The name of the JavaScript property to be removed. 087 */ 088 public abstract void removeMember(String name) throws JSException; 089 090 /** 091 * <p> Retrieves an indexed member of a JavaScript object. Equivalent to 092 * "this[index]" in JavaScript. 093 * </p> 094 * 095 * @param index The index of the array to be accessed. 096 * @return The value of the indexed member. 097 */ 098 public abstract Object getSlot(int index) throws JSException; 099 100 /** 101 * <p> Sets an indexed member of a JavaScript object. Equivalent to 102 * "this[index] = value" in JavaScript. 103 * </p> 104 * 105 * @param index The index of the array to be accessed. 106 */ 107 public abstract void setSlot(int index, Object value) throws JSException; 108 109 /* * 110 * <p> Returns a JSObject for the window containing the given applet. 111 * </p> 112 * 113 * @param applet The applet. 114 * @return JSObject for the window containing the given applet. 115 * / 116 public static JSObject getWindow(Applet applet) throws JSException { 117 118 try 119 { 120 if (applet != null) 121 { 122 123 String obj = (String) applet.getParameter("MAYSCRIPT"); 124 125 // Comment out MAYSCRIPT check because Internet Explorer doesn't support 126 // it. 127// if (obj != null && (obj.equals("") || (new Boolean(obj).booleanValue() == true))) 128 { 129 // MAYSCRIPT is enabled 130 131 AppletContext c = applet.getAppletContext(); 132 133 // The applet context must implement the sun.plugin.javascript.JSContext 134 // in order for us to get the handle that can be used when evaluating 135 // JavaScript expression. 136 // 137 JSObject ret = null; 138 139 if (c instanceof sun.plugin.javascript.JSContext) 140 { 141 JSContext j = (JSContext) c; 142 ret = j.getJSObject(); 143 } 144 145 if (ret != null) { 146 return ret; 147 } 148 } 149 } else { 150 // new code for CustomProgress to get the JSObject w/o applet 151 AppContext ac = ToolkitStore.get().getAppContext(); 152 if (ac != null) { 153 Object context = ac.get(sun.plugin2.applet.Plugin2Manager.APPCONTEXT_PLUGIN2HOST_KEY); 154 if (context != null && (context instanceof JSContext)) { 155 JSContext jsc = (JSContext) context; 156 JSObject ret = jsc.getOneWayJSObject(); 157 if (ret != null) { 158 return ret; 159 } 160 } 161 } 162 } 163 } 164 catch (Throwable e) 165 { 166 throw (JSException) new JSException(JSException.EXCEPTION_TYPE_ERROR, e).initCause(e); 167 } 168 169 throw new JSException(); 170 } 171 */ 172}