1 /*
   2  * Copyright (c) 2011, 2012, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package apple.applescript;
  27 
  28 import java.security.*;
  29 import java.util.*;
  30 import javax.script.*;
  31 
  32 public class AppleScriptEngineFactory implements ScriptEngineFactory {
  33     private static native void initNative();
  34 
  35     static {
  36         java.awt.Toolkit.getDefaultToolkit();
  37         System.loadLibrary("AppleScriptEngine");
  38         initNative();
  39         TRACE("<static-init>");
  40     }
  41 
  42     static void TRACE(final String str) {
  43 //        System.out.println(AppleScriptEngineFactory.class.getName() + "." + str);
  44     }
  45 
  46     /**
  47      * The name of this ScriptEngine
  48      */
  49     static final String ENGINE_NAME = "AppleScriptEngine";
  50 
  51     /**
  52      * The version of this ScriptEngine
  53      */
  54     static final String ENGINE_VERSION = "1.1";
  55 
  56     /**
  57      * The name of this ScriptEngine (yes, again)
  58      */
  59     static final String ENGINE_SHORT_NAME = ENGINE_NAME;
  60 
  61     /**
  62      * The name of the language supported by this ScriptEngine
  63      */
  64     static final String LANGUAGE = "AppleScript";
  65 
  66     static ScriptEngineFactory getFactory() {
  67         TRACE("getFactory()");
  68         return new AppleScriptEngineFactory();
  69     }
  70 
  71     /**
  72      * Initialize a new AppleScriptEngineFactory, replete with a member AppleScriptEngine
  73      */
  74     public AppleScriptEngineFactory() {
  75         TRACE("<ctor>()");
  76     }
  77 
  78     /**
  79      * Returns the full name of the ScriptEngine.
  80      *
  81      * @return full name of the ScriptEngine
  82      */
  83     public String getEngineName() {
  84         TRACE("getEngineName()");
  85         return ENGINE_NAME;
  86     }
  87 
  88     /**
  89      * Returns the version of the ScriptEngine.
  90      *
  91      * @return version of the ScriptEngine
  92      */
  93     public String getEngineVersion() {
  94         TRACE("getEngineVersion()");
  95         return ENGINE_VERSION;
  96     }
  97 
  98     /**
  99      * Returns the name of the scripting language supported by this ScriptEngine.
 100      *
 101      * @return name of the language supported by the ScriptEngine(Factory)
 102      */
 103     public String getLanguageName() {
 104         TRACE("getLanguageName()");
 105         return LANGUAGE;
 106     }
 107 
 108     /**
 109      * Returns the version of the scripting language supported by this ScriptEngine(Factory).
 110      *
 111      * @return language version supported by the ScriptEngine(Factory)
 112      */
 113     public String getLanguageVersion() {
 114         TRACE("getLanguageVersion()");
 115         return AccessController.doPrivileged(new PrivilegedAction<String>() {
 116             public String run() {
 117                 final AppleScriptEngine engine = new AppleScriptEngine(AppleScriptEngineFactory.this);
 118                 return engine.getLanguageVersion();
 119             }
 120         });
 121     }
 122 
 123     /**
 124      * Returns an immutable list of filename extensions, which generally identify
 125      * scripts written in the language supported by this ScriptEngine.
 126      *
 127      * @return ArrayList of file extensions AppleScript associates with
 128      */
 129     public List<String> getExtensions() {
 130         TRACE("getExtensions()");
 131         return Arrays.asList("scpt", "applescript", "app");
 132     }
 133 
 134     /**
 135      * Returns an immutable list of mimetypes, associated with scripts
 136      * that can be executed by the engine.
 137      *
 138      * @return ArrayList of mimetypes that AppleScript associates with
 139      */
 140     public List<String> getMimeTypes() {
 141         TRACE("getMimeTypes()");
 142         return Arrays.asList("application/x-applescript", "text/plain", "text/applescript");
 143     }
 144 
 145     /**
 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      * @return
 150      */
 151     public List<String> getNames() {
 152         TRACE("getNames()");
 153         return Arrays.asList("AppleScriptEngine", "AppleScript", "OSA");
 154     }
 155 
 156     /**
 157      * Returns a String which can be used to invoke a method of a Java
 158      * object using the syntax of the supported scripting language.
 159      *
 160      * @param obj
 161      *            unused -- AppleScript does not support objects
 162      * @param m
 163      *            function name
 164      * @param args
 165      *            arguments to the function
 166      * @return the AppleScript string calling the method
 167      */
 168     public String getMethodCallSyntax(final String obj, final String fname, final String ... args) {
 169 //        StringBuilder builder = new StringBuilder();
 170 //        builder.append("my " + fname + "(");
 171 //        // TODO -- do
 172 //        builder.append(")\n");
 173 //        return builder.toString();
 174 
 175         return null;
 176     }
 177 
 178     /**
 179      * Returns a String that can be used as a statement to display the specified String using the syntax of the supported scripting language.
 180      *
 181      * @param toDisplay
 182      * @return
 183      */
 184     public String getOutputStatement(final String toDisplay) {
 185         // TODO -- this might even be good enough? XD
 186         return getMethodCallSyntax(null, "print", toDisplay);
 187     }
 188 
 189     /**
 190      * Returns the value of an attribute whose meaning may be implementation-specific.
 191      *
 192      * @param key
 193      *            the key to look up
 194      * @return the static preseeded value for the key in the ScriptEngine, if it exists, otherwise <code>null</code>
 195      */
 196     public Object getParameter(final String key) {
 197         final AppleScriptEngine engine = new AppleScriptEngine(this);
 198         if (!engine.getBindings(ScriptContext.ENGINE_SCOPE).containsKey(key)) return null;
 199         return engine.getBindings(ScriptContext.ENGINE_SCOPE).get(key);
 200     }
 201 
 202     /**
 203      * Returns A valid scripting language executable program with given statements.
 204      *
 205      * @param statements
 206      * @return
 207      */
 208     public String getProgram(final String ... statements) {
 209         final StringBuilder program = new StringBuilder();
 210         for (final String statement : statements) {
 211             program.append(statement + "\n");
 212         }
 213         return program.toString();
 214     }
 215 
 216     /**
 217      * Returns an instance of the ScriptEngine associated with this ScriptEngineFactory.
 218      *
 219      * @return new AppleScriptEngine with this factory as it's parent
 220      */
 221     public ScriptEngine getScriptEngine() {
 222         AppleScriptEngine.checkSecurity();
 223         return new AppleScriptEngine(this);
 224     }
 225 }