alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (BatchGenerate.groovy)

This example Groovy source code file (BatchGenerate.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

batchgenerate, batchgenerate, file, file, io, testgenerator, testgenerator

The Groovy BatchGenerate.groovy source code

/**
 * @author Jeremy Rayner
 */
package org.codehaus.groovy.tck

import java.io.File;

class BatchGenerate {
    def generator;
    def srcDirPath;
    def targetDir;
    def srcEncoding;
    def srcs;
    def spew

    public BatchGenerate() {
        generator = new TestGenerator();
        // verbose = false;
        spew = true;
        srcDirPath = "./";
    }

    public void setSrcdirPath(String pathName) {
        if (spew) {println("srcDir:${pathName}") }
        srcDirPath = pathName;
    }

    public void setTargetDirectory(File destDir) {
        if (spew) { println("destDir:${destDir}") }
        targetDir = destDir;
    }

    public void setSourceEncoding(String encoding) {
        if (spew) { println("encoding:${encoding}") }
        srcEncoding = encoding;
    }

    public void addSources( File[] compileList ) {
        if (spew) { println("compileList:${compileList}") }
        srcs = compileList
    }

    public void setVerbose(boolean verbose) {
        spew = verbose
    }

    public void compile() {
        if (spew) { println("compile()") }


        for (src in srcs) {
            println( src )
            // mung the ${test.src.dir}/gls/ch14/s4 path into ${dest.dir}/gls/ch14/s4
            // first determine the relative path e.g. gls/ch14/s4
            def relativeSrcFilePathAndName = src.getAbsolutePath().substring(srcDirPath.length() + 1)
            def relativeSrcFileNameStartIndex = relativeSrcFilePathAndName.lastIndexOf(File.separator);
            def relativeOutputPath = ""
            if (relativeSrcFileNameStartIndex >= 0) {
                relativeOutputPath = relativeSrcFilePathAndName.substring(0,relativeSrcFileNameStartIndex);
            }

            // then determine the absolute output path
            def ghostOutputFile = new File(targetDir, relativeSrcFilePathAndName)
            def ghostOutputFilePath = ghostOutputFile.getAbsolutePath()
            def fileNameStartIndex = ghostOutputFilePath.lastIndexOf(File.separator);
            def realOutputPath = ghostOutputFilePath.substring(0,fileNameStartIndex);

            // mkdir if does not exist
            File directory = new File(realOutputPath)
            if (directory != null && !directory.exists()) {
                directory.mkdirs();
            }

            // generate a suitable java file to put there
            def fileStem = src.name.tokenize(".")[0]
            def targetFileName = "${fileStem}Test.java"
            def anOutputFile = new File(realOutputPath, targetFileName)

            System.out.println("generating " + targetFileName)
            def someOutputText = generator.generate(relativeOutputPath, targetDir, src.name,src.text);
            if (someOutputText != null && someOutputText != "") {
                anOutputFile.write(someOutputText);
            }
        }
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy BatchGenerate.groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.