J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package javax.script; |
| 27 | |
| 28 | import java.io.Reader; |
| 29 | import java.util.Map; |
| 30 | import java.util.Set; |
| 31 | |
| 32 | /** |
| 33 | * <code>ScriptEngine</code> is the fundamental interface whose methods must be |
| 34 | * fully functional in every implementation of this specification. |
| 35 | * <br><br> |
| 36 | * These methods provide basic scripting functionality. Applications written to this |
| 37 | * simple interface are expected to work with minimal modifications in every implementation. |
| 38 | * It includes methods that execute scripts, and ones that set and get values. |
| 39 | * <br><br> |
| 40 | * The values are key/value pairs of two types. The first type of pairs consists of |
| 41 | * those whose keys are reserved and defined in this specification or by individual |
| 42 | * implementations. The values in the pairs with reserved keys have specified meanings. |
| 43 | * <br><br> |
| 44 | * The other type of pairs consists of those that create Java language Bindings, the values are |
| 45 | * usually represented in scripts by the corresponding keys or by decorated forms of them. |
| 46 | * |
| 47 | * @author Mike Grogan |
| 48 | * @since 1.6 |
| 49 | */ |
| 50 | |
| 51 | public interface ScriptEngine { |
| 52 | |
| 53 | /** |
| 54 | * Reserved key for a named value that passes |
| 55 | * an array of positional arguments to a script. |
| 56 | */ |
| 57 | public static final String ARGV="javax.script.argv"; |
| 58 | |
| 59 | /** |
| 60 | * Reserved key for a named value that is |
| 61 | * the name of the file being executed. |
| 62 | */ |
| 63 | public static final String FILENAME = "javax.script.filename"; |
| 64 | |
| 65 | /** |
| 66 | * Reserved key for a named value that is |
| 67 | * the name of the <code>ScriptEngine</code> implementation. |
| 68 | */ |
| 69 | public static final String ENGINE = "javax.script.engine"; |
| 70 | |
| 71 | /** |
| 72 | * Reserved key for a named value that identifies |
| 73 | * the version of the <code>ScriptEngine</code> implementation. |
| 74 | */ |
| 75 | public static final String ENGINE_VERSION = "javax.script.engine_version"; |
| 76 | |
| 77 | /** |
| 78 | * Reserved key for a named value that identifies |
| 79 | * the short name of the scripting language. The name is used by the |
| 80 | * <code>ScriptEngineManager</code> to locate a <code>ScriptEngine</code> |
| 81 | * with a given name in the <code>getEngineByName</code> method. |
| 82 | */ |
| 83 | public static final String NAME = "javax.script.name"; |
| 84 | |
| 85 | /** |
| 86 | * Reserved key for a named value that is |
| 87 | * the full name of Scripting Language supported by the implementation. |
| 88 | */ |
| 89 | public static final String LANGUAGE = "javax.script.language"; |
| 90 | |
| 91 | /** |
| 92 | * Reserved key for the named value that identifies |
| 93 | * the version of the scripting language supported by the implementation. |
| 94 | */ |
| 95 | public static final String LANGUAGE_VERSION ="javax.script.language_version"; |
| 96 | |
| 97 | |
| 98 | /** |
| 99 | * Causes the immediate execution of the script whose source is the String |
| 100 | * passed as the first argument. The script may be reparsed or recompiled before |
| 101 | * execution. State left in the engine from previous executions, including |
| 102 | * variable values and compiled procedures may be visible during this execution. |
| 103 | * |
| 104 | * @param script The script to be executed by the script engine. |
| 105 | * |
| 106 | * @param context A <code>ScriptContext</code> exposing sets of attributes in |
| 107 | * different scopes. The meanings of the scopes <code>ScriptContext.GLOBAL_SCOPE</code>, |
| 108 | * and <code>ScriptContext.ENGINE_SCOPE</code> are defined in the specification. |
| 109 | * <br><br> |
| 110 | * The <code>ENGINE_SCOPE</code> <code>Bindings</code> of the <code>ScriptContext</code> contains the |
| 111 | * bindings of scripting variables to application objects to be used during this |
| 112 | * script execution. |
| 113 | * |
| 114 | * |
| 115 | * @return The value returned from the execution of the script. |
| 116 | * |
| 117 | * @throws ScriptException if an error occurrs in script. ScriptEngines should create and throw |
| 118 | * <code>ScriptException</code> wrappers for checked Exceptions thrown by underlying scripting |
| 119 | * implementations. |
| 120 | * @throws NullPointerException if either argument is null. |
| 121 | */ |
| 122 | public Object eval(String script, ScriptContext context) throws ScriptException; |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * Same as <code>eval(String, ScriptContext)</code> where the source of the script |
| 127 | * is read from a <code>Reader</code>. |
| 128 | * |
| 129 | * @param reader The source of the script to be executed by the script engine. |
| 130 | * |
| 131 | * @param context The <code>ScriptContext</code> passed to the script engine. |
| 132 | * |
| 133 | * @return The value returned from the execution of the script. |
| 134 | * |
| 135 | * @throws ScriptException if an error occurrs in script. |
| 136 | * @throws NullPointerException if either argument is null. |
| 137 | */ |
| 138 | public Object eval(Reader reader , ScriptContext context) throws ScriptException; |
| 139 | |
| 140 | /** |
| 141 | * Executes the specified script. The default <code>ScriptContext</code> for the <code>ScriptEngine</code> |
| 142 | * is used. |
| 143 | * |
| 144 | * @param script The script language source to be executed. |
| 145 | * |
| 146 | * @return The value returned from the execution of the script. |
| 147 | * |
| 148 | * @throws ScriptException if error occurrs in script. |
| 149 | * @throws NullPointerException if the argument is null. |
| 150 | */ |
| 151 | public Object eval(String script) throws ScriptException; |
| 152 | |
| 153 | /** |
| 154 | * Same as <code>eval(String)</code> except that the source of the script is |
| 155 | * provided as a <code>Reader</code> |
| 156 | * |
| 157 | * @param reader The source of the script. |
| 158 | * |
| 159 | * @return The value returned by the script. |
| 160 | * |
| 161 | * @throws ScriptException if an error occurrs in script. |
| 162 | * @throws NullPointerException if the argument is null. |
| 163 | */ |
| 164 | public Object eval(Reader reader) throws ScriptException; |
| 165 | |
| 166 | /** |
| 167 | * Executes the script using the <code>Bindings</code> argument as the <code>ENGINE_SCOPE</code> |
| 168 | * <code>Bindings</code> of the <code>ScriptEngine</code> during the script execution. The |
| 169 | * <code>Reader</code>, <code>Writer</code> and non-<code>ENGINE_SCOPE</code> <code>Bindings</code> of the |
| 170 | * default <code>ScriptContext</code> are used. The <code>ENGINE_SCOPE</code> |
| 171 | * <code>Bindings</code> of the <code>ScriptEngine</code> is not changed, and its |
| 172 | * mappings are unaltered by the script execution. |
| 173 | * |
| 174 | * @param script The source for the script. |
| 175 | * |
| 176 | * @param n The <code>Bindings</code> of attributes to be used for script execution. |
| 177 | * |
| 178 | * @return The value returned by the script. |
| 179 | * |
| 180 | * @throws ScriptException if an error occurrs in script. |
| 181 | * @throws NullPointerException if either argument is null. |
| 182 | */ |
| 183 | public Object eval(String script, Bindings n) throws ScriptException; |
| 184 | |
| 185 | /** |
| 186 | * Same as <code>eval(String, Bindings)</code> except that the source of the script |
| 187 | * is provided as a <code>Reader</code>. |
| 188 | * |
| 189 | * @param reader The source of the script. |
| 190 | * @param n The <code>Bindings</code> of attributes. |
| 191 | * |
| 192 | * @return The value returned by the script. |
| 193 | * |
| 194 | * @throws ScriptException if an error occurrs. |
| 195 | * @throws NullPointerException if either argument is null. |
| 196 | */ |
| 197 | public Object eval(Reader reader , Bindings n) throws ScriptException; |
| 198 | |
| 199 | |
| 200 | |
| 201 | /** |
| 202 | * Sets a key/value pair in the state of the ScriptEngine that may either create |
| 203 | * a Java Language Binding to be used in the execution of scripts or be used in some |
| 204 | * other way, depending on whether the key is reserved. Must have the same effect as |
| 205 | * <code>getBindings(ScriptContext.ENGINE_SCOPE).put</code>. |
| 206 | * |
| 207 | * @param key The name of named value to add |
| 208 | * @param value The value of named value to add. |
| 209 | * |
| 210 | * @throws NullPointerException if key is null. |
| 211 | * @throws IllegalArgumentException if key is empty. |
| 212 | */ |
| 213 | public void put(String key, Object value); |
| 214 | |
| 215 | |
| 216 | /** |
| 217 | * Retrieves a value set in the state of this engine. The value might be one |
| 218 | * which was set using <code>setValue</code> or some other value in the state |
| 219 | * of the <code>ScriptEngine</code>, depending on the implementation. Must have the same effect |
| 220 | * as <code>getBindings(ScriptContext.ENGINE_SCOPE).get</code> |
| 221 | * |
| 222 | * @param key The key whose value is to be returned |
| 223 | * @return the value for the given key |
| 224 | * |
| 225 | * @throws NullPointerException if key is null. |
| 226 | * @throws IllegalArgumentException if key is empty. |
| 227 | */ |
| 228 | public Object get(String key); |
| 229 | |
| 230 | |
| 231 | /** |
| 232 | * Returns a scope of named values. The possible scopes are: |
| 233 | * <br><br> |
| 234 | * <ul> |
| 235 | * <li><code>ScriptContext.GLOBAL_SCOPE</code> - The set of named values representing global |
| 236 | * scope. If this <code>ScriptEngine</code> is created by a <code>ScriptEngineManager</code>, |
| 237 | * then the manager sets global scope bindings. This may be <code>null</code> if no global |
| 238 | * scope is associated with this <code>ScriptEngine</code></li> |
| 239 | * <li><code>ScriptContext.ENGINE_SCOPE</code> - The set of named values representing the state of |
| 240 | * this <code>ScriptEngine</code>. The values are generally visible in scripts using |
| 241 | * the associated keys as variable names.</li> |
| 242 | * <li>Any other value of scope defined in the default <code>ScriptContext</code> of the <code>ScriptEngine</code>. |
| 243 | * </li> |
| 244 | * </ul> |
| 245 | * <br><br> |
| 246 | * The <code>Bindings</code> instances that are returned must be identical to those returned by the |
| 247 | * <code>getBindings</code> method of <code>ScriptContext</code> called with corresponding arguments on |
| 248 | * the default <code>ScriptContext</code> of the <code>ScriptEngine</code>. |
| 249 | * |
| 250 | * @param scope Either <code>ScriptContext.ENGINE_SCOPE</code> or <code>ScriptContext.GLOBAL_SCOPE</code> |
| 251 | * which specifies the <code>Bindings</code> to return. Implementations of <code>ScriptContext</code> |
| 252 | * may define additional scopes. If the default <code>ScriptContext</code> of the <code>ScriptEngine</code> |
| 253 | * defines additional scopes, any of them can be passed to get the corresponding <code>Bindings</code>. |
| 254 | * |
| 255 | * @return The <code>Bindings</code> with the specified scope. |
| 256 | * |
| 257 | * @throws IllegalArgumentException if specified scope is invalid |
| 258 | * |
| 259 | */ |
| 260 | public Bindings getBindings(int scope); |
| 261 | |
| 262 | /** |
| 263 | * Sets a scope of named values to be used by scripts. The possible scopes are: |
| 264 | *<br><br> |
| 265 | * <ul> |
| 266 | * <li><code>ScriptContext.ENGINE_SCOPE</code> - The specified <code>Bindings</code> replaces the |
| 267 | * engine scope of the <code>ScriptEngine</code>. |
| 268 | * </li> |
| 269 | * <li><code>ScriptContext.GLOBAL_SCOPE</code> - The specified <code>Bindings</code> must be visible |
| 270 | * as the <code>GLOBAL_SCOPE</code>. |
| 271 | * </li> |
| 272 | * <li>Any other value of scope defined in the default <code>ScriptContext</code> of the <code>ScriptEngine</code>. |
| 273 | *</li> |
| 274 | * </ul> |
| 275 | * <br><br> |
| 276 | * The method must have the same effect as calling the <code>setBindings</code> method of |
| 277 | * <code>ScriptContext</code> with the corresponding value of <code>scope</code> on the default |
| 278 | * <code>ScriptContext</code> of the <code>ScriptEngine</code>. |
| 279 | * |
| 280 | * @param bindings The <code>Bindings</code> for the specified scope. |
| 281 | * @param scope The specified scope. Either <code>ScriptContext.ENGINE_SCOPE</code>, |
| 282 | * <code>ScriptContext.GLOBAL_SCOPE</code>, or any other valid value of scope. |
| 283 | * |
| 284 | * @throws IllegalArgumentException if the scope is invalid |
| 285 | * @throws NullPointerException if the bindings is null and the scope is |
| 286 | * <code>ScriptContext.ENGINE_SCOPE</code> |
| 287 | */ |
| 288 | public void setBindings(Bindings bindings, int scope); |
| 289 | |
| 290 | |
| 291 | /** |
| 292 | * Returns an uninitialized <code>Bindings</code>. |
| 293 | * |
| 294 | * @return A <code>Bindings</code> that can be used to replace the state of this <code>ScriptEngine</code>. |
| 295 | **/ |
| 296 | public Bindings createBindings(); |
| 297 | |
| 298 | |
| 299 | /** |
| 300 | * Returns the default <code>ScriptContext</code> of the <code>ScriptEngine</code> whose Bindings, Reader |
| 301 | * and Writers are used for script executions when no <code>ScriptContext</code> is specified. |
| 302 | * |
| 303 | * @return The default <code>ScriptContext</code> of the <code>ScriptEngine</code>. |
| 304 | */ |
| 305 | public ScriptContext getContext(); |
| 306 | |
| 307 | /** |
| 308 | * Sets the default <code>ScriptContext</code> of the <code>ScriptEngine</code> whose Bindings, Reader |
| 309 | * and Writers are used for script executions when no <code>ScriptContext</code> is specified. |
| 310 | * |
| 311 | * @param context A <code>ScriptContext</code> that will replace the default <code>ScriptContext</code> in |
| 312 | * the <code>ScriptEngine</code>. |
| 313 | * @throws NullPointerException if context is null. |
| 314 | */ |
| 315 | public void setContext(ScriptContext context); |
| 316 | |
| 317 | /** |
| 318 | * Returns a <code>ScriptEngineFactory</code> for the class to which this <code>ScriptEngine</code> belongs. |
| 319 | * |
| 320 | * @return The <code>ScriptEngineFactory</code> |
| 321 | */ |
| 322 | public ScriptEngineFactory getFactory(); |
| 323 | } |