KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ant > taskdefs > AntCallOnDirectoryList


1 /*
2  * JBoss, the OpenSource webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.ant.taskdefs;
8
9 import org.apache.tools.ant.Task;
10 import org.apache.tools.ant.BuildException;
11 import org.apache.tools.ant.DirectoryScanner;
12 import org.apache.tools.ant.taskdefs.Ant;
13 import org.apache.tools.ant.taskdefs.Property;
14 import org.apache.tools.ant.util.SourceFileScanner;
15 import org.apache.tools.ant.util.FlatFileNameMapper;
16 import org.apache.tools.ant.types.DirSet;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.io.File JavaDoc;
21
22
23 /**
24  * This class will call a specified ant class for each existent file into fileSet or dirSet
25  *
26  * @author Clebert.suconic@jboss.com
27  */

28 public class AntCallOnDirectoryList extends Task
29 {
30     ArrayList JavaDoc list = new ArrayList JavaDoc();
31
32     String JavaDoc targetToExecute;
33
34     String JavaDoc directoryProperty;
35     String JavaDoc versionNameProperty;
36
37     Ant ant = null;
38
39     public String JavaDoc getVersionNameProperty()
40     {
41         return versionNameProperty;
42     }
43
44     public void setVersionNameProperty(String JavaDoc versionNameProperty)
45     {
46         this.versionNameProperty = versionNameProperty;
47     }
48
49     public String JavaDoc getTargetToExecute()
50     {
51         return targetToExecute;
52     }
53
54     public void setTargetToExecute(String JavaDoc targetToExecute)
55     {
56         this.targetToExecute = targetToExecute;
57     }
58
59     public String JavaDoc getDirectoryProperty()
60     {
61         return directoryProperty;
62     }
63
64     public void setDirectoryProperty(String JavaDoc directoryProperty)
65     {
66         this.directoryProperty = directoryProperty;
67     }
68
69
70     public void init()
71     {
72         super.init();
73         ant = (Ant) this.getProject().createTask("ant");
74         ant.setAntfile(this.getProject().getProperty("ant.file"));
75         ant.setOwningTarget(getOwningTarget());
76         ant.setTaskName(getTaskName());
77         ant.setLocation(getLocation());
78         ant.init();
79     }
80
81
82     public void addDirSet(DirSet dirSet)
83     {
84         list.add(dirSet);
85     }
86
87     public void execute() throws BuildException
88     {
89         init();
90         ant.setTarget(getTargetToExecute());
91         Property parameterDirectory = ant.createProperty();
92         parameterDirectory.setName(this.getDirectoryProperty());
93
94         Property parameterName = ant.createProperty();
95         parameterName.setName(this.getVersionNameProperty());
96
97
98         Iterator JavaDoc iter = list.iterator();
99         while (iter.hasNext())
100         {
101             DirSet dirSet = (DirSet) iter.next();
102             File JavaDoc currentDir = dirSet.getDir(this.getProject());
103             DirectoryScanner scanner = dirSet.getDirectoryScanner(this.getProject());
104
105             SourceFileScanner sourceScanner = new SourceFileScanner(this);
106             String JavaDoc[] strfiles = scanner.getIncludedDirectories();
107             ArrayList JavaDoc files = new ArrayList JavaDoc();
108             for (int i = 0; i < strfiles.length; i++)
109             {
110                 File JavaDoc currentFile = new File JavaDoc(currentDir, strfiles[i]);
111                 if (currentFile.getParentFile().getAbsolutePath().equals(currentDir.getAbsolutePath()))
112                 {
113                     files.add(currentFile);
114                     parameterDirectory.setValue(currentFile.getAbsolutePath());
115                     parameterName.setValue(currentFile.getName());
116                     ant.execute();
117                 }
118             }
119
120         }
121     }
122 }
123
Popular Tags