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();
}
}
}
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();
}
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);
}
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);
}
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);
}
}
}
}
Aggregations