1 package org.apache.beehive.controls.runtime.generator; 2 3 20 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.DirectoryScanner; 23 import org.apache.tools.ant.taskdefs.Javac; 24 import org.apache.tools.ant.types.Commandline; 25 import org.apache.tools.ant.types.Path; 26 import org.apache.tools.ant.util.FileUtils; 27 import org.apache.tools.ant.util.GlobPatternMapper; 28 import org.apache.tools.ant.util.SourceFileScanner; 29 30 import java.io.File ; 31 import java.io.IOException ; 32 import java.util.StringTokenizer ; 33 import java.util.Vector ; 34 35 49 public class AptTask extends Javac 50 { 51 56 public void setSrcExtensions(String srcExts) 57 { 58 StringTokenizer tok = new StringTokenizer (srcExts, ","); 59 while (tok.hasMoreTokens()) 60 _srcExts.add(tok.nextToken()); 61 } 62 63 68 public void setProcessorOptions(String processorOptions) 69 { 70 StringTokenizer tok = new StringTokenizer (processorOptions, ","); 71 while (tok.hasMoreTokens()) 72 _processorOptions.add(tok.nextToken()); 73 } 74 75 79 public void setGendir(File genDir) 80 { 81 _genDir = genDir; 82 } 83 84 88 public void setNocompile(boolean nocompile) 89 { 90 _nocompile = nocompile; 91 } 92 93 98 public void setCompileByExtension(boolean compileByExt) 99 { 100 _compileByExt = compileByExt; 101 } 102 103 107 protected void scanDir(File srcDir, File destDir, String [] files, String ext) 108 { 109 if (!_hasSourcepath) 112 { 113 Path srcPath = new Path(getProject()); 114 srcPath.setLocation(srcDir); 115 Path sp = getSourcepath(); 116 sp.append(srcPath); 117 setSourcepath(sp); 118 } 119 120 GlobPatternMapper m = new GlobPatternMapper(); 121 m.setFrom(ext); 122 m.setTo("*.class"); 123 SourceFileScanner sfs = new SourceFileScanner(this); 124 if (ext.equals("*.java")) 125 { 126 File [] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m); 127 if (newFiles.length > 0) 128 { 129 File [] newCompileList = new File [compileList.length + newFiles.length]; 130 System.arraycopy(compileList, 0, newCompileList, 0, compileList.length); 131 System.arraycopy(newFiles, 0, newCompileList, compileList.length, 132 newFiles.length); 133 compileList = newCompileList; 134 } 135 } 136 else 137 { 138 String [] newSources = sfs.restrict(files, srcDir, destDir, m); 139 int extLen = ext.length() - 1; if (newSources.length > 0) 141 { 142 File [] newCompileList = new File [compileList.length + newSources.length]; 143 System.arraycopy(compileList, 0, newCompileList, 0, compileList.length); 144 try 145 { 146 FileUtils fileUtils = FileUtils.newFileUtils(); 147 for (int j = 0; j < newSources.length; j++) 148 { 149 String toName = 150 newSources[j].substring(0, newSources[j].length() - extLen) + 151 ".java"; 152 153 File srcFile = new File (srcDir, newSources[j]); 154 File dstFile = new File (_genDir, toName); 155 fileUtils.copyFile(srcFile, dstFile, null, true, true); 156 newCompileList[compileList.length + j] = dstFile; 157 } 158 } 159 catch (IOException ioe) 160 { 161 throw new BuildException("Unable to copy " + ext + " file", ioe, 162 getLocation()); 163 } 164 compileList = newCompileList; 165 } 166 } 167 } 168 169 public void execute() throws BuildException 170 { 171 if (_genDir == null) 173 throw new BuildException("Missing genDir attribute: must be set to codegen output directory", getLocation()); 174 175 176 if (_srcExts.size() == 0) 178 _srcExts.add("*.java"); 179 180 String [] userSourcepaths = null; 182 _hasSourcepath = getSourcepath() != null; 183 if ( _hasSourcepath ) 184 userSourcepaths = getSourcepath().list(); 185 186 Path genPath = new Path(getProject()); 188 genPath.setLocation(_genDir); 189 setSourcepath(genPath); 190 191 if ( _hasSourcepath ) 196 { 197 String genDirPath = _genDir.getAbsolutePath(); 198 String srcDirPath = (getSrcdir().list())[0]; for ( String p: userSourcepaths ) 200 { 201 if ( p.startsWith( srcDirPath ) && p.length() > srcDirPath.length() ) 202 { 203 File genDirElem = new File ( _genDir, p.substring( srcDirPath.length()+1 )); 204 Path gp = new Path(getProject()); 205 gp.setLocation( genDirElem ); 206 setSourcepath(gp); 207 } 208 } 209 } 210 211 setExecutable("apt"); 215 setFork(true); 216 217 Commandline.Argument arg = createCompilerArg(); 221 arg.setValue("-s"); 222 arg = createCompilerArg(); 223 arg.setFile(_genDir); 224 225 if(_nocompile) 227 { 228 Commandline.Argument ncarg = createCompilerArg(); 229 ncarg.setValue("-nocompile"); 230 } 231 232 for (Object i : _processorOptions) 236 { 237 Commandline.Argument optionArg = createCompilerArg(); 238 optionArg.setValue("-A" + i); 239 } 240 241 checkParameters(); 242 resetFileLists(); 243 244 for (int j = 0; j < _srcExts.size(); j++) 247 { 248 String ext = (String )_srcExts.get(j); 249 Vector <File > inputFiles = new Vector <File >(); 250 251 String [] list = getSrcdir().list(); 254 File destDir = getDestdir(); 255 for (int i = 0; i < list.length; i++) 256 { 257 File srcFile = getProject().resolveFile(list[i]); 258 if (!srcFile.exists()) { 259 throw new BuildException("srcdir \"" 260 + srcFile.getPath() 261 + "\" does not exist!", getLocation()); 262 } 263 264 if (srcFile.isDirectory()) 269 { 270 DirectoryScanner ds = this.getDirectoryScanner(srcFile); 271 String [] files = ds.getIncludedFiles(); 272 scanDir(srcFile, destDir != null ? destDir : srcFile, files, ext); 273 } 274 else 275 { 276 if (srcFile.getPath().endsWith(ext.substring(1))) 280 inputFiles.add(srcFile); 281 } 282 } 283 284 if (inputFiles.size() != 0) 285 { 286 File [] newCompileList = new File [compileList.length + inputFiles.size()]; 287 inputFiles.toArray(newCompileList); 288 System.arraycopy(compileList, 0, newCompileList, inputFiles.size(), 289 compileList.length); 290 compileList = newCompileList; 291 } 292 293 if (_compileByExt) 298 { 299 compile(); 300 resetFileLists(); 301 } 302 } 303 304 if (!_compileByExt) 308 compile(); 309 } 310 311 protected boolean _nocompile = false; 312 protected boolean _compileByExt = false; 313 protected boolean _hasSourcepath; 314 protected File _genDir; 315 protected Vector _srcExts = new Vector (); 316 protected Vector _processorOptions = new Vector (); 317 } 318 | Popular Tags |