1 7 package com.inversoft.savant.ant.taskdefs; 8 9 10 import java.io.File ; 11 import java.util.ArrayList ; 12 import java.util.Arrays ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.StringTokenizer ; 16 17 import org.apache.tools.ant.BuildException; 18 import org.apache.tools.ant.Project; 19 import org.apache.tools.ant.Task; 20 import org.apache.tools.ant.taskdefs.Ant; 21 import org.apache.tools.ant.taskdefs.Property; 22 import org.apache.tools.ant.types.FileSet; 23 import org.apache.tools.ant.types.Mapper; 24 import org.apache.tools.ant.util.FileNameMapper; 25 import org.apache.tools.ant.util.IdentityMapper; 26 27 28 60 public class ForeachTask extends Task { 61 62 private String target; 63 private boolean inheritAll = true; 64 private boolean inheritRefs = false; 65 private String delims = ", \t\n"; 66 private String property = null; 67 private String mappedProperty = null; 68 private String list = null; 69 private final List filesets = new ArrayList (); 70 private final List dirsets = new ArrayList (); 71 private Mapper mapperElement; 72 private int verbosity = Project.MSG_VERBOSE; 73 private Ant antTarget; 74 75 76 79 public void setTarget(String target) { 80 this.target = target; 81 } 82 83 86 public void setInheritAll(boolean inheritAll) { 87 this.inheritAll = inheritAll; 88 } 89 90 93 public void setInheritRefs(boolean inheritRefs) { 94 this.inheritRefs = inheritRefs; 95 } 96 97 100 public void setProperty(String property) { 101 this.property = property; 102 } 103 104 107 public void setMappedproperty(String property) { 108 this.mappedProperty = property; 109 } 110 111 114 public void setList(String list) { 115 this.list = list; 116 } 117 118 121 public void setDelims(String delims) { 122 this.delims = delims; 123 } 124 125 128 public void addFileset(FileSet set) { 129 filesets.add(set); 130 } 131 132 135 public Property createParam() { 136 return antTarget.createProperty(); 137 } 138 139 142 public void addDirset(FileSet set) { 143 dirsets.add(set); 144 } 145 146 149 public Mapper createMapper() throws BuildException { 150 if (mapperElement != null) { 151 throw new BuildException("Cannot define more than one mapper", 152 getLocation()); 153 } else { 154 mapperElement = new Mapper(getProject()); 155 return mapperElement; 156 } 157 } 158 159 162 public void setVerbose(boolean verbose) { 163 this.verbosity = verbose ? Project.MSG_INFO : Project.MSG_VERBOSE; 164 } 165 166 169 public void init() { 170 antTarget = (Ant) getProject().createTask("ant"); 171 antTarget.setOwningTarget(super.getOwningTarget()); 172 antTarget.setTaskName(getTaskName()); 173 antTarget.setLocation(getLocation()); 174 antTarget.init(); 175 } 176 177 180 public void execute() throws BuildException { 181 182 if (target == null) { 184 throw new BuildException("Attribute target is required.", getLocation()); 185 } 186 187 if (property == null && mappedProperty == null) { 188 throw new BuildException( "Specify a property or mappedproperty name.", 189 getLocation()); 190 } 191 192 if (!(list == null ^ (filesets.size() == 0 && dirsets.size() == 0))) { 193 throw new BuildException("Specify either list or a fileset.", getLocation()); 194 } 195 196 antTarget.setInheritAll(inheritAll); 197 antTarget.setInheritRefs(inheritRefs); 198 antTarget.setDir(getProject().getBaseDir()); 199 antTarget.setAntfile(getProject().getProperty("ant.file")); 200 antTarget.setTarget(target); 201 Project proj = antTarget.getProject(); 202 203 FileNameMapper mapper = null; 205 if (mapperElement != null) { 206 mapper = mapperElement.getImplementation(); 207 } else { 208 mapper = new IdentityMapper(); 209 } 210 211 if (list != null) { 213 for (StringTokenizer st = new StringTokenizer (list, delims); 214 st.hasMoreTokens(); ) { 215 String val = st.nextToken(); 216 String [] val2 = mapper.mapFileName(val); 217 218 callTarget(val, val2, antTarget, proj, target); 219 } 220 } 221 222 if (filesets.size() != 0) { 224 225 for (Iterator sets = filesets.iterator(); sets.hasNext() ;) { 227 FileSet fs = (FileSet) sets.next(); 228 File fromDir = fs.getDir(getProject()); 229 ArrayList fromFiles = new ArrayList (); 230 231 fromFiles.addAll( 232 Arrays.asList(fs.getDirectoryScanner(getProject()).getIncludedFiles())); 233 234 for (Iterator files = fromFiles.iterator(); files.hasNext();) { 235 String f = (String ) files.next(); 236 String val = new File (fromDir, f).getPath(); 237 String [] val2 = mapper.mapFileName(val); 238 239 callTarget(val, val2, antTarget, proj, target); 240 } 241 } 242 } 243 244 if (dirsets.size() != 0) { 246 247 for (Iterator sets = dirsets.iterator(); sets.hasNext() ;) { 249 FileSet fs = (FileSet) sets.next(); 250 File fromDir = fs.getDir(getProject()); 251 ArrayList fromFiles = new ArrayList (); 252 253 fromFiles.addAll( 254 Arrays.asList(fs.getDirectoryScanner(getProject()).getIncludedDirectories())); 255 256 for (Iterator files = fromFiles.iterator(); files.hasNext(); ) { 257 String f = (String ) files.next(); 258 String val = new File (fromDir, f).getPath(); 259 String [] val2 = mapper.mapFileName(val); 260 261 callTarget(val, val2, antTarget, proj, target); 262 } 263 } 264 } 265 } 266 267 270 protected void callTarget(String val, String [] val2, Ant antTarget, 271 Project proj, String target) { 272 273 if (val2 == null || val2.length == 0) { 274 log("Skipping unmapped " + val, verbosity); 275 return; 276 } 277 278 for (int j = 0; j < val2.length; ++j) { 279 if (property != null) { 280 proj.setProperty(property, val); 281 } 282 283 if (mappedProperty != null) { 284 proj.setProperty(mappedProperty, val2[j]); 285 } 286 287 log( "Calling " + target + " with " 288 + ( property == null ? "" : 289 property + "=" + proj.getProperty( property ) 290 + ( mappedProperty == null ? "" : " and " ) ) 291 + ( mappedProperty == null ? "" : 292 mappedProperty + "=" + proj.getProperty( mappedProperty ) ), 293 verbosity ); 294 295 antTarget.execute(); 296 } 297 } 298 } | Popular Tags |