4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/Apackage apple.applescript;
4632N/A
4632N/Aimport java.security.*;
4632N/Aimport java.util.*;
4632N/Aimport javax.script.*;
4632N/A
4632N/Apublic class AppleScriptEngineFactory implements ScriptEngineFactory {
4632N/A private static native void initNative();
4632N/A
4632N/A static {
4632N/A java.awt.Toolkit.getDefaultToolkit();
4632N/A System.loadLibrary("AppleScriptEngine");
4632N/A initNative();
4632N/A TRACE("<static-init>");
4632N/A }
4632N/A
4632N/A static void TRACE(final String str) {
4632N/A// System.out.println(AppleScriptEngineFactory.class.getName() + "." + str);
4632N/A }
4632N/A
4632N/A /**
4632N/A * The name of this ScriptEngine
4632N/A */
4632N/A static final String ENGINE_NAME = "AppleScriptEngine";
4632N/A
4632N/A /**
4632N/A * The version of this ScriptEngine
4632N/A */
4632N/A static final String ENGINE_VERSION = "1.1";
4632N/A
4632N/A /**
4632N/A * The name of this ScriptEngine (yes, again)
4632N/A */
4632N/A static final String ENGINE_SHORT_NAME = ENGINE_NAME;
4632N/A
4632N/A /**
4632N/A * The name of the language supported by this ScriptEngine
4632N/A */
4632N/A static final String LANGUAGE = "AppleScript";
4632N/A
4632N/A static ScriptEngineFactory getFactory() {
4632N/A TRACE("getFactory()");
4632N/A return new AppleScriptEngineFactory();
4632N/A }
4632N/A
4632N/A /**
4632N/A * Initialize a new AppleScriptEngineFactory, replete with a member AppleScriptEngine
4632N/A */
4632N/A public AppleScriptEngineFactory() {
4632N/A TRACE("<ctor>()");
4632N/A }
4632N/A
4632N/A /**
4632N/A * Returns the full name of the ScriptEngine.
4632N/A *
4632N/A * @return full name of the ScriptEngine
4632N/A */
4632N/A public String getEngineName() {
4632N/A TRACE("getEngineName()");
4632N/A return ENGINE_NAME;
4632N/A }
4632N/A
4632N/A /**
4632N/A * Returns the version of the ScriptEngine.
4632N/A *
4632N/A * @return version of the ScriptEngine
4632N/A */
4632N/A public String getEngineVersion() {
4632N/A TRACE("getEngineVersion()");
4632N/A return ENGINE_VERSION;
4632N/A }
4632N/A
4632N/A /**
4632N/A * Returns the name of the scripting language supported by this ScriptEngine.
4632N/A *
4632N/A * @return name of the language supported by the ScriptEngine(Factory)
4632N/A */
4632N/A public String getLanguageName() {
4632N/A TRACE("getLanguageName()");
4632N/A return LANGUAGE;
4632N/A }
4632N/A
4632N/A /**
4632N/A * Returns the version of the scripting language supported by this ScriptEngine(Factory).
4632N/A *
4632N/A * @return language version supported by the ScriptEngine(Factory)
4632N/A */
4632N/A public String getLanguageVersion() {
4632N/A TRACE("getLanguageVersion()");
4632N/A return AccessController.doPrivileged(new PrivilegedAction<String>() {
4632N/A public String run() {
4632N/A final AppleScriptEngine engine = new AppleScriptEngine(AppleScriptEngineFactory.this);
4632N/A return engine.getLanguageVersion();
4632N/A }
4632N/A });
4632N/A }
4632N/A
4632N/A /**
4632N/A * Returns an immutable list of filename extensions, which generally identify
4632N/A * scripts written in the language supported by this ScriptEngine.
4632N/A *
4632N/A * @return ArrayList of file extensions AppleScript associates with
4632N/A */
4632N/A public List<String> getExtensions() {
4632N/A TRACE("getExtensions()");
4632N/A return Arrays.asList("scpt", "applescript", "app");
4632N/A }
4632N/A
4632N/A /**
4632N/A * Returns an immutable list of mimetypes, associated with scripts
4632N/A * that can be executed by the engine.
4632N/A *
4632N/A * @return ArrayList of mimetypes that AppleScript associates with
4632N/A */
4632N/A public List<String> getMimeTypes() {
4632N/A TRACE("getMimeTypes()");
4632N/A return Arrays.asList("application/x-applescript", "text/plain", "text/applescript");
4632N/A }
4632N/A
4632N/A /**
4632N/A * Returns an immutable list of short names for the ScriptEngine,
4632N/A * which may be used to identify the ScriptEngine by the ScriptEngineManager.
4632N/A *
4632N/A * @return
4632N/A */
4632N/A public List<String> getNames() {
4632N/A TRACE("getNames()");
4632N/A return Arrays.asList("AppleScriptEngine", "AppleScript", "OSA");
4632N/A }
4632N/A
4632N/A /**
4632N/A * Returns a String which can be used to invoke a method of a Java
4632N/A * object using the syntax of the supported scripting language.
4632N/A *
4632N/A * @param obj
4632N/A * unused -- AppleScript does not support objects
4632N/A * @param m
4632N/A * function name
4632N/A * @param args
4632N/A * arguments to the function
4632N/A * @return the AppleScript string calling the method
4632N/A */
4632N/A public String getMethodCallSyntax(final String obj, final String fname, final String ... args) {
4632N/A// StringBuilder builder = new StringBuilder();
4632N/A// builder.append("my " + fname + "(");
4632N/A// // TODO -- do
4632N/A// builder.append(")\n");
4632N/A// return builder.toString();
4632N/A
4632N/A return null;
4632N/A }
4632N/A
4632N/A /**
4632N/A * Returns a String that can be used as a statement to display the specified String using the syntax of the supported scripting language.
4632N/A *
4632N/A * @param toDisplay
4632N/A * @return
4632N/A */
4632N/A public String getOutputStatement(final String toDisplay) {
4632N/A // TODO -- this might even be good enough? XD
4632N/A return getMethodCallSyntax(null, "print", toDisplay);
4632N/A }
4632N/A
4632N/A /**
4632N/A * Returns the value of an attribute whose meaning may be implementation-specific.
4632N/A *
4632N/A * @param key
4632N/A * the key to look up
4632N/A * @return the static preseeded value for the key in the ScriptEngine, if it exists, otherwise <code>null</code>
4632N/A */
4632N/A public Object getParameter(final String key) {
4632N/A final AppleScriptEngine engine = new AppleScriptEngine(this);
4632N/A if (!engine.getBindings(ScriptContext.ENGINE_SCOPE).containsKey(key)) return null;
4632N/A return engine.getBindings(ScriptContext.ENGINE_SCOPE).get(key);
4632N/A }
4632N/A
4632N/A /**
4632N/A * Returns A valid scripting language executable program with given statements.
4632N/A *
4632N/A * @param statements
4632N/A * @return
4632N/A */
4632N/A public String getProgram(final String ... statements) {
4632N/A final StringBuilder program = new StringBuilder();
4632N/A for (final String statement : statements) {
4632N/A program.append(statement + "\n");
4632N/A }
4632N/A return program.toString();
4632N/A }
4632N/A
4632N/A /**
4632N/A * Returns an instance of the ScriptEngine associated with this ScriptEngineFactory.
4632N/A *
4632N/A * @return new AppleScriptEngine with this factory as it's parent
4632N/A */
4632N/A public ScriptEngine getScriptEngine() {
4632N/A AppleScriptEngine.checkSecurity();
4632N/A return new AppleScriptEngine(this);
4632N/A }
4632N/A}