FileDocCategorySizeDatePackage
JspC.javaAPI DocApache Ant 1.7019892Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.taskdefs.optional.jsp

JspC

public class JspC extends org.apache.tools.ant.taskdefs.MatchingTask
Runs a JSP compiler.

This task takes the given jsp files and compiles them into java files. It is then up to the user to compile the java files into classes.

The task requires the srcdir and destdir attributes to be set. This Task is a MatchingTask, so the files to be compiled can be specified using includes/excludes attributes or nested include/exclude elements. Optional attributes are verbose (set the verbosity level passed to jasper), package (name of the destination package for generated java classes and classpath (the classpath to use when running the jsp compiler).

This task supports the nested elements classpath (A Path) and classpathref (A Reference) which can be used in preference to the attribute classpath, if the jsp compiler is not already in the ant classpath.

Usage

<jspc srcdir="${basedir}/src/war"
destdir="${basedir}/gensrc"
package="com.i3sp.jsp"
verbose="9">
<include name="**\/*.jsp" />
</jspc>

Large Amount of cutting and pasting from the Javac task...

since
1.5

Fields Summary
private org.apache.tools.ant.types.Path
classpath
private org.apache.tools.ant.types.Path
compilerClasspath
private org.apache.tools.ant.types.Path
src
private File
destDir
private String
packageName
private String
compilerName
name of the compiler to use
private String
iepluginid
-ieplugin <clsid> Java Plugin classid for Internet Explorer
private boolean
mapped
private int
verbose
protected Vector
compileList
Vector
javaFiles
protected boolean
failOnError
flag to control action on execution trouble
private File
uriroot
-uriroot <dir> The root directory that uri files should be resolved against,
private File
webinc
-webinc <file> Creates partial servlet mappings for the -webapp option
private File
webxml
-webxml <file> Creates a complete web.xml when using the -webapp option.
protected WebAppParameter
webApp
web apps
private static final String
FAIL_MSG
Constructors Summary
Methods Summary
public voidaddWebApp(org.apache.tools.ant.taskdefs.optional.jsp.JspC$WebAppParameter webappParam)
Adds a single webapp.

param
webappParam add a web app parameter
throws
BuildException if more than one webapp is specified.

        //demand create vector of filesets
        if (webApp == null) {
            webApp = webappParam;
        } else {
            throw new BuildException("Only one webapp can be specified");
        }
    
public org.apache.tools.ant.types.PathcreateClasspath()
Adds a path to the classpath.

return
a path to be configured.

        if (classpath == null) {
            classpath = new Path(getProject());
        }
        return classpath.createPath();
    
public org.apache.tools.ant.types.PathcreateCompilerclasspath()
Support nested compiler classpath, used to locate compiler adapter

return
a path to be configured.

        if (compilerClasspath == null) {
            compilerClasspath = new Path(getProject());
        }
        return compilerClasspath.createPath();
    
public voiddeleteEmptyJavaFiles()
delete any java output files that are empty this is to get around a little defect in jasper: when it fails, it leaves incomplete files around.

        if (javaFiles != null) {
            Enumeration e = javaFiles.elements();
            while (e.hasMoreElements()) {
                File file = (File) e.nextElement();
                if (file.exists() && file.length() == 0) {
                    log("deleting empty output file " + file);
                    file.delete();
                }
            }
        }
    
private voiddoCompilation(org.apache.tools.ant.taskdefs.optional.jsp.compilers.JspCompilerAdapter compiler)
do the compile

        // now we need to populate the compiler adapter
        compiler.setJspc(this);

        // finally, lets execute the compiler!!
        if (!compiler.execute()) {
            if (failOnError) {
                throw new BuildException(FAIL_MSG, getLocation());
            } else {
                log(FAIL_MSG, Project.MSG_ERR);
            }
        }
    
public voidexecute()
execute by building up a list of files that have changed and hand them off to a jsp compiler

throws
BuildException on error.


        // make sure that we've got a destdir
        if (destDir == null) {
            throw new BuildException("destdir attribute must be set!",
                                     getLocation());
        }

        if (!destDir.isDirectory()) {
            throw new BuildException("destination directory \"" + destDir
                    + "\" does not exist or is not a directory", getLocation());
        }

        File dest = getActualDestDir();

        //bind to a compiler
        JspCompilerAdapter compiler =
            JspCompilerAdapterFactory.getCompiler(compilerName, this,
                getProject().createClassLoader(compilerClasspath));

        //if we are a webapp, hand off to the compiler, which had better handle it
        if (webApp != null) {
            doCompilation(compiler);
            return;
        }

        // make sure that we've got a srcdir
        if (src == null) {
            throw new BuildException("srcdir attribute must be set!",
                                     getLocation());
        }
        String [] list = src.list();
        if (list.length == 0) {
            throw new BuildException("srcdir attribute must be set!",
                    getLocation());
        }


        // if the compiler does its own dependency stuff, we just call it right now
        if (compiler.implementsOwnDependencyChecking()) {
            doCompilation(compiler);
            return;
        }

        //the remainder of this method is only for compilers that need their dependency work done
        JspMangler mangler = compiler.createMangler();

        // scan source directories and dest directory to build up both copy
        // lists and compile lists
        resetFileLists();
        int filecount = 0;
        for (int i = 0; i < list.length; i++) {
            File srcDir = getProject().resolveFile(list[i]);
            if (!srcDir.exists()) {
                throw new BuildException("srcdir \"" + srcDir.getPath()
                    + "\" does not exist!", getLocation());
            }
            DirectoryScanner ds = this.getDirectoryScanner(srcDir);
            String[] files = ds.getIncludedFiles();
            filecount = files.length;
            scanDir(srcDir, dest, mangler, files);
        }

        // compile the source files

        log("compiling " + compileList.size() + " files", Project.MSG_VERBOSE);

        if (compileList.size() > 0) {

            log("Compiling " + compileList.size() + " source file"
                + (compileList.size() == 1 ? "" : "s")
                + " to "
                + dest);
            doCompilation(compiler);

        } else {
            if (filecount == 0) {
                log("there were no files to compile", Project.MSG_INFO);
            } else {
                log("all files are up to date", Project.MSG_VERBOSE);
            }
        }
    
private java.io.FilegetActualDestDir()
calculate where the files will end up: this is destDir or it id destDir + the package name

        File dest = null;
        if (packageName == null) {
            dest = destDir;
        } else {
            String path = destDir.getPath() + File.separatorChar
                + packageName.replace('.", File.separatorChar);
            dest = new File(path);
        }
        return dest;
    
public org.apache.tools.ant.types.PathgetClasspath()
Get the classpath.

return
the classpath.

        return classpath;
    
public java.util.VectorgetCompileList()
get the list of files to compile

return
the list of files.

        return compileList;
    
public org.apache.tools.ant.types.PathgetCompilerclasspath()
get the classpath used to find the compiler adapter

return
the compiler classpath.

        return compilerClasspath;
    
public java.io.FilegetDestdir()
Get the destination directory.

return
the directory.

        return destDir;
    
public booleangetFailonerror()
Gets the failonerror flag.

return
the flag.

        return failOnError;
    
public java.lang.StringgetIeplugin()
Get the IE CLASSID value.

return
the value.

        return iepluginid;
    
public java.lang.StringgetPackage()
Get the name of the package.

return
the package.

        return packageName;
    
public org.apache.tools.ant.types.PathgetSrcDir()
Get the source dir.

return
the source path.

        return src;
    
public java.io.FilegetUribase()
Get the uri base value.

return
the value.

        return uriroot;
    
public java.io.FilegetUriroot()
Get the uri root value.

return
the value.

        return uriroot;
    
public intgetVerbose()
Get the verbose level.

return
the level.

        return verbose;
    
public org.apache.tools.ant.taskdefs.optional.jsp.JspC$WebAppParametergetWebApp()
Get the web app.

return
the web app attribute.

        return webApp;
    
public java.io.FilegetWebinc()
Get the webinc attribute.

return
the webinc attribute.

        return this.webinc;
    
public java.io.FilegetWebxml()
Filename for web.xml.

return
The filename for web.xml.

        return this.webxml;
    
private booleanisCompileNeeded(java.io.File srcFile, java.io.File javaFile)
Test whether or not compilation is needed. A return value of true means yes, false means our tests do not indicate this, but as the TLDs are not used for dependency checking this is not guaranteed. The current tests are
  1. no dest file
  2. dest file out of date w.r.t source
  3. dest file zero bytes long

param
srcFile JSP source file
param
javaFile JSP dest file
return
true if a compile is definately needed.

        boolean shouldCompile = false;
        if (!javaFile.exists()) {
            shouldCompile = true;
            log("Compiling " + srcFile.getPath()
                + " because java file " + javaFile.getPath()
                + " does not exist", Project.MSG_VERBOSE);
            } else {
                if (srcFile.lastModified() > javaFile.lastModified()) {
                    shouldCompile = true;
                    log("Compiling " + srcFile.getPath()
                        + " because it is out of date with respect to "
                        + javaFile.getPath(),
                        Project.MSG_VERBOSE);
                } else {
                    if (javaFile.length() == 0) {
                        shouldCompile = true;
                        log("Compiling " + srcFile.getPath()
                            + " because java file " + javaFile.getPath()
                            + " is empty", Project.MSG_VERBOSE);
                    }
                }
        }
        return shouldCompile;
    
public booleanisMapped()
If true, generate separate write() calls for each HTML line in the JSP.

return
mapping status

        return mapped;
    
protected java.io.FilemapToJavaFile(JspMangler mangler, java.io.File srcFile, java.io.File srcDir, java.io.File dest)
get a filename from our jsp file.

param
mangler the jsp filename managler.
param
srcFile the source file.
param
srcDir the source directory.
param
dest the destination directory.
return
the filename.
todo
support packages and subdirs

        if (!srcFile.getName().endsWith(".jsp")) {
            return null;
        }
        String javaFileName = mangler.mapJspToJavaName(srcFile);
//        String srcFileDir=srcFile.getParent();
        return new File(dest, javaFileName);
    
protected voidresetFileLists()
Clear the list of files to be compiled and copied..

        compileList.removeAllElements();
    
protected voidscanDir(java.io.File srcDir, java.io.File dest, JspMangler mangler, java.lang.String[] files)
Scans the directory looking for source files to be compiled. The results are returned in the class variable compileList

param
srcDir the source directory.
param
dest the destination directory.
param
mangler the jsp filename mangler.
param
files the file names to mangle.


        long now = (new Date()).getTime();

        for (int i = 0; i < files.length; i++) {
            String filename = files[i];
            File srcFile = new File(srcDir, filename);
            File javaFile = mapToJavaFile(mangler, srcFile, srcDir, dest);
            if (javaFile == null) {
                continue;
            }

            if (srcFile.lastModified() > now) {
                log("Warning: file modified in the future: " + filename,
                        Project.MSG_WARN);
            }
            boolean shouldCompile = false;
            shouldCompile = isCompileNeeded(srcFile, javaFile);
            if (shouldCompile) {
               compileList.addElement(srcFile.getAbsolutePath());
               javaFiles.addElement(javaFile);
            }
        }
    
public voidsetClasspath(org.apache.tools.ant.types.Path cp)
Set the classpath to be used for this compilation.

param
cp the path to be used.

        if (classpath == null) {
            classpath = cp;
        } else {
            classpath.append(cp);
        }
    
public voidsetClasspathRef(org.apache.tools.ant.types.Reference r)
Adds a reference to a classpath defined elsewhere

param
r a reference to a classpath.

        createClasspath().setRefid(r);
    
public voidsetCompiler(java.lang.String compiler)
Class name of a JSP compiler adapter.

param
compiler the compiler class name.

        this.compilerName = compiler;
    
public voidsetCompilerclasspath(org.apache.tools.ant.types.Path cp)
Set the classpath to be used to find this compiler adapter

param
cp the compiler classpath.

        if (compilerClasspath == null) {
            compilerClasspath = cp;
        } else {
            compilerClasspath.append(cp);
        }
    
public voidsetDestdir(java.io.File destDir)
Set the destination directory into which the JSP source files should be compiled.

param
destDir the destination directory.

        this.destDir = destDir;
    
public voidsetFailonerror(boolean fail)
Whether or not the build should halt if compilation fails. Defaults to true.

param
fail a boolean value.

        failOnError = fail;
    
public voidsetIeplugin(java.lang.String iepluginid)
Java Plugin CLASSID for Internet Explorer

param
iepluginid the id to use.

        this.iepluginid = iepluginid;
    
public voidsetMapped(boolean mapped)
If true, generate separate write() calls for each HTML line in the JSP.

param
mapped a boolean value.

        this.mapped = mapped;
    
public voidsetPackage(java.lang.String pkg)
Set the name of the package the compiled jsp files should be in.

param
pkg the name of the package.

        this.packageName = pkg;
    
public voidsetSrcDir(org.apache.tools.ant.types.Path srcDir)
Set the path for source JSP files.

param
srcDir the source path.


    // CheckStyle:VisibilityModifier ON

                     
        
        if (src == null) {
            src = srcDir;
        } else {
            src.append(srcDir);
        }
    
public voidsetUribase(java.io.File uribase)
The URI context of relative URI references in the JSP pages. If it does not exist then it is derived from the location of the file relative to the declared or derived value of uriroot.

param
uribase The new Uribase value

        log("Uribase is currently an unused parameter", Project.MSG_WARN);
    
public voidsetUriroot(java.io.File uriroot)
The root directory that uri files should be resolved against. (Default is the directory jspc is invoked from)

param
uriroot The new Uribase value

        this.uriroot = uriroot;
    
public voidsetVerbose(int i)
Set the verbose level of the compiler

param
i the verbose level to use.

        verbose = i;
    
public voidsetWebinc(java.io.File webinc)
output filename for the fraction of web.xml that lists servlets.

param
webinc The new Webinc value

        this.webinc = webinc;
    
public voidsetWebxml(java.io.File webxml)
Filename for web.xml.

param
webxml The new Webxml value

        this.webxml = webxml;