src/macosx/classes/apple/applescript/AppleScriptEngineFactory.java

Print this page


   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 


  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 }
   1 /*
   2  * Copyright (c) 2011, 2013, 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 volatile boolean initialized = false;
  34 
  35     private static native void initNative();





  36 
  37     static void TRACE(final String str) {
  38 //        System.out.println(AppleScriptEngineFactory.class.getName() + "." + str);
  39     }
  40 
  41     /**
  42      * The name of this ScriptEngine
  43      */
  44     static final String ENGINE_NAME = "AppleScriptEngine";
  45 
  46     /**
  47      * The version of this ScriptEngine
  48      */
  49     static final String ENGINE_VERSION = "1.1";
  50 
  51     /**
  52      * The name of this ScriptEngine (yes, again)
  53      */
  54     static final String ENGINE_SHORT_NAME = ENGINE_NAME;
  55 


  58      */
  59     static final String LANGUAGE = "AppleScript";
  60 
  61     static ScriptEngineFactory getFactory() {
  62         TRACE("getFactory()");
  63         return new AppleScriptEngineFactory();
  64     }
  65 
  66     /**
  67      * Initialize a new AppleScriptEngineFactory, replete with a member AppleScriptEngine
  68      */
  69     public AppleScriptEngineFactory() {
  70         TRACE("<ctor>()");
  71     }
  72 
  73     /**
  74      * Returns the full name of the ScriptEngine.
  75      *
  76      * @return full name of the ScriptEngine
  77      */
  78     @Override
  79     public String getEngineName() {
  80         TRACE("getEngineName()");
  81         return ENGINE_NAME;
  82     }
  83 
  84     /**
  85      * Returns the version of the ScriptEngine.
  86      *
  87      * @return version of the ScriptEngine
  88      */
  89     @Override
  90     public String getEngineVersion() {
  91         TRACE("getEngineVersion()");
  92         return ENGINE_VERSION;
  93     }
  94 
  95     /**
  96      * Returns the name of the scripting language supported by this ScriptEngine.
  97      *
  98      * @return name of the language supported by the ScriptEngine(Factory)
  99      */
 100     @Override
 101     public String getLanguageName() {
 102         TRACE("getLanguageName()");
 103         return LANGUAGE;
 104     }
 105 
 106     /**
 107      * Returns the version of the scripting language supported by this ScriptEngine(Factory).
 108      *
 109      * @return language version supported by the ScriptEngine(Factory)
 110      */
 111     @Override
 112     public String getLanguageVersion() {
 113         TRACE("getLanguageVersion()");
 114         return AccessController.doPrivileged(new PrivilegedAction<String>() {
 115             public String run() {
 116                 final AppleScriptEngine engine = getScriptEngine();
 117                 return engine.getLanguageVersion();
 118             }
 119         });
 120     }
 121 
 122     /**
 123      * Returns an immutable list of filename extensions, which generally identify
 124      * scripts written in the language supported by this ScriptEngine.
 125      *
 126      * @return ArrayList of file extensions AppleScript associates with
 127      */
 128     @Override
 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     @Override
 141     public List<String> getMimeTypes() {
 142         TRACE("getMimeTypes()");
 143         return Arrays.asList("application/x-applescript", "text/plain", "text/applescript");
 144     }
 145 
 146     /**
 147      * Returns an immutable list of short names for the ScriptEngine,
 148      * which may be used to identify the ScriptEngine by the ScriptEngineManager.
 149      *
 150      * @return
 151      */
 152     @Override
 153     public List<String> getNames() {
 154         TRACE("getNames()");
 155         return Arrays.asList("AppleScriptEngine", "AppleScript", "OSA");
 156     }
 157 
 158     /**
 159      * Returns a String which can be used to invoke a method of a Java
 160      * object using the syntax of the supported scripting language.
 161      *
 162      * @param obj
 163      *            unused -- AppleScript does not support objects
 164      * @param m
 165      *            function name
 166      * @param args
 167      *            arguments to the function
 168      * @return the AppleScript string calling the method
 169      */
 170     @Override
 171     public String getMethodCallSyntax(final String obj, final String fname, final String ... args) {
 172 //        StringBuilder builder = new StringBuilder();
 173 //        builder.append("my " + fname + "(");
 174 //        // TODO -- do
 175 //        builder.append(")\n");
 176 //        return builder.toString();
 177 
 178         return null;
 179     }
 180 
 181     /**
 182      * Returns a String that can be used as a statement to display the specified String using the syntax of the supported scripting language.
 183      *
 184      * @param toDisplay
 185      * @return
 186      */
 187     @Override
 188     public String getOutputStatement(final String toDisplay) {
 189         // TODO -- this might even be good enough? XD
 190         return getMethodCallSyntax(null, "print", toDisplay);
 191     }
 192 
 193     /**
 194      * Returns the value of an attribute whose meaning may be implementation-specific.
 195      *
 196      * @param key
 197      *            the key to look up
 198      * @return the static preseeded value for the key in the ScriptEngine, if it exists, otherwise <code>null</code>
 199      */
 200     @Override
 201     public Object getParameter(final String key) {
 202         final AppleScriptEngine engine = getScriptEngine();
 203         if (!engine.getBindings(ScriptContext.ENGINE_SCOPE).containsKey(key)) return null;
 204         return engine.getBindings(ScriptContext.ENGINE_SCOPE).get(key);
 205     }
 206 
 207     /**
 208      * Returns A valid scripting language executable program with given statements.
 209      *
 210      * @param statements
 211      * @return
 212      */
 213     @Override
 214     public String getProgram(final String ... statements) {
 215         final StringBuilder program = new StringBuilder();
 216         for (final String statement : statements) {
 217             program.append(statement + "\n");
 218         }
 219         return program.toString();
 220     }
 221 
 222     /**
 223      * Returns an instance of the ScriptEngine associated with this ScriptEngineFactory.
 224      *
 225      * @return new AppleScriptEngine with this factory as it's parent
 226      */
 227     @Override
 228     public AppleScriptEngine getScriptEngine() {
 229         AppleScriptEngine.checkSecurity();
 230         ensureInitialized();
 231 
 232         return new AppleScriptEngine(this);
 233     }
 234 
 235     private static synchronized void ensureInitialized() {
 236         if (!initialized) {
 237             initialized = true;
 238 
 239             java.awt.Toolkit.getDefaultToolkit();
 240             System.loadLibrary("AppleScriptEngine");
 241             initNative();
 242         }
 243     }
 244 }