Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/apple/applescript/AppleScriptEngineFactory.java
38829 views
/*1* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package apple.applescript;2627import java.security.*;28import java.util.*;29import javax.script.*;3031public class AppleScriptEngineFactory implements ScriptEngineFactory {32private static volatile boolean initialized = false;3334private static native void initNative();3536static void TRACE(final String str) {37// System.out.println(AppleScriptEngineFactory.class.getName() + "." + str);38}3940/**41* The name of this ScriptEngine42*/43static final String ENGINE_NAME = "AppleScriptEngine";4445/**46* The version of this ScriptEngine47*/48static final String ENGINE_VERSION = "1.1";4950/**51* The name of this ScriptEngine (yes, again)52*/53static final String ENGINE_SHORT_NAME = ENGINE_NAME;5455/**56* The name of the language supported by this ScriptEngine57*/58static final String LANGUAGE = "AppleScript";5960static ScriptEngineFactory getFactory() {61TRACE("getFactory()");62return new AppleScriptEngineFactory();63}6465/**66* Initialize a new AppleScriptEngineFactory, replete with a member AppleScriptEngine67*/68public AppleScriptEngineFactory() {69TRACE("<ctor>()");70}7172/**73* Returns the full name of the ScriptEngine.74*75* @return full name of the ScriptEngine76*/77@Override78public String getEngineName() {79TRACE("getEngineName()");80return ENGINE_NAME;81}8283/**84* Returns the version of the ScriptEngine.85*86* @return version of the ScriptEngine87*/88@Override89public String getEngineVersion() {90TRACE("getEngineVersion()");91return ENGINE_VERSION;92}9394/**95* Returns the name of the scripting language supported by this ScriptEngine.96*97* @return name of the language supported by the ScriptEngine(Factory)98*/99@Override100public String getLanguageName() {101TRACE("getLanguageName()");102return LANGUAGE;103}104105/**106* Returns the version of the scripting language supported by this ScriptEngine(Factory).107*108* @return language version supported by the ScriptEngine(Factory)109*/110@Override111public String getLanguageVersion() {112TRACE("getLanguageVersion()");113return AccessController.doPrivileged(new PrivilegedAction<String>() {114public String run() {115final AppleScriptEngine engine = getScriptEngine();116return engine.getLanguageVersion();117}118});119}120121/**122* Returns an immutable list of filename extensions, which generally identify123* scripts written in the language supported by this ScriptEngine.124*125* @return ArrayList of file extensions AppleScript associates with126*/127@Override128public List<String> getExtensions() {129TRACE("getExtensions()");130return Arrays.asList("scpt", "applescript", "app");131}132133/**134* Returns an immutable list of mimetypes, associated with scripts135* that can be executed by the engine.136*137* @return ArrayList of mimetypes that AppleScript associates with138*/139@Override140public List<String> getMimeTypes() {141TRACE("getMimeTypes()");142return Arrays.asList("application/x-applescript", "text/plain", "text/applescript");143}144145/**146* Returns an immutable list of short names for the ScriptEngine,147* which may be used to identify the ScriptEngine by the ScriptEngineManager.148*149* @return150*/151@Override152public List<String> getNames() {153TRACE("getNames()");154return Arrays.asList("AppleScriptEngine", "AppleScript", "OSA");155}156157/**158* Returns a String which can be used to invoke a method of a Java159* object using the syntax of the supported scripting language.160*161* @param obj162* unused -- AppleScript does not support objects163* @param m164* function name165* @param args166* arguments to the function167* @return the AppleScript string calling the method168*/169@Override170public String getMethodCallSyntax(final String obj, final String fname, final String ... args) {171// StringBuilder builder = new StringBuilder();172// builder.append("my " + fname + "(");173// // TODO -- do174// builder.append(")\n");175// return builder.toString();176177return null;178}179180/**181* Returns a String that can be used as a statement to display the specified String using the syntax of the supported scripting language.182*183* @param toDisplay184* @return185*/186@Override187public String getOutputStatement(final String toDisplay) {188// TODO -- this might even be good enough? XD189return getMethodCallSyntax(null, "print", toDisplay);190}191192/**193* Returns the value of an attribute whose meaning may be implementation-specific.194*195* @param key196* the key to look up197* @return the static preseeded value for the key in the ScriptEngine, if it exists, otherwise <code>null</code>198*/199@Override200public Object getParameter(final String key) {201final AppleScriptEngine engine = getScriptEngine();202if (!engine.getBindings(ScriptContext.ENGINE_SCOPE).containsKey(key)) return null;203return engine.getBindings(ScriptContext.ENGINE_SCOPE).get(key);204}205206/**207* Returns A valid scripting language executable program with given statements.208*209* @param statements210* @return211*/212@Override213public String getProgram(final String ... statements) {214final StringBuilder program = new StringBuilder();215for (final String statement : statements) {216program.append(statement + "\n");217}218return program.toString();219}220221/**222* Returns an instance of the ScriptEngine associated with this ScriptEngineFactory.223*224* @return new AppleScriptEngine with this factory as it's parent225*/226@Override227public AppleScriptEngine getScriptEngine() {228AppleScriptEngine.checkSecurity();229ensureInitialized();230231return new AppleScriptEngine(this);232}233234private static synchronized void ensureInitialized() {235if (!initialized) {236initialized = true;237238java.awt.Toolkit.getDefaultToolkit();239System.loadLibrary("AppleScriptEngine");240initNative();241}242}243}244245246