Java Code Examples for javax.script.ScriptEngineManager#getEngineByMimeType()
The following examples show how to use
javax.script.ScriptEngineManager#getEngineByMimeType() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonpTest.java From mica with GNU Lesser General Public License v3.0 | 7 votes |
@Test public void test() throws ScriptException { String jsonp = "/**/callback( {\"client_id\":\"123\",\"openid\":\"123\",\"unionid\":\"123\"} )"; String jsFun = "function callback(json) { return json };"; ScriptEngineManager engineManager = new ScriptEngineManager(); ScriptEngine engine = engineManager.getEngineByMimeType("text/javascript"); engine.eval(jsFun); Map json = (Map) engine.eval(jsonp); Assert.assertEquals("123", json.get("client_id")); Assert.assertEquals("123", json.get("openid")); Assert.assertEquals("123", json.get("unionid")); }
Example 2
Source File: Main.java From sieve with MIT License | 6 votes |
public static void main(String... args) throws Exception { File script = findSieveJs(); ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine engine = sem.getEngineByName("Graal.js"); if (engine == null) { engine = sem.getEngineByMimeType("text/javascript"); } System.err.printf("Using engine %s to run %s%n", engine.getFactory().getEngineName(), script); if (args.length == 1) { int repeat = Integer.parseInt(args[0]); engine.eval("this.count = " + repeat); } try (FileReader reader = new FileReader(script)) { engine.eval(reader); } }
Example 3
Source File: MethodHandleRelatedCrashIT.java From glowroot with Apache License 2.0 | 6 votes |
@Override public void executeApp() throws Exception { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByMimeType("application/javascript"); try { for (int i = 0; i < 1000; i++) { String js = "var map = Array.prototype.map;" + "var names = ['john', 'jerry', 'bob'];" + "var a = map.call(names, function(name) { return name.length })"; engine.eval(js); } } catch (Exception e) { e.printStackTrace(); throw e; } }
Example 4
Source File: GraalJSTest.java From netbeans with Apache License 2.0 | 5 votes |
public void testJavaScriptEngineIsGraalJS() { ScriptEngineManager m = Scripting.createManager(); StringBuilder sb = new StringBuilder(); for (ScriptEngineFactory f : m.getEngineFactories()) { sb.append("\nf: ").append(f.getEngineName()).append(" ext: ").append(f.getMimeTypes()); } ScriptEngine text = m.getEngineByMimeType("text/javascript"); assertEquals(sb.toString(), "GraalVM:js", text.getFactory().getEngineName()); ScriptEngine app = m.getEngineByMimeType("application/javascript"); assertEquals(sb.toString(), "GraalVM:js", app.getFactory().getEngineName()); }
Example 5
Source File: JavaScriptEnginesTest.java From netbeans with Apache License 2.0 | 5 votes |
private static void fillArray(final ScriptEngineManager man, boolean allowAllAccess, List<Object[]> arr) { for (ScriptEngineFactory f : man.getEngineFactories()) { final String name = f.getEngineName(); if ( f.getMimeTypes().contains("text/javascript") || name.contains("Nashorn") ) { final ScriptEngine eng = f.getScriptEngine(); arr.add(new Object[] { name, "engineFactories", implName(eng), eng, allowAllAccess }); for (String n : eng.getFactory().getNames()) { ScriptEngine byName = n == null ? null : man.getEngineByName(n); if (byName != null && eng.getClass() == byName.getClass()) { arr.add(new Object[] { n, "name", implName(byName), byName, allowAllAccess }); } } for (String t : eng.getFactory().getMimeTypes()) { ScriptEngine byType = t == null ? null : man.getEngineByMimeType(t); if (byType != null && eng.getClass() == byType.getClass()) { arr.add(new Object[] { t, "type", implName(byType), byType, allowAllAccess }); } } for (String e : eng.getFactory().getExtensions()) { ScriptEngine byExt = e == null ? null : man.getEngineByExtension(e); if (byExt != null && eng.getClass() == byExt.getClass()) { arr.add(new Object[] { e, "ext", implName(byExt), byExt, allowAllAccess }); } } } } }
Example 6
Source File: ScriptingTutorial.java From netbeans with Apache License 2.0 | 5 votes |
public void callRFunctionFromJava() throws Exception { // BEGIN: org.netbeans.libs.graalsdk.ScriptingTutorial#allowAllAccess // FastR currently needs access to native libraries: final ScriptEngineManager manager = Scripting.newBuilder().allowAllAccess(true).build(); ScriptEngine rEngine = manager.getEngineByMimeType("application/x-r"); // END: org.netbeans.libs.graalsdk.ScriptingTutorial#allowAllAccess final Object funcRaw = rEngine.eval("qbinom"); BinomQuantile func = ((Invocable) rEngine).getInterface(funcRaw, BinomQuantile.class); assertEquals(4, func.qbinom(0.37, 10, 0.5)); }
Example 7
Source File: JsUtil.java From util with Apache License 2.0 | 5 votes |
/** * 功能:构造函数。(文件路径) * @author 朱志杰 QQ:695520848 * May 31, 2013 9:05:22 AM * @param jsFilePaths 文件路径下的js文件全路径,可以同时传入很多js路径。 * @throws ScriptException 读取js文件异常。 * @throws FileNotFoundException js文件没有找到。 */ public JsUtil(String... jsFilePaths) throws FileNotFoundException, ScriptException{ ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByMimeType("text/javascript"); for(String filePath : jsFilePaths){ engine.eval(new FileReader(filePath)); } inv = (Invocable) engine; }
Example 8
Source File: JsUtil.java From util with Apache License 2.0 | 5 votes |
/** * 功能:构造函数。(网络地址) * @author 朱志杰 QQ:695520848 * May 31, 2013 9:05:22 AM * @param jsUrl js文件在网络上的全路径,可以同时传入多个JS的URL。 * @throws ScriptException 读取js文件异常。 * @throws IOException 从网路上加载js文件异常。 */ public JsUtil(URL... jsUrls) throws ScriptException, IOException{ ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByMimeType("text/javascript"); //从网络读取js文件流 for(URL url: jsUrls){ InputStreamReader isr=new InputStreamReader(url.openStream()); BufferedReader br=new BufferedReader(isr); engine.eval(br); } inv = (Invocable) engine; }
Example 9
Source File: AttributeCalculatorPlugin.java From constellation with Apache License 2.0 | 4 votes |
@Override public void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException { final ScriptEngineManager manager = new ScriptEngineManager(); final ScriptEngine engine = manager.getEngineByMimeType(language); final Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); final CalculatorContextManager calculatorContext = new CalculatorContextManager(graph, elementType); final Map<Integer, Object> editedAttributeValues = new HashMap<>(); preprocessScriptAndBindObjects(graph, bindings, calculatorContext); LOGGER.log(Level.INFO, "processedScript::{0}", script); int editAttributeId = graph.getAttribute(elementType, editAttribute); if (editAttributeId == Graph.NOT_FOUND) { editAttributeId = graph.addAttribute(elementType, editAttributeType, editAttribute, "", null, null); } int selectedAttr = selectedOnly ? graph.getAttribute(elementType, "selected") : Graph.NOT_FOUND; try { // Import any desired modules before trying to do anything with the script. importModules(engine); CompiledScript compiledScript; CompiledScript functionWrapperScript = null; final CompiledScript testExpressionScript = ((Compilable) engine).compile("ast.parse(" + __builtin__.repr(new PyString(script)) + ", mode='eval')"); // shiiii, such a ridic line of code try { testExpressionScript.eval(); compiledScript = ((Compilable) engine).compile(script); } catch (ScriptException e) { final String functionWrappedScript = "def __script__():\n " + script.replace("\n", "\n ") + "\n"; LOGGER.log(Level.INFO, "processedScript::{0}", functionWrappedScript); compiledScript = ((Compilable) engine).compile(functionWrappedScript); functionWrapperScript = ((Compilable) engine).compile("__script__()"); } final int elementCount = elementType == GraphElementType.VERTEX ? graph.getVertexCount() : graph.getTransactionCount(); // Compute the values for the desired attribute for (int i = 0; i < elementCount; i++) { final int elementId = elementType == GraphElementType.VERTEX ? graph.getVertex(i) : graph.getTransaction(i); if (selectedAttr == Graph.NOT_FOUND || graph.getBooleanValue(selectedAttr, elementId)) { calculatorContext.enter(elementId); Object result = compiledScript.eval(); if (functionWrapperScript != null) { result = functionWrapperScript.eval(); } if (result == AbstractCalculatorValue.getTheObliterator()) { result = null; } editedAttributeValues.put(elementId, result); calculatorContext.exit(); } } // Edit the actual attribute values for the desired attribute for (final Map.Entry<Integer, Object> entry : editedAttributeValues.entrySet()) { graph.setObjectValue(editAttributeId, entry.getKey(), entry.getValue()); if (!completeWithSchema) { // do nothing } else if (elementType == GraphElementType.VERTEX) { if (graph.getSchema() != null) { graph.getSchema().completeVertex(graph, entry.getKey()); } } else { if (graph.getSchema() != null) { graph.getSchema().completeTransaction(graph, entry.getKey()); } } } } catch (ScriptException ex) { throw new PluginException(PluginNotificationLevel.ERROR, "Attribute Calculator Error: " + ex.getMessage()); } }
Example 10
Source File: BindingsNGTest.java From netbeans with Apache License 2.0 | 4 votes |
@BeforeMethod public void initEngine() { ScriptEngineManager sem = Scripting.createManager(); eng = sem.getEngineByMimeType("text/javascript"); }