Search in sources :

Example 71 with Binding

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();
}
Also used : Binding(groovy.lang.Binding) JobProcessingException(org.codelibs.fess.exception.JobProcessingException) GroovyShell(groovy.lang.GroovyShell) JobProcessingException(org.codelibs.fess.exception.JobProcessingException) GroovyClassLoader(groovy.lang.GroovyClassLoader) ScriptEngineFactory(org.codelibs.fess.script.ScriptEngineFactory) Map(java.util.Map) HashMap(java.util.HashMap) AbstractScriptEngine(org.codelibs.fess.script.AbstractScriptEngine)

Example 72 with Binding

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();
}
Also used : Binding(groovy.lang.Binding) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) List(java.util.List) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) ParseException(org.apache.commons.cli.ParseException) GroovyShell(groovy.lang.GroovyShell)

Example 73 with Binding

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);
}
Also used : Binding(groovy.lang.Binding) GroovyShell(groovy.lang.GroovyShell)

Example 74 with Binding

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();
        }
    }
}
Also used : Binding(groovy.lang.Binding) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException)

Example 75 with Binding

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();
}
Also used : Binding(groovy.lang.Binding)

Aggregations

Binding (groovy.lang.Binding)213 GroovyShell (groovy.lang.GroovyShell)76 Script (groovy.lang.Script)55 Test (org.junit.Test)41 IOException (java.io.IOException)29 File (java.io.File)24 HashMap (java.util.HashMap)23 Closure (groovy.lang.Closure)22 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)22 Map (java.util.Map)20 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)13 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)12 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)11 MissingPropertyException (groovy.lang.MissingPropertyException)11 GroovyClassLoader (groovy.lang.GroovyClassLoader)10 StringWriter (java.io.StringWriter)10 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)9 InputStreamReader (java.io.InputStreamReader)9 Writer (java.io.Writer)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9