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 56 /** 57 * The name of the language supported by this ScriptEngine 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 }