Search in sources :

Example 96 with Binding

use of groovy.lang.Binding in project groovy by apache.

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 97 with Binding

use of groovy.lang.Binding in project groovy by apache.

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)

Example 98 with Binding

use of groovy.lang.Binding in project groovy by apache.

the class Eval method me.

/**
 * Evaluates the specified String expression and makes the parameter available inside
 * the script, returning the result. For example, this code binds the 'x' variable:
 * <pre class="groovyTestCase">
 * assert Eval.me('x', 2, ' x * 4 + 2') == 10
 * </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 me(final String symbol, final Object object, final String expression) throws CompilationFailedException {
    Binding b = new Binding();
    b.setVariable(symbol, object);
    GroovyShell sh = new GroovyShell(b);
    return sh.evaluate(expression);
}
Also used : Binding(groovy.lang.Binding) GroovyShell(groovy.lang.GroovyShell)

Example 99 with Binding

use of groovy.lang.Binding in project groovy by apache.

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 class="groovyTestCase">
 * assert Eval.xy(2, 4, ' x * y + 2') == 10
 * </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 100 with Binding

use of groovy.lang.Binding in project grails-core by grails.

the class UrlMappingUtils method includeForUrlMappingInfoHelper.

private static IncludedContent includeForUrlMappingInfoHelper(String includeUrl, HttpServletRequest request, HttpServletResponse response, UrlMappingInfo info, Map model) {
    final GrailsWebRequest webRequest = GrailsWebRequest.lookup(request);
    String currentController = null;
    String currentAction = null;
    String currentId = null;
    ModelAndView currentMv = null;
    Binding currentPageBinding = null;
    Map currentParams = null;
    Object currentLayoutAttribute = null;
    Object currentRenderingView = null;
    if (webRequest != null) {
        currentPageBinding = (Binding) webRequest.getAttribute(GrailsApplicationAttributes.PAGE_SCOPE, 0);
        webRequest.removeAttribute(GrailsApplicationAttributes.PAGE_SCOPE, 0);
        currentLayoutAttribute = webRequest.getAttribute(WebUtils.LAYOUT_ATTRIBUTE, 0);
        if (currentLayoutAttribute != null) {
            webRequest.removeAttribute(WebUtils.LAYOUT_ATTRIBUTE, 0);
        }
        currentRenderingView = webRequest.getAttribute(WebUtils.RENDERING_VIEW, 0);
        if (currentRenderingView != null) {
            webRequest.removeAttribute(WebUtils.RENDERING_VIEW, 0);
        }
        currentController = webRequest.getControllerName();
        currentAction = webRequest.getActionName();
        currentId = webRequest.getId();
        currentParams = new HashMap();
        currentParams.putAll(webRequest.getParameterMap());
        currentMv = (ModelAndView) webRequest.getAttribute(GrailsApplicationAttributes.MODEL_AND_VIEW, 0);
    }
    try {
        if (webRequest != null) {
            webRequest.getParameterMap().clear();
            info.configure(webRequest);
            webRequest.getParameterMap().putAll(info.getParameters());
            webRequest.removeAttribute(GrailsApplicationAttributes.MODEL_AND_VIEW, 0);
        }
        return includeForUrl(includeUrl, request, response, model);
    } finally {
        if (webRequest != null) {
            if (webRequest.isActive()) {
                webRequest.setAttribute(GrailsApplicationAttributes.PAGE_SCOPE, currentPageBinding, 0);
                if (currentLayoutAttribute != null) {
                    webRequest.setAttribute(WebUtils.LAYOUT_ATTRIBUTE, currentLayoutAttribute, 0);
                }
                if (currentRenderingView != null) {
                    webRequest.setAttribute(WebUtils.RENDERING_VIEW, currentRenderingView, 0);
                }
                webRequest.getParameterMap().clear();
                webRequest.getParameterMap().putAll(currentParams);
                webRequest.setId(currentId);
                webRequest.setControllerName(currentController);
                webRequest.setActionName(currentAction);
                if (currentMv != null) {
                    webRequest.setAttribute(GrailsApplicationAttributes.MODEL_AND_VIEW, currentMv, 0);
                }
            } else {
                RequestContextHolder.setRequestAttributes(null);
            }
        }
    }
}
Also used : Binding(groovy.lang.Binding) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) HashMap(java.util.HashMap) Map(java.util.Map)

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