001/* 002 * Copyright 2010-2015 Institut Pasteur. 003 * 004 * This file is part of Icy. 005 * 006 * Icy is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Icy is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Icy. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package icy.script; 020 021/** 022 * @author fab 023 */ 024public class ScriptReader 025{ 026 027 // import java.io.InputStream; 028 // import java.io.InputStreamReader; 029 // import java.io.Reader; 030 // import java.util.ArrayList; 031 // import java.util.List; 032 // 033 // import javax.script.Invocable; 034 // import javax.script.ScriptEngine; 035 // import javax.script.ScriptEngineFactory; 036 // import javax.script.ScriptEngineManager; 037 // import javax.script.ScriptException; 038 // 039 040 // System.out.println(""); 041 // System.out.println("------ Script supportés: "); 042 // 043 // 044 // { 045 // 046 // ScriptEngineManager mgr = new ScriptEngineManager(); 047 // List<ScriptEngineFactory> factories = 048 // mgr.getEngineFactories(); 049 // for (ScriptEngineFactory factory: factories) { 050 // System.out.println("ScriptEngineFactory Info"); 051 // String engName = factory.getEngineName(); 052 // String engVersion = factory.getEngineVersion(); 053 // String langName = factory.getLanguageName(); 054 // String langVersion = factory.getLanguageVersion(); 055 // System.out.printf("\tScript Engine: %s (%s)\n", 056 // engName, engVersion); 057 // List<String> engNames = factory.getNames(); 058 // for(String name: engNames) { 059 // System.out.printf("\tEngine Alias: %s\n", name); 060 // } 061 // System.out.printf("\tLanguage: %s (%s)\n", 062 // langName, langVersion); 063 // } 064 // } 065 // 066 // System.out.println(""); 067 // System.out.println("------ Script test : "); 068 // 069 // { 070 // 071 // ScriptEngineManager mgr = new ScriptEngineManager(); 072 // ScriptEngine jsEngine = mgr.getEngineByName("JavaScript"); 073 // try { 074 // jsEngine.eval("print('Hello, world!')"); 075 // } catch (ScriptException ex) { 076 // ex.printStackTrace(); 077 // } 078 // } 079 080 // System.out.println("------ Script loading test : "); 081 082 // { 083 // ScriptEngineManager engineMgr = new ScriptEngineManager(); 084 // ScriptEngine engine = engineMgr.getEngineByName("JavaScript"); 085 // InputStream is = 086 // this.getClass().getResourceAsStream("/script/test.js"); 087 // try { 088 // Reader reader = new InputStreamReader(is); 089 // engine.eval(reader); 090 // } catch (ScriptException ex) { 091 // ex.printStackTrace(); 092 // } 093 // } 094 095 // System.out.println(""); 096 // System.out.println("------ Script test : using invocable to call function in a script."); 097 // 098 // { 099 // ScriptEngineManager mgr = new ScriptEngineManager(); 100 // ScriptEngine jsEngine = mgr.getEngineByName("JavaScript"); 101 // try { 102 // jsEngine.eval("function sayHello() {" + 103 // " println('Hello, world!');" + 104 // "}"); 105 // } catch (ScriptException e) { 106 // // TODO Auto-generated catch block 107 // e.printStackTrace(); 108 // } 109 // Invocable invocableEngine = (Invocable) jsEngine; 110 // try { 111 // invocableEngine.invokeFunction("sayHello"); 112 // } catch (ScriptException e) { 113 // // TODO Auto-generated catch block 114 // e.printStackTrace(); 115 // } catch (NoSuchMethodException e) { 116 // // TODO Auto-generated catch block 117 // e.printStackTrace(); 118 // } 119 // } 120 // 121 // System.out.println(""); 122 // System.out.println("------ Script test : using script to access java host."); 123 // 124 // { 125 // List<String> namesList = new ArrayList<String>(); 126 // namesList.add("Jill"); 127 // namesList.add("Bob"); 128 // namesList.add("Laureen"); 129 // namesList.add("Ed"); 130 // 131 // ScriptEngineManager mgr = new ScriptEngineManager(); 132 // ScriptEngine jsEngine = mgr.getEngineByName("JavaScript"); 133 // 134 // jsEngine.put("namesListKey", namesList); 135 // System.out.println("Executing in script environment..."); 136 // try { 137 // jsEngine.eval("var x;" + 138 // "var names = namesListKey.toArray();" + 139 // "for(x in names) {" + 140 // " println(names[x]);" + 141 // "}" + 142 // "namesListKey.add(\"Dana\");"); 143 // } catch (ScriptException ex) { 144 // ex.printStackTrace(); 145 // } 146 // System.out.println("Executing in Java environment..."); 147 // for (String name: namesList) { 148 // System.out.println(name); 149 // } 150 // 151 // 152 // } 153 // 154 // System.out.println(""); 155 // System.out.println("------ Script test : using script function with java's host object as Input."); 156 // 157 // { 158 // List<String> namesList = new ArrayList<String>(); 159 // namesList.add("Jill"); 160 // namesList.add("Bob"); 161 // namesList.add("Laureen"); 162 // namesList.add("Ed"); 163 // 164 // ScriptEngineManager mgr = new ScriptEngineManager(); 165 // ScriptEngine jsEngine = mgr.getEngineByName("JavaScript"); 166 // 167 // Invocable invocableEngine = (Invocable)jsEngine; 168 // try { 169 // jsEngine.eval("function printNames1(namesList) {" + 170 // " var x;" + 171 // " var names = namesList.toArray();" + 172 // " for(x in names) {" + 173 // " println(names[x]);" + 174 // " }" + 175 // "}" + 176 // 177 // "function addName(namesList, name) {" + 178 // " namesList.add(name);" + 179 // "}"); 180 // invocableEngine.invokeFunction("printNames1", namesList); 181 // invocableEngine.invokeFunction("addName", namesList, "Dana"); 182 // } catch (ScriptException ex) { 183 // ex.printStackTrace(); 184 // } catch (NoSuchMethodException ex) { 185 // ex.printStackTrace(); 186 // } 187 // } 188 // 189 // // System.out.println(""); 190 // // System.out.println("------ Script test : script import a java package."); 191 // // { 192 // // try { 193 // // ScriptEngineManager mgr = new ScriptEngineManager(); 194 // // ScriptEngine jsEngine = mgr.getEngineByName("JavaScript"); 195 // // 196 // // jsEngine.eval("importPackage(javax.swing);" + 197 // // "var optionPane = " + 198 // // " JOptionPane.showMessageDialog(null, 'Hello, world!');"); 199 // // } catch (ScriptException ex) { 200 // // ex.printStackTrace(); 201 // // } 202 // // } 203 // 204 // System.out.println(""); 205 // System.out.println("------ Script test : Using with a script."); 206 // { 207 // 208 // // Code Equivalent in icy : 209 // Sequence s = getSequenceAt( 0 ); 210 // // s.setName("name changed !"); 211 // // for ( int x = 0 ; x < 400 ; x ++ ) 212 // // for ( int y = 0 ; y < 400 ; y ++ ) 213 // // s.getImageAt(0).setRGB(x, y, 0 , s.getImageAt( 0 ).getGray8( x, y, 0 )/2 ); 214 // 215 // 216 // try 217 // { 218 // 219 // ScriptEngineManager mgr = new ScriptEngineManager(); 220 // ScriptEngine jsEngine = mgr.getEngineByName("JavaScript"); 221 // Invocable invocableEngine = (Invocable)jsEngine; 222 // 223 // jsEngine.eval( 224 // "importPackage(javax.swing);" + 225 // "function changeName( sequence ) {" + 226 // "sequence.setName('new name given by script');"+ 227 // "image = sequence.getImageAt( 0 ); "+ 228 // "for ( x = 0 ; x < 400 ; x ++ )"+ 229 // "for ( y = 0 ; y < 400 ; y ++ )"+ 230 // "image.setRGB( x , y , 0 ," + 231 // "image.getRGB(x, y, 0) / 2"+ 232 // ");"+ 233 // "}" ); 234 // 235 // invocableEngine.invokeFunction("changeName", getSequenceAt( 0 ) ); 236 // 237 // } catch (ScriptException ex) 238 // { 239 // ex.printStackTrace(); 240 // } catch (NoSuchMethodException e) { 241 // // TODO Auto-generated catch block 242 // e.printStackTrace(); 243 // } 244 // 245 // s.overlayChanged(); 246 // } 247 // 248 // System.out.println(""); 249 // System.out.println("------ Script test : End."); 250 251}