public Script parse(final String scriptText, final String fileName) throws CompilationFailedException { GroovyCodeSource gcs = AccessController.doPrivileged(new PrivilegedAction<GroovyCodeSource>() { public GroovyCodeSource run() { return new GroovyCodeSource(scriptText, fileName, DEFAULT_CODE_BASE); } }); return parse(gcs); }
private Object evaluate(final String expression) { Script script; if (SCRIPTS.containsKey(expression)) { script = SCRIPTS.get(expression); } else { script = SHELL.parse(expression); SCRIPTS.put(expression, script); } return script.run(); }
/** * Parses the given script and returns it ready to be run * * @param reader the stream reading the script * @param fileName is the logical file name of the script (which is used to create the class name of the script) * @return the parsed script which is ready to be run via {@link Script#run()} */ public Script parse(final Reader reader, final String fileName) throws CompilationFailedException { return parse(new GroovyCodeSource(reader, fileName, DEFAULT_CODE_BASE)); }
/** * Parses the given script and returns it ready to be run * * @param uri is the URI of the script (which is used to create the class name of the script) */ public Script parse(URI uri) throws CompilationFailedException, IOException { return parse(new GroovyCodeSource(uri)); }
/** * Parses the given script and returns it ready to be run * * @param scriptText the text of the script */ public Script parse(String scriptText) throws CompilationFailedException { return parse(scriptText, generateScriptName()); }
/** * Parses the given script and returns it ready to be run * * @param in the stream reading the script */ public Script parse(Reader in) throws CompilationFailedException { return parse(in, generateScriptName()); }
/** * Evaluates some script against the current Binding and returns the result * * @param codeSource * @throws CompilationFailedException */ public Object evaluate(GroovyCodeSource codeSource) throws CompilationFailedException { Script script = parse(codeSource); return script.run(); }
/** * Parses the bean definition groovy script by first exporting the given {@link Binding}. */ public void parse(InputStream script, Binding binding) { if (script==null) throw new IllegalArgumentException("No script is provided"); setBinding(binding); CompilerConfiguration cc = new CompilerConfiguration(); cc.setScriptBaseClass(ClosureScript.class.getName()); GroovyShell shell = new GroovyShell(classLoader,binding,cc); ClosureScript s = (ClosureScript)shell.parse(new InputStreamReader(script)); s.setDelegate(this); s.run(); }
/** * Runs this server. There is typically no need to call this method, as the object's constructor * creates a new thread and runs this object automatically. */ public void run() { try { ServerSocket serverSocket = new ServerSocket(url.getPort()); while (true) { // Create one script per socket connection. // This is purposefully not caching the Script // so that the script source file can be changed on the fly, // as each connection is made to the server. //FIXME: Groovy has other mechanisms specifically for watching to see if source code changes. // We should probably be using that here. // See also the comment about the fact we recompile a script that can't change. Script script = groovy.parse(source); new GroovyClientConnection(script, autoOutput, serverSocket.accept()); } } catch (Exception e) { e.printStackTrace(); } }
public Template createTemplate(Reader reader) throws CompilationFailedException, IOException { SimpleTemplate template = new SimpleTemplate(); String script = template.parse(reader); if (verbose) { System.out.println("\n-- script source --"); System.out.print(script); System.out.println("\n-- script end --\n"); } try { template.script = groovyShell.parse(script, "SimpleTemplateScript" + counter++ + ".groovy"); } catch (Exception e) { throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage()); } return template; }
/** * Parses the given script and returns it ready to be run * * @param file is the file of the script (which is used to create the class name of the script) */ public Script parse(File file) throws CompilationFailedException, IOException { return parse(new GroovyCodeSource(file, config.getSourceEncoding())); }
/** * Evaluates some script against the current Binding and returns the result * * @param in the stream reading the script * @param fileName is the logical file name of the script (which is used to create the class name of the script) */ public Object evaluate(Reader in, String fileName) throws CompilationFailedException { Script script = null; try { script = parse(in, fileName); return script.run(); } finally { if (script != null) { InvokerHelper.removeClass(script.getClass()); } } }
public Template createTemplate(Reader reader) throws CompilationFailedException, ClassNotFoundException, IOException { Node root ; try { root = xmlParser.parse(reader); } catch (SAXException e) { throw new RuntimeException("Parsing XML source failed.", e); } if (root == null) { throw new IOException("Parsing XML source failed: root node is null."); } StringWriter writer = new StringWriter(1024); writer.write("/* Generated by XmlTemplateEngine */\n"); new GspPrinter(new PrintWriter(writer), indentation).print(root); Script script; try { script = groovyShell.parse(writer.toString(), "XmlTemplateScript" + counter++ + ".groovy"); } catch (Exception e) { throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage()); } return new XmlTemplate(script); }
/** * Process the input files. */ private void processFiles() throws CompilationFailedException, IOException, URISyntaxException { GroovyShell groovy = new GroovyShell(conf); setupContextClassLoader(groovy); Script s = groovy.parse(getScriptSource(isScriptFile, script)); if (args.isEmpty()) { try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { PrintWriter writer = new PrintWriter(System.out); processReader(s, reader, writer); writer.flush(); } } else { Iterator i = args.iterator(); while (i.hasNext()) { String filename = (String) i.next(); //TODO: These are the arguments for -p and -i. Why are we searching using Groovy script extensions? // Where is this documented? File file = huntForTheScriptFile(filename); processFile(s, file); } } }
@Nullable private DelegatingScript loadScriptFromFileSystemLocation(String location) { File file = new File(location); try { if (!file.exists()) return null; } catch (AccessControlException e) { // no permission to check for existence of file (e.g. on Spock Web Console), // so let's just assume it's not there and continue return null; } GroovyShell shell = createShell(); try { return (DelegatingScript) shell.parse(file); } catch (IOException e) { throw new ConfigurationException("Error reading configuration script '%s'", location); } catch (CompilationFailedException e) { throw new ConfigurationException("Error compiling configuration script '%s'", location); } }
@Nullable private DelegatingScript loadScriptFromClassPathLocation(String location) { URL url = this.getClass().getClassLoader().getResource(location); if (url == null) return null; GroovyShell shell = createShell(); GroovyCodeSource codeSource = null; // try block below for compatibility pre/post Groovy 2.4.7 (see GROOVY-8126) try { codeSource = new GroovyCodeSource(url); } catch (Exception e) { throw new ConfigurationException("Error reading configuration script '%s'", location); } try { return (DelegatingScript) shell.parse(codeSource); } catch (CompilationFailedException e) { throw new ConfigurationException("Error compiling configuration script '%s'", location); } }
private Script compile(String input, GroovyShell groovyShell) throws Exception { TraversalTestHelper traverser = new TraversalTestHelper(); traverser.traverse(input, SourcePrinter.class, Boolean.FALSE); return groovyShell.parse(input); }
scriptText = scriptBody; script = shell.parse(PRELOADS + scriptText, scriptName); compiled = (Class<Script>) script.getClass();
script = (TypeCheckingDSL) shell.parse( new InputStreamReader(is, typeCheckingVisitor.getSourceUnit().getConfiguration().getSourceEncoding()) );
public void testExplicitReturnFromCRaSHScript() throws Exception { CompilerConfiguration config = new CompilerConfiguration(); config.setScriptBaseClass(GroovyScriptCommand.class.getName()); GroovyShell shell = new GroovyShell(config); Script script = shell.parse("return 'something'"); Field f = script.getClass().getDeclaredField("org_crsh_has_explicit_return"); assertTrue(Modifier.isPublic(f.getModifiers())); assertTrue(Modifier.isStatic(f.getModifiers())); }