/** * Returns a new Reader on the underlying source object. */ public Reader getReader() throws IOException { if (stream != null) { Reader reader = new InputStreamReader(stream, configuration.getSourceEncoding()); stream = null; return reader; } return null; }
private void initSourceEncoding(CompilerConfiguration config) { sourceEncoding = config.getSourceEncoding(); if (null == sourceEncoding) { // Keep the same default source encoding with the one used by #parseClass(InputStream, String) // TODO should we use org.codehaus.groovy.control.CompilerConfiguration.DEFAULT_SOURCE_ENCODING instead? sourceEncoding = CharsetToolkit.getDefaultSystemCharset().name(); } }
/** * Returns a new Reader on the underlying source object. */ public Reader getReader() throws IOException { return new InputStreamReader(URLStreams.openUncachedStream(url), configuration.getSourceEncoding() ); }
public GroovyCodeSource run() { try { String scriptText = config.getSourceEncoding() != null ? IOGroovyMethods.getText(in, config.getSourceEncoding()) : IOGroovyMethods.getText(in); return new GroovyCodeSource(scriptText, fileName, "/groovy/script"); } catch (IOException e) { throw new RuntimeException("Impossible to read the content of the input stream for file named: " + fileName, e); } } });
/** * Returns a new Reader on the underlying source object. */ public Reader getReader() throws IOException { // we want to remove the BOM windows adds from a file if the encoding is UTF-8 // in other cases we depend on the charsets Charset cs = Charset.forName(configuration.getSourceEncoding()); InputStream in = new BufferedInputStream(new FileInputStream(file)); if (UTF8.name().equalsIgnoreCase(cs.name())) { in.mark(3); boolean hasBOM = true; try { int i = in.read(); hasBOM &= i == 0xEF; i = in.read(); hasBOM &= i == 0xBB; i = in.read(); hasBOM &= i == 0xFF; } catch (IOException ioe) { hasBOM= false; } if (!hasBOM) in.reset(); } return new InputStreamReader( in, cs ); }
/** * Parses the given file into a Java class capable of being run * * @param file the file name to parse * @return the main class defined in the given script */ public Class parseClass(File file) throws CompilationFailedException, IOException { return parseClass(new GroovyCodeSource(file, config.getSourceEncoding())); }
/** * 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 file is the file of the script (which is used to create the class name of the script) */ public Object evaluate(File file) throws CompilationFailedException, IOException { return evaluate(new GroovyCodeSource(file, config.getSourceEncoding())); }
/** * Get the class of the scriptName in question, so that you can instantiate * Groovy objects with caching and reloading. * * @param scriptName resource name pointing to the script * @return the loaded scriptName as a compiled class * @throws ResourceException if there is a problem accessing the script * @throws ScriptException if there is a problem parsing the script */ public Class loadScriptByName(String scriptName) throws ResourceException, ScriptException { URLConnection conn = rc.getResourceConnection(scriptName); String path = conn.getURL().toExternalForm(); ScriptCacheEntry entry = scriptCache.get(path); Class clazz = null; if (entry != null) clazz = entry.scriptClass; try { if (isSourceNewer(entry)) { try { String encoding = conn.getContentEncoding() != null ? conn.getContentEncoding() : config.getSourceEncoding(); String content = IOGroovyMethods.getText(conn.getInputStream(), encoding); clazz = groovyLoader.parseClass(content, path); } catch (IOException e) { throw new ResourceException(e); } } } finally { forceClose(conn); } return clazz; }
/** * Includes contents of another file, not as a template but as escaped text. * * @param templatePath the path to the other file * @throws IOException */ public void includeEscaped(String templatePath) throws IOException { URL resource = engine.resolveTemplate(templatePath); yield(ResourceGroovyMethods.getText(resource, engine.getCompilerConfiguration().getSourceEncoding())); }
/** * Includes contents of another file, not as a template but as unescaped text. * * @param templatePath the path to the other file * @throws IOException */ public void includeUnescaped(String templatePath) throws IOException { URL resource = engine.resolveTemplate(templatePath); yieldUnescaped(ResourceGroovyMethods.getText(resource, engine.getCompilerConfiguration().getSourceEncoding())); }
generationGoal = (File) options.get("stubDir"); boolean useJava5 = CompilerConfiguration.isPostJDK5(configuration.getTargetBytecode()); String encoding = configuration.getSourceEncoding(); stubGenerator = new JavaStubGenerator(generationGoal, false, useJava5, encoding); keepStubs = Boolean.TRUE.equals(options.get("keepStubs"));
public JavaStubCompilationUnit(final CompilerConfiguration config, final GroovyClassLoader gcl, File destDir) { super(config, null, gcl); assert config != null; Map options = config.getJointCompilationOptions(); if (destDir == null) { destDir = (File) options.get("stubDir"); } boolean useJava5 = CompilerConfiguration.isPostJDK5(configuration.getTargetBytecode()); String encoding = configuration.getSourceEncoding(); stubGenerator = new JavaStubGenerator(destDir, false, useJava5, encoding); addPhaseOperation(new PrimaryClassNodeOperation() { @Override public void call(SourceUnit source, GeneratorContext context, ClassNode node) throws CompilationFailedException { VariableScopeVisitor scopeVisitor = new VariableScopeVisitor(source); scopeVisitor.visitClass(node); new JavaAwareResolveVisitor(JavaStubCompilationUnit.this).startResolving(node, source); } }, Phases.CONVERSION); addPhaseOperation(new PrimaryClassNodeOperation() { @Override public void call(final SourceUnit source, final GeneratorContext context, final ClassNode node) throws CompilationFailedException { try { stubGenerator.generateClass(node); stubCount++; } catch (FileNotFoundException e) { source.addException(e); } } }, Phases.CONVERSION); }
GroovyShell shell = new GroovyShell(transformLoader, new Binding(), config); script = (TypeCheckingDSL) shell.parse( new InputStreamReader(is, typeCheckingVisitor.getSourceUnit().getConfiguration().getSourceEncoding()) ); } catch (CompilationFailedException e) {
setTargetBytecode(configuration.getTargetBytecode()); setDefaultScriptExtension(configuration.getDefaultScriptExtension()); setSourceEncoding(configuration.getSourceEncoding()); setTargetDirectory(configuration.getTargetDirectory()); Map<String, Object> jointCompilationOptions = configuration.getJointCompilationOptions();
public void testSetViaSystemProperties() { System.setProperty("groovy.warnings", "PaRaNoiA"); System.setProperty("groovy.output.verbose", "trUE"); System.setProperty("groovy.recompile.minimumInterval", "867892345"); assertEquals("PaRaNoiA", System.getProperty("groovy.warnings")); final CompilerConfiguration config = new CompilerConfiguration(System.getProperties()); assertEquals(WarningMessage.PARANOIA, config.getWarningLevel()); assertEquals(false, config.getDebug()); assertEquals(true, config.getVerbose()); assertEquals(10, config.getTolerance()); assertEquals(867892345, config.getMinimumRecompilationInterval()); assertNull(config.getScriptBaseClass()); assertEquals(getSystemEncoding(), config.getSourceEncoding()); assertEquals(getVMVersion(), config.getTargetBytecode()); assertEquals(false, config.getRecompileGroovySource()); { final List listCP = config.getClasspath(); assertNotNull(listCP); assertEquals(0, listCP.size()); } assertNotNull(config.getOutput()); assertNull(config.getTargetDirectory()); assertEquals(".groovy", config.getDefaultScriptExtension()); assertNull(config.getJointCompilationOptions()); assertNotNull(config.getPluginFactory()); }
public void testDefaultConstructor() { final CompilerConfiguration config = CompilerConfiguration.DEFAULT; assertEquals(WarningMessage.LIKELY_ERRORS, config.getWarningLevel()); assertEquals(Boolean.getBoolean("groovy.output.debug"), config.getDebug()); assertEquals(Boolean.getBoolean("groovy.output.verbose"), config.getVerbose()); assertEquals(false, config.getDebug()); assertEquals(false, config.getVerbose()); assertEquals(10, config.getTolerance()); assertEquals(100, config.getMinimumRecompilationInterval()); assertNull(config.getScriptBaseClass()); assertEquals(getSystemEncoding(), config.getSourceEncoding()); assertEquals(getVMVersion(), config.getTargetBytecode()); assertEquals(false, config.getRecompileGroovySource()); { final List listCP = config.getClasspath(); assertNotNull(listCP); assertEquals(0, listCP.size()); } assertNotNull(config.getOutput()); assertNull(config.getTargetDirectory()); assertEquals(".groovy", config.getDefaultScriptExtension()); assertNull(config.getJointCompilationOptions()); assertNotNull(config.getPluginFactory()); }
assertEquals(234, init.getMinimumRecompilationInterval()); assertEquals("blarg.foo.WhatSit", init.getScriptBaseClass()); assertEquals("LEAD-123", init.getSourceEncoding()); assertEquals(CompilerConfiguration.POST_JDK5, init.getTargetBytecode()); assertEquals(true, init.getRecompileGroovySource()); assertEquals(234, config.getMinimumRecompilationInterval()); assertEquals("blarg.foo.WhatSit", config.getScriptBaseClass()); assertEquals("LEAD-123", config.getSourceEncoding()); assertEquals(CompilerConfiguration.POST_JDK5, config.getTargetBytecode()); assertEquals(true, config.getRecompileGroovySource());
assertEquals(975, init.getMinimumRecompilationInterval()); assertEquals("", init.getScriptBaseClass()); assertEquals("Gutenberg", init.getSourceEncoding()); assertEquals(CompilerConfiguration.PRE_JDK5, init.getTargetBytecode()); assertEquals(false, init.getRecompileGroovySource()); assertEquals(975, config.getMinimumRecompilationInterval()); assertEquals("", config.getScriptBaseClass()); assertEquals("Gutenberg", config.getSourceEncoding()); assertEquals(CompilerConfiguration.PRE_JDK5, config.getTargetBytecode()); assertEquals(false, config.getRecompileGroovySource());
/** * Returns a new Reader on the underlying source object. */ public Reader getReader() throws IOException { if (stream != null) { Reader reader = new InputStreamReader(stream, configuration.getSourceEncoding()); stream = null; return reader; } return null; }