1 /* 2 * Copyright (c) 2005, 2010, 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 com.sun.script.javascript; 27 import javax.script.*; 28 import java.util.*; 29 import sun.org.mozilla.javascript.internal.*; 30 import com.sun.script.util.*; 31 32 /** 33 * Factory to create RhinoScriptEngine 34 * 35 * @author Mike Grogan 36 * @since 1.6 37 */ 38 public class RhinoScriptEngineFactory extends ScriptEngineFactoryBase { 39 40 public RhinoScriptEngineFactory() { 41 } 42 43 public List<String> getExtensions() { 44 return extensions; 45 } 46 47 public List<String> getMimeTypes() { 48 return mimeTypes; 49 } 50 51 public List<String> getNames() { 52 return names; 53 } 54 55 public Object getParameter(String key) { 56 if (key.equals(ScriptEngine.NAME)) { 57 return "javascript"; 58 } else if (key.equals(ScriptEngine.ENGINE)) { 59 return "Mozilla Rhino"; 60 } else if (key.equals(ScriptEngine.ENGINE_VERSION)) { 61 return "1.7 release 3 PRERELEASE"; 62 } else if (key.equals(ScriptEngine.LANGUAGE)) { 63 return "ECMAScript"; 64 } else if (key.equals(ScriptEngine.LANGUAGE_VERSION)) { 65 return "1.8"; 66 } else if (key.equals("THREADING")) { 67 return "MULTITHREADED"; 68 } else { 69 throw new IllegalArgumentException("Invalid key"); 70 } 71 } 72 73 public ScriptEngine getScriptEngine() { 74 RhinoScriptEngine ret = new RhinoScriptEngine(); 75 ret.setEngineFactory(this); 76 return ret; 77 } 78 79 public String getMethodCallSyntax(String obj, String method, String... args) { 80 81 String ret = obj + "." + method + "("; 82 int len = args.length; 83 if (len == 0) { 84 ret += ")"; 85 return ret; 86 } 87 88 for (int i = 0; i < len; i++) { 89 ret += args[i]; 90 if (i != len - 1) { 91 ret += ","; 92 } else { 93 ret += ")"; 94 } 95 } 96 return ret; 97 } 98 99 public String getOutputStatement(String toDisplay) { 100 StringBuffer buf = new StringBuffer(); 101 int len = toDisplay.length(); 102 buf.append("print(\""); 103 for (int i = 0; i < len; i++) { 104 char ch = toDisplay.charAt(i); 105 switch (ch) { 106 case '"': 107 buf.append("\\\""); 108 break; 109 case '\\': 110 buf.append("\\\\"); 111 break; 112 default: 113 buf.append(ch); 114 break; 115 } 116 } 117 buf.append("\")"); 118 return buf.toString(); 119 } 120 121 public String getProgram(String... statements) { 122 int len = statements.length; 123 String ret = ""; 124 for (int i = 0; i < len; i++) { 125 ret += statements[i] + ";"; 126 } 127 128 return ret; 129 } 130 131 /* 132 public static void main(String[] args) { 133 RhinoScriptEngineFactory fact = new RhinoScriptEngineFactory(); 134 System.out.println(fact.getParameter(ScriptEngine.ENGINE_VERSION)); 135 } 136 */ 137 138 private static List<String> names; 139 private static List<String> mimeTypes; 140 private static List<String> extensions; 141 142 static { 143 names = new ArrayList<String>(6); 144 names.add("js"); 145 names.add("rhino"); 146 names.add("JavaScript"); 147 names.add("javascript"); 148 names.add("ECMAScript"); 149 names.add("ecmascript"); 150 names = Collections.unmodifiableList(names); 151 152 mimeTypes = new ArrayList<String>(4); 153 mimeTypes.add("application/javascript"); 154 mimeTypes.add("application/ecmascript"); 155 mimeTypes.add("text/javascript"); 156 mimeTypes.add("text/ecmascript"); 157 mimeTypes = Collections.unmodifiableList(mimeTypes); 158 159 extensions = new ArrayList<String>(1); 160 extensions.add("js"); 161 extensions = Collections.unmodifiableList(extensions); 162 } 163 }