| 1 | import javax.swing.*;
|
|---|
| 2 | import java.awt.*;
|
|---|
| 3 | import java.util.*;
|
|---|
| 4 | import java.util.Map.Entry;
|
|---|
| 5 | import java.util.List;
|
|---|
| 6 | import java.util.zip.*;
|
|---|
| 7 | import java.io.*;
|
|---|
| 8 | import java.net.*;
|
|---|
| 9 |
|
|---|
| 10 | import javax.script.*;
|
|---|
| 11 |
|
|---|
| 12 | public class TestJS2 {
|
|---|
| 13 |
|
|---|
| 14 | public static class API {
|
|---|
| 15 | public static void hi() {
|
|---|
| 16 | System.out.println("ho");
|
|---|
| 17 | }
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | public static void main(String[] args) {
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | ScriptEngineManager mgr = new ScriptEngineManager();
|
|---|
| 24 |
|
|---|
| 25 | ScriptEngine jsEngine = mgr.getEngineByName("rhino");
|
|---|
| 26 |
|
|---|
| 27 | Map<String, String> map = new HashMap<String, String>();
|
|---|
| 28 | map.put("key1", "value1");
|
|---|
| 29 | map.put("key2", "value2");
|
|---|
| 30 | Map<String, List<String>> lists = new HashMap<String, List<String>>();
|
|---|
| 31 | lists.put("lkey1", Arrays.asList("first", "list"));
|
|---|
| 32 | lists.put("lkey2", Arrays.asList("second", "list"));
|
|---|
| 33 |
|
|---|
| 34 | jsEngine.put("map", map);
|
|---|
| 35 | jsEngine.put("lists", lists);
|
|---|
| 36 | jsEngine.put("_API", new API());
|
|---|
| 37 |
|
|---|
| 38 | String init =
|
|---|
| 39 | "API = {};"+
|
|---|
| 40 | "API.hi = _API.hi;"+
|
|---|
| 41 | "API.pref = {};"+
|
|---|
| 42 | "for (it = map.entrySet().iterator(); it.hasNext();) {"+
|
|---|
| 43 | " e = it.next();"+
|
|---|
| 44 | " API.pref[String(e.getKey())] = String(e.getValue());"+
|
|---|
| 45 | "}"+
|
|---|
| 46 | "for (it = lists.entrySet().iterator(); it.hasNext();) {"+
|
|---|
| 47 | " e = it.next();"+
|
|---|
| 48 | " list = e.getValue();"+
|
|---|
| 49 | " jsList = [];"+
|
|---|
| 50 | " for (i = 0; i < list.size(); i++) {"+
|
|---|
| 51 | " jsList.push(String(list.get(i)));"+
|
|---|
| 52 | " }"+
|
|---|
| 53 | " jsList.type = 'list';"+
|
|---|
| 54 | " API.pref[String(e.getKey())] = jsList;"+
|
|---|
| 55 | "}"+
|
|---|
| 56 | "map = null; lists = null;";
|
|---|
| 57 |
|
|---|
| 58 | String script =
|
|---|
| 59 | "API.hi();"+
|
|---|
| 60 | "API.pref['key3'] = 'value3';"+
|
|---|
| 61 | "API.pref['lkey2'].push('another');";
|
|---|
| 62 |
|
|---|
| 63 | String finish =
|
|---|
| 64 | "map = new java.util.HashMap;"+
|
|---|
| 65 | "lists = new java.util.HashMap;"+
|
|---|
| 66 | "for (key in API.pref) {"+
|
|---|
| 67 | " val = API.pref[key];"+
|
|---|
| 68 | " type = typeof val == 'string' ? 'string' : val.type;"+
|
|---|
| 69 | " if (type == 'string') {"+
|
|---|
| 70 | " map.put(key, val);"+
|
|---|
| 71 | " } else if (type == 'list') {"+
|
|---|
| 72 | " l = new java.util.ArrayList;"+
|
|---|
| 73 | " for (i=0; i<val.length; i++) {"+
|
|---|
| 74 | " l.add(val[i]);"+
|
|---|
| 75 | " }"+
|
|---|
| 76 | " lists.put(key, l);"+
|
|---|
| 77 | " }"+
|
|---|
| 78 | "}";
|
|---|
| 79 | try {
|
|---|
| 80 | System.out.println("Running init...");
|
|---|
| 81 | jsEngine.eval(init);
|
|---|
| 82 | System.out.println("Running script...");
|
|---|
| 83 | jsEngine.eval(script);
|
|---|
| 84 | System.out.println("Running finish...");
|
|---|
| 85 | jsEngine.eval(finish);
|
|---|
| 86 | Map<String, String> map2 = (HashMap) jsEngine.get("map");
|
|---|
| 87 | Map<String, List<String>> lists2 = (HashMap) jsEngine.get("lists");;
|
|---|
| 88 |
|
|---|
| 89 | System.out.println("map2: "+map2);
|
|---|
| 90 | System.out.println("lists2: "+lists2);
|
|---|
| 91 | } catch (ScriptException ex) {
|
|---|
| 92 | ex.printStackTrace();
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|