I recently had a need to get the weblogic jspc task working for a project I work on. I made quite a number of improvements that I believe are compatible with the existing optional task and I'd like to submit it back to the Ant project for your consideration. Let me know if there's anything else that I need to do to submit a patch proposal to Ant.
Dave Emerson
59d58
< import java.util.StringTokenizer;
66a66
> import org.apache.tools.ant.types.FileSet;
120a121,122
>     private String dirEncodingStr = "_"; // the string used to encode 
> directories under the package
>     private String fileEncodingStr = "_"; //the string used to encode class 
> file names
121a124,126
>     private FileSet aFileSet; // a single fileset element to use for 
> excludes. 
>     private boolean flagVerbose = false; // verbose jspc and javac. 
>     private String compilerEXE; // executable for a compiler - use for jikes 
> or others
124,126d128
<     private String pathToPackage = "";
<     private Vector filesToDo = new Vector();
<     
142,144c144,146
<         
<         pathToPackage
<             = this.destinationPackage.replace('.', File.separatorChar);
---
>         log("Source directory = " + sourceDirectory.getAbsolutePath().trim());
>         log("Destination directory = " + 
> destinationDirectory.getAbsolutePath().trim());
>         String[] filesToDo = scanDir();
145,146d146
<         // get all the files in the sourceDirectory
<         DirectoryScanner ds = super.getDirectoryScanner(sourceDirectory);
152d151
<         
154d152
<         String[] files = ds.getIncludedFiles();
164c162
<         String[] args = new String[12];
---
>         Vector args = new Vector();
168c166
<         int j = 0;
---
> 
170,174c168,172
<         args[j++] = "-d";
<         args[j++] = destinationDirectory.getAbsolutePath().trim(); 
<         args[j++] = "-docroot";
<         args[j++] = sourceDirectory.getAbsolutePath().trim();
<         args[j++] = "-keepgenerated";  //TODO: Parameterise ??
---
>         args.add("-d");
>         args.add(destinationDirectory.getAbsolutePath().trim()); 
>         args.add("-docroot");
>         args.add(sourceDirectory.getAbsolutePath().trim());
>         args.add("-keepgenerated");  //TODO: Parameterise ??
174a173,177
>         
>         if (flagVerbose){
>             args.add("-verbose");  //TODO: Parameterise ??
>             args.add("-verboseJavac");  //TODO: Parameterise ??
>         }
177,178c180,181
<         args[j++] =  "-compilerclass";
<         args[j++] = "sun.tools.javac.Main";
---
>         if (null != compilerEXE) {
>             args.add("-compiler");
178a182,188
>             args.add(compilerEXE);
>         }
>         else {
>             args.add("-compilerclass");
>             args.add("sun.tools.javac.Main");
>         }
> 
182,183c192,193
<         args[j++] = "-classpath";
<         args[j++] = compileClasspath.toString();
---
>         args.add("-classpath");
>         args.add(compileClasspath.toString());
183a194,203
> //        args.add("-bootclasspath");
> //        args.add(compileClasspath.toString());
> 
>         args.add("-package");
>         args.add(destinationPackage);
>         
>         for (int i = 0; i < filesToDo.length; i++) {
>             if (null != filesToDo[i]) {
>                 String filename = sourceDirectory + File.separator + 
> filesToDo[i];
>                 log("Compiling " + filename);
185,205d204
<         this.scanDir(files);
<         log("Compiling " + filesToDo.size() + " JSP files");
<             
<         for (int i = 0; i < filesToDo.size(); i++) {
<             //XXX
<             // All this to get package according to weblogic standards
<             // Can be written better... this is too hacky! 
<             // Careful.. similar code in scanDir , but slightly different!!
<             String filename = (String) filesToDo.elementAt(i);
<             jspFile = new File(filename);
<             args[j] = "-package";
<             parents = jspFile.getParent();
<             if ((parents != null)  && (!("").equals(parents))) {
<                 parents =  this.replaceString(parents, File.separator, "_.");
<                 args[j + 1] = destinationPackage + "." + "_" + parents;
<             } else {
<                 args[j + 1] = destinationPackage;
<             }
<             
<             
<             args[j + 2] =  sourceDirectory + File.separator + filename;
207,208c206,207
<             
<             for (int x = 0; x < j + 3; x++) {
---
>                 for (int x = 0; x < args.size(); x++) {
>                     helperTask.createArg().setValue((String)args.get(x));
209d207
<                 helperTask.createArg().setValue(args[x]);
210a209
>                 helperTask.createArg().setValue(filename);
218c217
< 
---
>     }
242a242,245
>     public void addFileSet(FileSet fileset) {
>         aFileSet = fileset;
>     }
>     
250d252
<         
254,256c256,258
<      /**
<      * Set the directory containing the source jsp's
<      * 
---
>     
>     /** Getter for property destinationDirectory.
>      * @return Value of property destinationDirectory.
258d259
<      * @param dirName the directory containg the source jsp's
260c261
<     public void setDest(File dirName) {
---
>     public File getDest() {
260a262,263
>         return destinationDirectory;
>     }
262c265
<         destinationDirectory = dirName;
---
>     /** Setter for property destinationDirectory.
262a266,270
>      * @param destinationDirectory New value of property destinationDirectory.
>      *
>      */
>     public void setDest(File destinationDirectory) {
>         this.destinationDirectory = destinationDirectory;
271d278
<         
274a282,340
>     /** Get the string used to encode directory names
>      *  under the destination package.
>      *
>      */
>     public java.lang.String getDirEncodingStr() {
>         return dirEncodingStr;
>     }
>     
>     /** Set the string used to encode directory names
>      *  under the destination package.
>      *
>      */
>     public void setDirEncodingStr(String dirEncodingStr) {
>         this.dirEncodingStr = dirEncodingStr;
>     }
>     
>     /** Get the string used to encode class file names.
>      */
>     public java.lang.String getFileEncodingStr() {
>         return fileEncodingStr;
>     }
>     
>     /** Set the string used to encode class file names.
>      *  Weblogic varies the string used to encode class file
>      *  names depending on the version and service pack. This
>      *  defaults to "_" and would typically be set to "__"
>      *  if your release of weblogic uses that instead.
>      */
>     public void setFileEncodingStr(String fileEncodingStr) {
>         this.fileEncodingStr = fileEncodingStr;
>     }
> 
>     /** Get property flagVerbose.
>      */
>     public boolean getVerbose() {
>         return flagVerbose;
>     }
>     
>     /** Sets both verbose jspc and javac on for weblogic.jspc.
>      */
>     public void setVerbose(boolean flagVerbose) {
>         this.flagVerbose = flagVerbose;
>     }
>     
>     /** Get name of an external compiler to be called by weblogic.jspc.
>      * @return compilerEXE External compiler (i.e jikes.exe or others).
>      *
>      */
>     public String getCompilerEXE() {
>         return compilerEXE;
>     }
>     
>     /** Set name of an external compiler to be called by weblogic.jspc.
>      * @param compilerEXE External compiler (i.e jikes.exe or others).
>      *
>      */
>     public void setCompilerEXE(String compilerEXE) {
>         this.compilerEXE = compilerEXE;
>     }
275a342
>     protected String[] scanDir() {
277c344
<     protected void scanDir(String files[]) {
---
>         // get all the files to compare
277a345,355
>         DirectoryScanner dsSource;
>         if (null == aFileSet)
>            dsSource = super.getDirectoryScanner(sourceDirectory);
>         else
>            dsSource = aFileSet.getDirectoryScanner(getProject());
> 
>         String[] inclSource = {"**\\*.jsp"};
>         dsSource.setIncludes(inclSource);
>         dsSource.setCaseSensitive(true);
>         dsSource.scan();
>         String[] files = dsSource.getIncludedFiles();
281,282c359,360
<         String parents = null;
<         String pack = "";
---
>         File classFile = null;
>         String destRelPath = null;
282a361
> 
284,296d362
<             File srcFile = new File(this.sourceDirectory, files[i]);
<             //XXX
<             // All this to convert source to destination directory according 
to weblogic standards
<             // Can be written better... this is too hacky!
<             jspFile = new File(files[i]);
<             parents = jspFile.getParent();
< 
<             if ((parents != null)  && (!("").equals(parents))) {
<                 parents =  this.replaceString(parents, File.separator, "_/");
<                 pack = pathToPackage + File.separator + "_" + parents;
<             } else {
<                 pack = pathToPackage;
<             }
298,304c364,370
<             String filePath = pack + File.separator + "_";
<             int startingIndex 
<                 = files[i].lastIndexOf(File.separator) != -1 ? 
files[i].lastIndexOf(File.separator) + 1 : 0;
<             int endingIndex = files[i].indexOf(".jsp");
<             if (endingIndex == -1) {
<                 log("Skipping " + files[i] + ". Not a JSP", 
<                     Project.MSG_VERBOSE);
---
>             int count = countMatches(files[i], File.separator);
>             if (0 == count){
>                 destRelPath = getFileEncodingStr() + files[i];
>             }
>             else if (1 == count) {
>                 destRelPath = getDirEncodingStr() + 
>                     replace(files[i], File.separator, File.separator + 
> getFileEncodingStr(), -1);
305d370
<                 continue;
306a372,374
>             else {
>                 destRelPath = getDirEncodingStr() + 
>                     replace(files[i], File.separator, File.separator + 
> getDirEncodingStr(), -1);
308,310c376,378
<             filePath += files[i].substring(startingIndex, endingIndex);
<             filePath += ".class";
<             File classFile = new File(this.destinationDirectory, filePath);
---
>                 String strSearch = File.separator + getDirEncodingStr();
>                 int idxStart = destRelPath.lastIndexOf(strSearch);
>                 int idxEnd = idxStart + strSearch.length();
310a379,395
>                 destRelPath = overlayString(destRelPath,
>                     File.separator + getFileEncodingStr(),
>                     idxStart,idxEnd
>                     );
>             }
>             destRelPath = replace(destRelPath, ".jsp", ".class", 1);
>             classFile = new File(this.destinationDirectory + File.separator + 
> destinationPackage, destRelPath);
>             if (classFile.exists()){
>                 jspFile = new File(this.sourceDirectory, files[i]);
>                 if (jspFile.lastModified() <= classFile.lastModified()) {
>                     log("Class " + classFile + " is up to date, skipping.", 
> Project.MSG_VERBOSE);
>                     files[i] = null;
>                 }
>             }
>         }
>         return files;
>     }
312,314c397,399
<             if (srcFile.lastModified() > now) {
<                 log("Warning: file modified in the future: " +
<                     files[i], Project.MSG_WARN);
---
> // These methods were shamelessly "stolen" from 
> org.apache.commons.lang.StringUtils
> // to avoid a dependancy on the package for just these few methods.
>     public static String replace(String text, String repl, String with,
314a400,402
>                                  int max) {
>         if (text == null) {
>             return null;
316,319c404,407
<             if (srcFile.lastModified() > classFile.lastModified()) {
<                //log("Files are" + srcFile.getAbsolutePath()+" " 
+classFile.getAbsolutePath());
<                 filesToDo.addElement(files[i]);
<                 log("Recompiling File " + files[i], Project.MSG_VERBOSE);
---
> 
>         StringBuffer buf = new StringBuffer(text.length());
>         int start = 0, end = 0;
>         while ((end = text.indexOf(repl, start)) != -1) {
319a408,412
>             buf.append(text.substring(start, end)).append(with);
>             start = end + repl.length();
> 
>             if (--max == 0) {
>                 break;
321a415,416
>         buf.append(text.substring(start));
>         return buf.toString();
323a419,425
>     public static String overlayString(String text, String overlay, int 
> start, int end) {
>         return new StringBuffer(start + overlay.length() + text.length() - 
> end + 1)
>             .append(text.substring(0, start))
>             .append(overlay)
>             .append(text.substring(end))
>             .toString();
>     }
325,327c427,429
<     protected String replaceString(String inpString, String escapeChars,
<                                    String replaceChars) {
<         String localString = "";
---
>     public static int countMatches(String str, String sub) {
>         if (str == null) {
>             return 0;
328,334d429
<         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 += test;
336c431
<         return localString;
---
>         int count = 0;
336a432,435
>         int idx = 0;
>         while ((idx = str.indexOf(sub, idx)) != -1) {
>             count++;
>             idx += sub.length();
337a437,439
>         return count;
>     }
>     

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to