use of javax.script.ScriptEngineManager in project java8-tutorial by winterbe.
the class Nashorn4 method main.
public static void main(String[] args) throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval("loadWithNewGlobal('res/nashorn4.js')");
}
use of javax.script.ScriptEngineManager in project java8-tutorial by winterbe.
the class Nashorn5 method main.
public static void main(String[] args) throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval("load('res/nashorn5.js')");
Invocable invocable = (Invocable) engine;
Product product = new Product();
product.setName("Rubber");
product.setPrice(1.99);
product.setStock(1037);
Object result = invocable.invokeFunction("getValueOfGoods", product);
System.out.println(result);
}
use of javax.script.ScriptEngineManager in project java8-tutorial by winterbe.
the class Nashorn6 method main.
public static void main(String[] args) throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval("load('res/nashorn6.js')");
Invocable invocable = (Invocable) engine;
Product product = new Product();
product.setName("Rubber");
product.setPrice(1.99);
product.setStock(1337);
ScriptObjectMirror result = (ScriptObjectMirror) invocable.invokeFunction("calculate", product);
System.out.println(result.get("name") + ": " + result.get("valueOfGoods"));
}
use of javax.script.ScriptEngineManager in project java8-tutorial by winterbe.
the class Nashorn8 method main.
public static void main(String[] args) throws ScriptException, NoSuchMethodException {
NashornScriptEngine engine = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
engine.eval("load('res/nashorn8.js')");
// [object global]
engine.invokeFunction("evaluate1");
// [object Object]
engine.invokeFunction("evaluate2");
// Foobar
engine.invokeFunction("evaluate3", "Foobar");
// [object global] <- ???????
engine.invokeFunction("evaluate3", new Person("John", "Doe"));
}
use of javax.script.ScriptEngineManager in project apex-core by apache.
the class StramClientUtils method evalProperties.
public static void evalProperties(Properties target, Configuration vars) {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
Pattern substitutionPattern = Pattern.compile("\\$\\{(.+?)\\}");
Pattern evalPattern = Pattern.compile("\\{% (.+?) %\\}");
try {
engine.eval("var _prop = {}");
for (Map.Entry<String, String> entry : vars) {
String evalString = String.format("_prop[\"%s\"] = \"%s\"", StringEscapeUtils.escapeJava(entry.getKey()), StringEscapeUtils.escapeJava(entry.getValue()));
engine.eval(evalString);
}
} catch (ScriptException ex) {
LOG.warn("Javascript error: {}", ex.getMessage());
}
for (Map.Entry<Object, Object> entry : target.entrySet()) {
String value = entry.getValue().toString();
Matcher matcher = substitutionPattern.matcher(value);
if (matcher.find()) {
StringBuilder newValue = new StringBuilder();
int cursor = 0;
do {
newValue.append(value.substring(cursor, matcher.start()));
String subst = vars.get(matcher.group(1));
if (subst != null) {
newValue.append(subst);
}
cursor = matcher.end();
} while (matcher.find());
newValue.append(value.substring(cursor));
target.put(entry.getKey(), newValue.toString());
}
matcher = evalPattern.matcher(value);
if (matcher.find()) {
StringBuilder newValue = new StringBuilder();
int cursor = 0;
do {
newValue.append(value.substring(cursor, matcher.start()));
try {
Object result = engine.eval(matcher.group(1));
String eval = result.toString();
if (eval != null) {
newValue.append(eval);
}
} catch (ScriptException ex) {
LOG.warn("JavaScript exception {}", ex.getMessage());
}
cursor = matcher.end();
} while (matcher.find());
newValue.append(value.substring(cursor));
target.put(entry.getKey(), newValue.toString());
}
}
}
Aggregations