1 48 package net.sf.antcontrib.logic; 49 50 import org.apache.tools.ant.BuildException; 51 import org.apache.tools.ant.DirectoryScanner; 52 import org.apache.tools.ant.Project; 53 import org.apache.tools.ant.Target; 54 import org.apache.tools.ant.Task; 55 import org.apache.tools.ant.taskdefs.Ant; 56 import org.apache.tools.ant.taskdefs.CallTarget; 57 import org.apache.tools.ant.taskdefs.Property; 58 import org.apache.tools.ant.types.FileSet; 59 import java.io.File; 60 import java.util.Enumeration; 61 import java.util.StringTokenizer; 62 import java.util.Vector; 63 64 96 public class ForEach extends Task 97 { 98 private String list; 99 private String param; 100 private String delimiter; 101 private String target; 102 private boolean inheritAll; 103 private boolean inheritRefs; 104 private Vector filesets; 105 private Vector params; 106 private Vector references; 107 108 111 public ForEach() 112 { 113 super(); 114 this.list = null; 115 this.param = null; 116 this.delimiter = ","; 117 this.target = null; 118 this.inheritAll = false; 119 this.inheritRefs = false; 120 this.filesets = new Vector(); 121 this.params = new Vector(); 122 this.references = new Vector(); 123 } 124 125 public void execute() 126 throws BuildException 127 { 128 if (list == null && filesets.size() == 0) 129 throw new BuildException("You must have a list or fileset to iterate through"); 130 if (param == null) 131 throw new BuildException("You must supply a property name to set on each iteration"); 132 if (target == null) 133 throw new BuildException("You must supply an action to perform"); 134 135 if (list != null) 137 { 138 StringTokenizer st = new StringTokenizer(list, delimiter); 139 String toks[] = new String[st.countTokens()]; 140 int i = 0; 141 142 while (st.hasMoreTokens()) 143 { 144 String tok = st.nextToken(); 145 CallTarget ct = createCallTarget(); 146 Property p = ct.createParam(); 147 p.setName(param); 148 p.setValue(tok); 149 ct.execute(); 150 } 151 } 152 153 int sz = filesets.size(); 154 for (int i=0;i<sz;i++) 155 { 156 FileSet fs = (FileSet)(filesets.elementAt(i)); 157 DirectoryScanner ds = fs.getDirectoryScanner(getProject()); 158 159 String files[] = ds.getIncludedFiles(); 160 for (int j=0;j<files.length;j++) 161 { 162 CallTarget ct = createCallTarget(); 163 Property p = ct.createParam(); 164 p.setName(param); 165 p.setLocation(new File(fs.getDir(getProject()), files[j])); 166 ct.execute(); 167 } 168 169 String dirs[] = ds.getIncludedDirectories(); 170 for (int j=0;j<dirs.length;j++) 171 { 172 CallTarget ct = createCallTarget(); 173 Property p = ct.createParam(); 174 p.setName(param); 175 p.setLocation(new File(fs.getDir(getProject()), dirs[j])); 176 ct.execute(); 177 } 178 } 179 } 180 181 public void setList(String list) 182 { 183 this.list = list; 184 } 185 186 public void setDelimiter(String delimiter) 187 { 188 this.delimiter = delimiter; 189 } 190 191 public void setParam(String param) 192 { 193 this.param = param; 194 } 195 196 public void setTarget(String target) 197 { 198 this.target = target; 199 } 200 201 205 public void setInheritall(boolean b) { 206 this.inheritAll = b; 207 } 208 209 213 public void setInheritrefs(boolean b) { 214 this.inheritRefs = b; 215 } 216 217 221 public void addParam(Property p) { 222 params.addElement(p); 223 } 224 225 229 public void addReference(Ant.Reference r) { 230 references.addElement(r); 231 } 232 233 public void addFileset(FileSet set) 234 { 235 filesets.addElement(set); 236 } 237 238 private CallTarget createCallTarget() { 239 CallTarget ct = (CallTarget) getProject().createTask("antcall"); 240 ct.setOwningTarget(getOwningTarget()); 241 ct.init(); 242 ct.setTarget(target); 243 ct.setInheritAll(inheritAll); 244 ct.setInheritRefs(inheritRefs); 245 Enumeration enum = params.elements(); 246 while (enum.hasMoreElements()) { 247 Property param = (Property) enum.nextElement(); 248 Property toSet = ct.createParam(); 249 toSet.setName(param.getName()); 250 if (param.getValue() != null) { 251 toSet.setValue(param.getValue()); 252 } 253 if (param.getFile() != null) { 254 toSet.setFile(param.getFile()); 255 } 256 if (param.getResource() != null) { 257 toSet.setResource(param.getResource()); 258 } 259 if (param.getPrefix() != null) { 260 toSet.setPrefix(param.getPrefix()); 261 } 262 if (param.getRefid() != null) { 263 toSet.setRefid(param.getRefid()); 264 } 265 if (param.getEnvironment() != null) { 266 toSet.setEnvironment(param.getEnvironment()); 267 } 268 if (param.getClasspath() != null) { 269 toSet.setClasspath(param.getClasspath()); 270 } 271 } 272 273 enum = references.elements(); 274 while (enum.hasMoreElements()) { 275 ct.addReference((Ant.Reference) enum.nextElement()); 276 } 277 278 return ct; 279 } 280 281 } 282 283 284 | Popular Tags |