use of javax.script.ScriptEngine 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.ScriptEngine 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.ScriptEngine 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.ScriptEngine 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());
}
}
}
use of javax.script.ScriptEngine in project adempiere by adempiere.
the class MHRProcess method executeScriptEngine.
private Object executeScriptEngine(MHRConcept concept, MRule rule, String columnType) {
Object result = null;
try {
String text = "";
if (rule.getScript() != null) {
text = rule.getScript().trim().replaceAll("\\bget", "process.get").replace(".process.get", ".get");
}
String resultType = "double";
if (MHRAttribute.COLUMNTYPE_Date.equals(columnType))
resultType = "Timestamp";
else if (MHRAttribute.COLUMNTYPE_Text.equals(columnType))
resultType = "String";
final String script = s_scriptImport.toString() + Env.NL + resultType + " result = 0;" + Env.NL + "String description = null;" + Env.NL + text;
ScriptEngine engine = rule.getScriptEngine();
//MRule.setContext(engine, concept.getCtx(), 0); // no window
scriptCtx.entrySet().stream().forEach(entry -> engine.put(entry.getKey(), entry.getValue()));
result = engine.eval(script);
if (result != null && "@Error@".equals(result.toString())) {
throw new AdempiereException("@AD_Rule_ID@ @Error@" + result);
}
//
description = rule.getValue();
} catch (Exception e) {
throw new AdempiereException(e.getLocalizedMessage());
}
return result;
}
Aggregations