1 18 package org.apache.tools.ant.taskdefs.optional.jsp; 19 20 import java.io.File ; 22 import java.util.Date ; 23 import java.util.StringTokenizer ; 24 import java.util.Vector ; 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.DirectoryScanner; 27 import org.apache.tools.ant.Project; 28 import org.apache.tools.ant.taskdefs.Java; 29 import org.apache.tools.ant.taskdefs.MatchingTask; 30 import org.apache.tools.ant.types.Path; 31 32 79 80 public class WLJspc extends MatchingTask { 81 85 86 private File destinationDirectory; 87 88 89 private File sourceDirectory; 90 91 92 private String destinationPackage; 93 94 95 private Path compileClasspath; 96 97 99 private String pathToPackage = ""; 100 private Vector filesToDo = new Vector (); 101 102 106 public void execute() throws BuildException { 107 if (!destinationDirectory.isDirectory()) { 108 throw new BuildException("destination directory " 109 + destinationDirectory.getPath() + " is not valid"); 110 } 111 112 if (!sourceDirectory.isDirectory()) { 113 throw new BuildException("src directory " 114 + sourceDirectory.getPath() + " is not valid"); 115 } 116 117 if (destinationPackage == null) { 118 throw new BuildException("package attribute must be present.", 119 getLocation()); 120 } 121 122 123 pathToPackage 124 = this.destinationPackage.replace('.', File.separatorChar); 125 DirectoryScanner ds = super.getDirectoryScanner(sourceDirectory); 127 128 if (compileClasspath == null) { 130 compileClasspath = new Path(getProject()); 131 } 132 133 compileClasspath = compileClasspath.concatSystemClasspath(); 134 String [] files = ds.getIncludedFiles(); 135 136 Java helperTask = new Java(this); 141 helperTask.setFork(true); 142 helperTask.setClassname("weblogic.jspc"); 143 helperTask.setTaskName(getTaskName()); 144 String [] args = new String [12]; 145 146 File jspFile = null; 147 String parents = ""; 148 int j = 0; 149 args[j++] = "-d"; 151 args[j++] = destinationDirectory.getAbsolutePath().trim(); 152 args[j++] = "-docroot"; 153 args[j++] = sourceDirectory.getAbsolutePath().trim(); 154 args[j++] = "-keepgenerated"; args[j++] = "-compilerclass"; 158 args[j++] = "sun.tools.javac.Main"; 159 args[j++] = "-classpath"; 163 args[j++] = compileClasspath.toString(); 164 165 this.scanDir(files); 166 log("Compiling " + filesToDo.size() + " JSP files"); 167 168 for (int i = 0; i < filesToDo.size(); i++) { 169 String filename = (String ) filesToDo.elementAt(i); 174 jspFile = new File (filename); 175 args[j] = "-package"; 176 parents = jspFile.getParent(); 177 if ((parents != null) && (!("").equals(parents))) { 178 parents = this.replaceString(parents, File.separator, "_."); 179 args[j + 1] = destinationPackage + "." + "_" + parents; 180 } else { 181 args[j + 1] = destinationPackage; 182 } 183 184 185 args[j + 2] = sourceDirectory + File.separator + filename; 186 helperTask.clearArgs(); 187 188 for (int x = 0; x < j + 3; x++) { 189 helperTask.createArg().setValue(args[x]); 190 } 191 192 helperTask.setClasspath(compileClasspath); 193 if (helperTask.executeJava() != 0) { 194 log(filename + " failed to compile", Project.MSG_WARN); 195 } 196 } 197 } 198 199 200 201 205 public void setClasspath(Path classpath) { 206 if (compileClasspath == null) { 207 compileClasspath = classpath; 208 } else { 209 compileClasspath.append(classpath); 210 } 211 } 212 213 217 public Path createClasspath() { 218 if (compileClasspath == null) { 219 compileClasspath = new Path(getProject()); 220 } 221 return compileClasspath; 222 } 223 224 230 public void setSrc(File dirName) { 231 232 sourceDirectory = dirName; 233 } 234 235 241 public void setDest(File dirName) { 242 243 destinationDirectory = dirName; 244 } 245 246 251 public void setPackage(String packageName) { 252 253 destinationPackage = packageName; 254 } 255 256 261 protected void scanDir(String [] files) { 262 263 long now = (new Date ()).getTime(); 264 File jspFile = null; 265 String parents = null; 266 String pack = ""; 267 for (int i = 0; i < files.length; i++) { 268 File srcFile = new File (this.sourceDirectory, files[i]); 269 jspFile = new File (files[i]); 273 parents = jspFile.getParent(); 274 275 if ((parents != null) && (!("").equals(parents))) { 276 parents = this.replaceString(parents, File.separator, "_/"); 277 pack = pathToPackage + File.separator + "_" + parents; 278 } else { 279 pack = pathToPackage; 280 } 281 282 String filePath = pack + File.separator + "_"; 283 int startingIndex = files[i].lastIndexOf(File.separator) != -1 284 ? files[i].lastIndexOf(File.separator) + 1 : 0; 285 int endingIndex = files[i].indexOf(".jsp"); 286 if (endingIndex == -1) { 287 log("Skipping " + files[i] + ". Not a JSP", 288 Project.MSG_VERBOSE); 289 continue; 290 } 291 292 filePath += files[i].substring(startingIndex, endingIndex); 293 filePath += ".class"; 294 File classFile = new File (this.destinationDirectory, filePath); 295 296 if (srcFile.lastModified() > now) { 297 log("Warning: file modified in the future: " 298 + files[i], Project.MSG_WARN); 299 } 300 if (srcFile.lastModified() > classFile.lastModified()) { 301 filesToDo.addElement(files[i]); 302 log("Recompiling File " + files[i], Project.MSG_VERBOSE); 303 } 304 } 305 } 306 307 308 315 protected String replaceString(String inpString, String escapeChars, 316 String replaceChars) { 317 String localString = ""; 318 int numTokens = 0; 319 StringTokenizer st = new StringTokenizer (inpString, escapeChars, true); 320 numTokens = st.countTokens(); 321 for (int i = 0; i < numTokens; i++) { 322 String test = st.nextToken(); 323 test = (test.equals(escapeChars) ? replaceChars : test); 324 localString += test; 325 } 326 return localString; 327 } 328 } 329 | Popular Tags |