http://nagoya.apache.org/bugzilla/show_bug.cgi?id=1700
*** shadow/1700 Thu May 10 05:54:54 2001
--- shadow/1700.tmp.18234 Thu May 10 05:54:54 2001
***************
*** 0 ****
--- 1,177 ----
+ +============================================================================+
+ | call task for each entry in a fileset |
+ +----------------------------------------------------------------------------+
+ | Bug #: 1700 Product: Ant |
+ | Status: NEW Version: 1.3 |
+ | Resolution: Platform: All |
+ | Severity: Enhancement OS/Version: All |
+ | Priority: Component: Core tasks |
+ +----------------------------------------------------------------------------+
+ | Assigned To: [EMAIL PROTECTED] |
+ | Reported By: [EMAIL PROTECTED] |
+ | CC list: Cc: |
+ +----------------------------------------------------------------------------+
+ | URL: |
+ +============================================================================+
+ | DESCRIPTION |
+ For automating my project, I needed a way to do some stuff
+ on all files in a fileset, and so I implemented AntCallOn
+ which I've been using for some time now, an thought I'd share
+ the code with the Ant team...
+
+ As an example, I show how to call ant for all subprojects.
+
+ ==-- example --==
+ <taskdef name="antcallon" classname="AntCallOn"/>
+ <target name="main">
+ <antcallon target="main-sub" base="${PRJ.home}/">
+ <fileset dir="${PRJ.home}">
+ <include name="*/build.xml"/>
+ <exclude name="setup/build.xml"/>
+ </fileset>
+ </antcallon>
+ </target>
+ <target name="main-sub">
+ <echo message="fileext = ${fileext}"/>
+ <echo message="filedir = ${filedir}"/>
+ <echo message="reldir = ${reldir}"/>
+ <echo message="filename = ${filename}"/>
+ <echo message="filebase = ${filebase}"/>
+ <echo message="running ant in ${reldir}"/>
+ <ant dir="${reldir}"/>
+ </target>
+
+
+ ==-- the code --==
+
+ import org.apache.tools.ant.*;
+ import org.apache.tools.ant.types.*;
+ import org.apache.tools.ant.taskdefs.*;
+
+ import java.util.Vector;
+ import java.io.File;
+ import java.io.IOException;
+
+
+ public class AntCallOn extends CallTarget {
+ protected Vector filesets = new Vector();
+ protected String type = "file";
+ protected String base = null;
+
+ //public void log(String s, int i) { System.err.println(s); }
+ /**
+ * Adds a set of files (nested fileset attribute).
+ */
+ public void addFileset(FileSet set) {
+ filesets.addElement(set);
+ }
+
+ /**
+ * Shall the command work only on files, directories or both?
+ */
+ public void setType(FileDirBoth type) {
+ this.type = type.getValue();
+ }
+
+ public void setBase(String base) {
+ this.base = base;
+ }
+
+ protected void checkConfiguration() {
+ if (filesets.size() == 0) {
+ throw new BuildException("no filesets specified", location);
+ }
+ }
+
+ public void execute() throws BuildException {
+
+ Vector v = new Vector();
+ for (int i=0; i<filesets.size(); i++) {
+ FileSet fs = (FileSet) filesets.elementAt(i);
+ DirectoryScanner ds = fs.getDirectoryScanner(project);
+
+ if (!"dir".equals(type)) {
+ String[] s = ds.getIncludedFiles();
+ for (int j=0; j<s.length; j++) {
+ v.addElement(new File(fs.getDir(project),
+ s[j]).getAbsolutePath());
+ }
+ }
+
+ if (!"file".equals(type)) {
+ String[] s = ds.getIncludedDirectories();
+ for (int j=0; j<s.length; j++) {
+ v.addElement(new File(fs.getDir(project),
+ s[j]).getAbsolutePath());
+ }
+ }
+ }
+
+ String[] s = new String[v.size()];
+ v.copyInto(s);
+
+ this.init();
+ Property param = null;
+ for (int i=0; i<s.length; i++) {
+ log("AntCallOn processing file = " + s[i],Project.MSG_DEBUG);
+ String file = s[i];
+ int path = file.lastIndexOf(java.io.File.separatorChar);
+ if (path<0) { path = 0; }
+ int ext = file.lastIndexOf('.');
+ if (ext<path) { ext = -1; }
+
+ param = super.createParam();
+ param.setName("file");
+ param.setValue(file);
+
+ param = super.createParam();
+ param.setName("filename");
+ param.setValue(file.substring(path+1));
+
+ param = super.createParam();
+ param.setName("fileext");
+ if (ext<0) {
+ param.setValue("");
+ } else {
+ param.setValue(file.substring(ext+1));
+ }
+
+ param = super.createParam();
+ param.setName("filebase");
+ if (ext<0) {
+ param.setValue(file.substring(path+1));
+ } else {
+ param.setValue(file.substring(path+1,ext));
+ }
+
+ param = super.createParam();
+ param.setName("filedir");
+ param.setValue(file.substring(0,path));
+
+ if (base != null) {
+ String javafile=file.replace(java.io.File.separatorChar,'/');
+ String javabase=base.replace(java.io.File.separatorChar,'/');
+ log("javafile='"+javafile+"' javabase='"+javabase+"'",
+ Project.MSG_DEBUG);
+ if (javafile.startsWith(javabase)) {
+ String reldir = javafile.substring(base.length(),path);
+ log("reldir='" + reldir + "'", Project.MSG_DEBUG);
+ param = super.createParam();
+ param.setName("reldir");
+ param.setValue(reldir);
+ }
+ }
+ super.execute();
+ }
+ }
+
+ /**
+ * Enumerated attribute with the values "file", "dir" and "both"
+ * for the type attribute.
+ */
+ public static class FileDirBoth extends EnumeratedAttribute {
+ public String[] getValues() {
+ return new String[] {"file", "dir", "both"};
+ }
+ }
+ }