use of groovy.util.CharsetToolkit in project groovy by apache.
the class Groovy method execute.
/**
* Load the file and then execute it
*/
@Override
public void execute() throws BuildException {
log.debug("execute()");
command = command.trim();
// process filesets
for (FileSet next : filesets) {
for (Resource res : next) {
if (src == null) {
src = res;
} else {
throw new BuildException("A single source resource must be provided!", getLocation());
}
}
}
if (src == null && command.length() == 0) {
throw new BuildException("Source does not exist!", getLocation());
}
if (src != null && !src.isExists()) {
throw new BuildException("Source resource does not exist!", getLocation());
}
if (outputEncoding == null || outputEncoding.isEmpty()) {
outputEncoding = Charset.defaultCharset().name();
}
try {
PrintStream out = System.out;
try {
if (output != null) {
log.verbose("Opening PrintStream to output file " + output);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output.getAbsolutePath(), append));
out = new PrintStream(bos, false, outputEncoding);
}
// then read groovy statements in from a resource using the src attribute
if (command == null || command.trim().length() == 0) {
Reader reader;
if (src instanceof FileResource) {
File file = ((FileResource) src).getFile();
createClasspath().add(new Path(getProject(), file.getParentFile().getCanonicalPath()));
if (encoding != null && !encoding.isEmpty()) {
reader = new LineNumberReader(new InputStreamReader(new FileInputStream(file), encoding));
} else {
reader = new CharsetToolkit(file).getReader();
}
} else {
if (encoding != null && !encoding.isEmpty()) {
reader = new InputStreamReader(new BufferedInputStream(src.getInputStream()), encoding);
} else {
reader = new InputStreamReader(new BufferedInputStream(src.getInputStream()), Charset.defaultCharset());
}
}
readCommandFromReader(reader);
} else {
if (src != null) {
log.info("Ignoring supplied resource as direct script text found");
}
}
if (command != null) {
execGroovy(command, out);
}
} finally {
if (out != null && out != System.out) {
out.close();
}
}
} catch (IOException e) {
throw new BuildException(e, getLocation());
}
log.verbose("Statements executed successfully");
}
use of groovy.util.CharsetToolkit in project groovy by apache.
the class GroovyDocTest method testFileEncoding.
@Test
public void testFileEncoding() throws Exception {
rule.executeTarget("testFileEncoding");
final File testfilesPackageDir = new File(tmpDir, "org/codehaus/groovy/tools/groovydoc/testfiles");
final String[] list = testfilesPackageDir.list((file, name) -> name.equals("DocumentedClass.html"));
File documentedClassHtmlDoc = new File(testfilesPackageDir, list[0]);
CharsetToolkit charsetToolkit = new CharsetToolkit(documentedClassHtmlDoc);
assertEquals("The generated groovydoc must be in 'UTF-16LE' file encoding.'", StandardCharsets.UTF_16LE, charsetToolkit.getCharset());
}
use of groovy.util.CharsetToolkit in project groovy-core by groovy.
the class GroovyDocTest method testFileEncoding.
public void testFileEncoding() throws Exception {
executeTarget("testFileEncoding");
final File testfilesPackageDir = new File(tmpDir, "org/codehaus/groovy/tools/groovydoc/testfiles");
System.err.println("testfilesPackageDir = " + testfilesPackageDir);
final String[] list = testfilesPackageDir.list(new FilenameFilter() {
public boolean accept(File file, String name) {
return name.equals("DocumentedClass.html");
}
});
File documentedClassHtmlDoc = new File(testfilesPackageDir, list[0]);
CharsetToolkit charsetToolkit = new CharsetToolkit(documentedClassHtmlDoc);
assertEquals("The generated groovydoc must be in 'UTF-16LE' file encoding.'", Charset.forName("UTF-16LE"), charsetToolkit.getCharset());
}
Aggregations