// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   WL6Jspc.java

package org.apache.tools.ant.taskdefs.optional.jsp;

import java.io.File;
import java.io.PrintStream;
import java.util.*;
import org.apache.tools.ant.*;
import org.apache.tools.ant.taskdefs.Java;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.tools.ant.types.Path;

public class WL6Jspc extends MatchingTask
{

    public WL6Jspc()
    {
        pathToPackage = "";
        filesToDo = new Vector();
    }

    public void execute()
        throws BuildException
    {
        if(!destinationDirectory.isDirectory())
            throw new BuildException("destination directory " + destinationDirectory.getPath() + " is not valid");
        if(!sourceDirectory.isDirectory())
            throw new BuildException("src directory " + sourceDirectory.getPath() + " is not valid");
        if(destinationPackage == null)
            throw new BuildException("package attribute must be present.", super.location);
        String systemClassPath = System.getProperty("java.class.path");
        pathToPackage = destinationPackage.replace('.', File.separatorChar);
        DirectoryScanner ds = super.getDirectoryScanner(sourceDirectory);
        if(compileClasspath == null)
            compileClasspath = new Path(super.project);
        compileClasspath.append(Path.systemClasspath);
        String files[] = ds.getIncludedFiles();
        Java helperTask = (Java)super.project.createTask("java");
        helperTask.setFork(true);
        helperTask.setClassname("weblogic.jspc");
        helperTask.setTaskName(getTaskName());
	int argsnum = 14;
        String args[] = new String[argsnum];
        File jspFile = null;
        String parents = "";
        String arg = "";
        int j = 0;
	if (webAppDirIsSet == true) {
          args[j++] = "-webapp";
          args[j++] = webAppDir.getAbsolutePath().trim();
	}
        args[j++] = "-d";
        args[j++] = destinationDirectory.getAbsolutePath().trim();
        args[j++] = "-docroot";
        args[j++] = sourceDirectory.getAbsolutePath().trim();
        args[j++] = "-keepgenerated";
        args[j++] = "-compilerclass";
        args[j++] = "sun.tools.javac.Main";
        args[j++] = "-classpath";
        args[j++] = compileClasspath.toString();
        scanDir(files);
        for(int i = 0; i < filesToDo.size(); i++)
        {
            jspFile = new File((String)filesToDo.elementAt(i));
            args[j] = "-package";
            parents = jspFile.getParent();
            if(parents != null && !"".equals(parents))
            {
                parents = replaceString(parents, File.separator, "_.");
                // GOLLOT args[j + 1] = destinationPackage + "." + "_" + parents;
		// Do not add parents directory with new jspc 6.x
                args[j + 1] = destinationPackage ;
            } else
            {
                args[j + 1] = destinationPackage;
            }
            args[j + 2] = sourceDirectory + File.separator + (String)filesToDo.elementAt(i);
            arg = "";
            for(int x = 0; x < argsnum; x++)
                arg = arg + " " + args[x];

            helperTask.clearArgs();
            helperTask.setArgs(arg);
            helperTask.setClasspath(compileClasspath);
            if(helperTask.executeJava() != 0)
                log(files[i] + " failed to compile", 1);
        }

    }

    public void setClasspath(Path classpath)
    {
        if(compileClasspath == null)
            compileClasspath = classpath;
        else
            compileClasspath.append(classpath);
    }

    public Path createClasspath()
    {
        if(compileClasspath == null)
            compileClasspath = new Path(super.project);
        return compileClasspath;
    }

    public void setSrc(File dirName)
    {
        sourceDirectory = dirName;
    }

    public void setDest(File dirName)
    {
        destinationDirectory = dirName;
    }

    public void setPackage(String packageName)
    {
        destinationPackage = packageName;
    }

    public void setWebapp(File dirName)
    {
        webAppDir = dirName;
	webAppDirIsSet = true;
    }

    protected void scanDir(String files[])
    {
        long now = (new Date()).getTime();
        File jspFile = null;
        String parents = null;
        String pack = "";
        for(int i = 0; i < files.length; i++)
        {
            File srcFile = new File(sourceDirectory, files[i]);
            jspFile = new File(files[i]);
            parents = jspFile.getParent();
            int loc = 0;
            if(parents != null && !"".equals(parents))
            {
                parents = replaceString(parents, File.separator, "_/");
                pack = pathToPackage + File.separator + "_" + parents;
            } else
            {
                pack = pathToPackage;
            }
            String filePath = pack + File.separator + "_";
            int startingIndex = files[i].lastIndexOf(File.separator) == -1 ? 0 : files[i].lastIndexOf(File.separator) + 1;
            int endingIndex = files[i].indexOf(".jsp");
            if(endingIndex == -1)  {
                // GOLLOT : break;
                continue;
	    }
            filePath = filePath + files[i].substring(startingIndex, endingIndex);
            filePath = filePath + ".class";
            File classFile = new File(destinationDirectory, filePath);
            if(srcFile.lastModified() > now)
                log("Warning: file modified in the future: " + files[i], 1);
            if(srcFile.lastModified() > classFile.lastModified())
            {
                filesToDo.addElement(files[i]);
                log("Recompiling File " + files[i], 3);
            }
        }

    }

    protected String replaceString(String inpString, String escapeChars, String replaceChars)
    {
        String localString = "";
        int numTokens = 0;
        StringTokenizer st = new StringTokenizer(inpString, escapeChars, true);
        numTokens = st.countTokens();
        for(int i = 0; i < numTokens; i++)
        {
            String test = st.nextToken();
            test = test.equals(escapeChars) ? replaceChars : test;
            localString = localString + test;
        }

        return localString;
    }

    private File destinationDirectory;
    private File sourceDirectory;
    private String destinationPackage;
    private Path compileClasspath;
    private String pathToPackage;
    private File webAppDir;
    private boolean webAppDirIsSet = false;
    private Vector filesToDo;
}

