|
| 1 | +/* |
| 2 | + * Copyright (C) 2006 Sun Microsystems, Inc. All rights reserved. |
| 3 | + * Use is subject to license terms. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without modification, are |
| 6 | + * permitted provided that the following conditions are met: Redistributions of source code |
| 7 | + * must retain the above copyright notice, this list of conditions and the following disclaimer. |
| 8 | + * Redistributions in binary form must reproduce the above copyright notice, this list of |
| 9 | + * conditions and the following disclaimer in the documentation and/or other materials |
| 10 | + * provided with the distribution. Neither the name of the Sun Microsystems nor the names of |
| 11 | + * is contributors may be used to endorse or promote products derived from this software |
| 12 | + * without specific prior written permission. |
| 13 | +
|
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
| 15 | + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 16 | + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
| 17 | + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 19 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 20 | + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 21 | + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 22 | + * POSSIBILITY OF SUCH DAMAGE. |
| 23 | + */ |
| 24 | + |
| 25 | +package org.scijava.plugins.scripting.javascript; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Properties; |
| 30 | + |
| 31 | +import javax.script.ScriptEngine; |
| 32 | + |
| 33 | +import org.scijava.plugins.scripting.javascript.util.ScriptEngineFactoryBase; |
| 34 | + |
| 35 | +/** |
| 36 | + * Factory to create EmbeddedRhinoScriptEngine |
| 37 | + * |
| 38 | + */ |
| 39 | +public class EmbeddedRhinoScriptEngineFactory extends ScriptEngineFactoryBase { |
| 40 | + |
| 41 | + private Properties properties; |
| 42 | + private boolean initialized; |
| 43 | + |
| 44 | + public EmbeddedRhinoScriptEngineFactory() { |
| 45 | + } |
| 46 | + |
| 47 | + public List<String> getExtensions() { |
| 48 | + return extensions; |
| 49 | + } |
| 50 | + |
| 51 | + public List<String> getMimeTypes() { |
| 52 | + return mimeTypes; |
| 53 | + } |
| 54 | + |
| 55 | + public List<String> getNames() { |
| 56 | + return names; |
| 57 | + } |
| 58 | + |
| 59 | + public Object getParameter(String key) { |
| 60 | + if (key.equals(ScriptEngine.NAME)) { |
| 61 | + return "embedded-javascript"; |
| 62 | + } else if (key.equals(ScriptEngine.ENGINE)) { |
| 63 | + return "Mozilla Rhino"; |
| 64 | + } else if (key.equals(ScriptEngine.ENGINE_VERSION)) { |
| 65 | + return "1.6 release 2"; |
| 66 | + } else if (key.equals(ScriptEngine.LANGUAGE)) { |
| 67 | + return "EmbeddedECMAScript"; |
| 68 | + } else if (key.equals(ScriptEngine.LANGUAGE_VERSION)) { |
| 69 | + return "1.6"; |
| 70 | + } else if (key.equals("THREADING")) { |
| 71 | + return "MULTITHREADED"; |
| 72 | + } else { |
| 73 | + throw new IllegalArgumentException("Invalid key"); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public void setProperties(Properties properties) { |
| 78 | + this.properties = properties; |
| 79 | + } |
| 80 | + |
| 81 | + public ScriptEngine getScriptEngine() { |
| 82 | + EmbeddedRhinoScriptEngine ret = new EmbeddedRhinoScriptEngine(); |
| 83 | + ret.setEngineFactory(this); |
| 84 | + return ret; |
| 85 | + } |
| 86 | + |
| 87 | + public String getMethodCallSyntax(String obj, String method, String... args) { |
| 88 | + |
| 89 | + String ret = obj + "." + method + "("; |
| 90 | + int len = args.length; |
| 91 | + if (len == 0) { |
| 92 | + ret += ")"; |
| 93 | + return ret; |
| 94 | + } |
| 95 | + |
| 96 | + for (int i = 0; i < len; i++) { |
| 97 | + ret += args[i]; |
| 98 | + if (i != len - 1) { |
| 99 | + ret += ","; |
| 100 | + } else { |
| 101 | + ret += ")"; |
| 102 | + } |
| 103 | + } |
| 104 | + return ret; |
| 105 | + } |
| 106 | + |
| 107 | + public String getOutputStatement(String toDisplay) { |
| 108 | + return "print(" + toDisplay + ")"; |
| 109 | + } |
| 110 | + |
| 111 | + public String getProgram(String... statements) { |
| 112 | + int len = statements.length; |
| 113 | + String ret = ""; |
| 114 | + for (int i = 0; i < len; i++) { |
| 115 | + ret += statements[i] + ";"; |
| 116 | + } |
| 117 | + |
| 118 | + return ret; |
| 119 | + } |
| 120 | + |
| 121 | + public static void main(String[] args) { |
| 122 | + EmbeddedRhinoScriptEngineFactory fact = new EmbeddedRhinoScriptEngineFactory(); |
| 123 | + System.out.println(fact.getParameter(ScriptEngine.ENGINE_VERSION)); |
| 124 | + } |
| 125 | + |
| 126 | + private static List<String> names; |
| 127 | + private static List<String> mimeTypes; |
| 128 | + private static List<String> extensions; |
| 129 | + |
| 130 | + static { |
| 131 | + names = new ArrayList<String>(6); |
| 132 | + names.add("ejs"); |
| 133 | + names.add("EmbeddedJavaScript"); |
| 134 | + names.add("embeddedjavascript"); |
| 135 | + names = Collections.unmodifiableList(names); |
| 136 | + |
| 137 | + mimeTypes = new ArrayList<String>(4); |
| 138 | + mimeTypes.add("application/embeddedjavascript"); |
| 139 | + mimeTypes.add("text/embeddedjavascript"); |
| 140 | + mimeTypes = Collections.unmodifiableList(mimeTypes); |
| 141 | + |
| 142 | + extensions = new ArrayList<String>(1); |
| 143 | + extensions.add("ejs"); |
| 144 | + extensions = Collections.unmodifiableList(extensions); |
| 145 | + } |
| 146 | +} |
0 commit comments