use of groovy.lang.Binding in project fess by codelibs.
the class DocBoostMatcherTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
ScriptEngineFactory scriptEngineFactory = new ScriptEngineFactory();
ComponentUtil.register(scriptEngineFactory, "scriptEngineFactory");
new AbstractScriptEngine() {
@Override
public Object evaluate(String template, Map<String, Object> paramMap) {
final Map<String, Object> bindingMap = new HashMap<>(paramMap);
bindingMap.put("container", SingletonLaContainerFactory.getContainer());
final GroovyShell groovyShell = new GroovyShell(new Binding(bindingMap));
try {
return groovyShell.evaluate(template);
} catch (final JobProcessingException e) {
throw e;
} catch (final Exception e) {
return null;
} finally {
final GroovyClassLoader loader = groovyShell.getClassLoader();
loader.clearCache();
}
}
@Override
protected String getName() {
return Constants.DEFAULT_SCRIPT;
}
}.register();
}
use of groovy.lang.Binding in project groovy-core by groovy.
the class GroovyMain method process.
/**
* Process the users request.
*
* @param line the parsed command line.
* @throws ParseException if invalid options are chosen
*/
private static boolean process(CommandLine line) throws ParseException, IOException {
List args = line.getArgList();
if (line.hasOption('D')) {
String[] values = line.getOptionValues('D');
for (int i = 0; i < values.length; i++) {
setSystemPropertyFrom(values[i]);
}
}
GroovyMain main = new GroovyMain();
// add the ability to parse scripts with a specified encoding
main.conf.setSourceEncoding(line.getOptionValue('c', main.conf.getSourceEncoding()));
main.isScriptFile = !line.hasOption('e');
main.debug = line.hasOption('d');
main.conf.setDebug(main.debug);
main.processFiles = line.hasOption('p') || line.hasOption('n');
main.autoOutput = line.hasOption('p');
main.editFiles = line.hasOption('i');
if (main.editFiles) {
main.backupExtension = line.getOptionValue('i');
}
main.autoSplit = line.hasOption('a');
String sp = line.getOptionValue('a');
if (sp != null)
main.splitPattern = sp;
if (main.isScriptFile) {
if (args.isEmpty())
throw new ParseException("neither -e or filename provided");
main.script = (String) args.remove(0);
if (main.script.endsWith(".java"))
throw new ParseException("error: cannot compile file with .java extension: " + main.script);
} else {
main.script = line.getOptionValue('e');
}
main.processSockets = line.hasOption('l');
if (main.processSockets) {
// default port to listen to
String p = line.getOptionValue('l', "1960");
main.port = Integer.parseInt(p);
}
// we use "," as default, because then split will create
// an empty array if no option is set
String disabled = line.getOptionValue("disableopt", ",");
String[] deopts = disabled.split(",");
for (String deopt_i : deopts) {
main.conf.getOptimizationOptions().put(deopt_i, false);
}
if (line.hasOption("indy")) {
CompilerConfiguration.DEFAULT.getOptimizationOptions().put("indy", true);
main.conf.getOptimizationOptions().put("indy", true);
}
if (line.hasOption("basescript")) {
main.conf.setScriptBaseClass(line.getOptionValue("basescript"));
}
if (line.hasOption("configscript")) {
String path = line.getOptionValue("configscript");
File groovyConfigurator = new File(path);
Binding binding = new Binding();
binding.setVariable("configuration", main.conf);
CompilerConfiguration configuratorConfig = new CompilerConfiguration();
ImportCustomizer customizer = new ImportCustomizer();
customizer.addStaticStars("org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder");
configuratorConfig.addCompilationCustomizers(customizer);
GroovyShell shell = new GroovyShell(binding, configuratorConfig);
shell.evaluate(groovyConfigurator);
}
main.args = args;
return main.run();
}
use of groovy.lang.Binding in project groovy-core by groovy.
the class Eval method xy.
/**
* Evaluates the specified String expression and makes the first two parameters available inside
* the script bound to variables named 'x' and 'y' respectively, returning the result. For example,
* this code executes without failure:
* <pre>
* assert 10 == Eval.xy(2, 4, ' x * y + 2')
* </pre>
* @param expression the Groovy expression to evaluate
* @return the result of the expression
* @throws CompilationFailedException if expression is not valid Groovy
*/
public static Object xy(final Object x, final Object y, final String expression) throws CompilationFailedException {
Binding b = new Binding();
b.setVariable("x", x);
b.setVariable("y", y);
GroovyShell sh = new GroovyShell(b);
return sh.evaluate(expression);
}
use of groovy.lang.Binding in project groovy-core by groovy.
the class GroovyScriptEngine method main.
/**
* Simple testing harness for the GSE. Enter script roots as arguments and
* then input script names to run them.
*
* @param urls an array of URLs
* @throws Exception if something goes wrong
*/
public static void main(String[] urls) throws Exception {
GroovyScriptEngine gse = new GroovyScriptEngine(urls);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while (true) {
System.out.print("groovy> ");
if ((line = br.readLine()) == null || line.equals("quit")) {
break;
}
try {
System.out.println(gse.run(line, new Binding()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
use of groovy.lang.Binding in project groovy-core by groovy.
the class GroovyScriptEngine method run.
/**
* Run a script identified by name with a single argument.
*
* @param scriptName name of the script to run
* @param argument a single argument passed as a variable named <code>arg</code> in the binding
* @return a <code>toString()</code> representation of the result of the execution of the script
* @throws ResourceException if there is a problem accessing the script
* @throws ScriptException if there is a problem parsing the script
*/
public String run(String scriptName, String argument) throws ResourceException, ScriptException {
Binding binding = new Binding();
binding.setVariable("arg", argument);
Object result = run(scriptName, binding);
return result == null ? "" : result.toString();
}
Aggregations