Search in sources :

Example 1 with CharsetToolkit

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");
}
Also used : Path(org.apache.tools.ant.types.Path) PrintStream(java.io.PrintStream) FileSet(org.apache.tools.ant.types.FileSet) InputStreamReader(java.io.InputStreamReader) CharsetToolkit(groovy.util.CharsetToolkit) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) LineNumberReader(java.io.LineNumberReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) LineNumberReader(java.io.LineNumberReader) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) BuildException(org.apache.tools.ant.BuildException) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File)

Example 2 with CharsetToolkit

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());
}
Also used : CharsetToolkit(groovy.util.CharsetToolkit) File(java.io.File) Test(org.junit.Test)

Example 3 with CharsetToolkit

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());
}
Also used : FilenameFilter(java.io.FilenameFilter) CharsetToolkit(groovy.util.CharsetToolkit) File(java.io.File)

Aggregations

CharsetToolkit (groovy.util.CharsetToolkit)3 File (java.io.File)3 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 FilenameFilter (java.io.FilenameFilter)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 LineNumberReader (java.io.LineNumberReader)1 PrintStream (java.io.PrintStream)1 Reader (java.io.Reader)1 BuildException (org.apache.tools.ant.BuildException)1 FileSet (org.apache.tools.ant.types.FileSet)1 Path (org.apache.tools.ant.types.Path)1 Resource (org.apache.tools.ant.types.Resource)1 FileResource (org.apache.tools.ant.types.resources.FileResource)1 Test (org.junit.Test)1