KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antmod > tasks > FileIteratorTask


1 package org.antmod.tasks;
2
3 import java.io.File JavaDoc;
4 import java.util.ArrayList JavaDoc;
5
6 import org.apache.tools.ant.BuildException;
7 import org.apache.tools.ant.DirectoryScanner;
8 import org.apache.tools.ant.Project;
9 import org.apache.tools.ant.Task;
10 import org.apache.tools.ant.taskdefs.CallTarget;
11 import org.apache.tools.ant.types.FileSet;
12
13 /**
14  * Allow buildfiles to execute some things per file, by invoking a target in
15  * the buildfile per file in the given fileset. This task sets the variables
16  * 'antmod.fileiterator.file.name', 'antmod.fileiterator.file.path' etc. which the
17  * target that is invoked can use.
18  *
19  * @author kwaslander
20  */

21 public class FileIteratorTask extends Task {
22     private String JavaDoc target;
23
24     /** The FileSets used to identify the files to iterate across. */
25     protected ArrayList JavaDoc fileSets = new ArrayList JavaDoc();
26
27     
28     /**
29      * Public default constructor.
30      */

31     public FileIteratorTask() {
32     }
33
34     /**
35      * The Ant target to-be-invoked for each file in the fileset.
36      */

37     public void setTarget(String JavaDoc target) {
38         this.target = target;
39     }
40
41     public void addFileSet(FileSet set) {
42         fileSets.add(set);
43     }
44
45     public void execute() throws BuildException {
46         // iterate across all files in each file set
47
int nrFileSets = fileSets.size();
48         for (int i = 0; i < nrFileSets; i++) {
49             FileSet fileSet = (FileSet) fileSets.get(i);
50             runTarget(fileSet);
51         }
52     }
53
54     private void runTarget(FileSet fileSet) throws BuildException {
55         Project project = getProject();
56         File JavaDoc dir = fileSet.getDir(project);
57         DirectoryScanner scanner = fileSet.getDirectoryScanner(project);
58         
59         String JavaDoc[] files = scanner.getIncludedFiles();
60         for (int i = 0; i < files.length; i++) {
61             String JavaDoc file = files[i];
62             File JavaDoc fileObject = new File JavaDoc(dir, file);
63             
64             CallTarget antcall = new CallTarget();
65             antcall.setInheritAll(true);
66             antcall.setProject(getProject());
67             antcall.setTaskName(getTaskName());
68             antcall.setTarget(this.target);
69             ModuleIteratorTask.createInvokeParam(antcall, "antmod.fileiterator.file.name", fileObject.getName());
70             ModuleIteratorTask.createInvokeParam(antcall, "antmod.fileiterator.file.path", fileObject.getPath());
71             ModuleIteratorTask.createInvokeParam(antcall, "antmod.fileiterator.file.absolutepath", fileObject.getAbsolutePath());
72             antcall.execute();
73         }
74     }
75 }
76
Popular Tags