| 1 4 package com.inversoft.savant.ant.taskdefs; 5 6 7 import java.io.File ; 8 import java.util.ArrayList ; 9 import java.util.Arrays ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.StringTokenizer ; 13 14 import org.apache.tools.ant.BuildException; 15 import org.apache.tools.ant.Project; 16 import org.apache.tools.ant.Task; 17 import org.apache.tools.ant.taskdefs.MacroInstance; 18 import org.apache.tools.ant.types.FileSet; 19 import org.apache.tools.ant.types.Mapper; 20 import org.apache.tools.ant.util.FileNameMapper; 21 import org.apache.tools.ant.util.IdentityMapper; 22 23 24 47 public class MacroForeachTask extends Task { 48 49 private String macroName; 50 private String delims = ", \t\n"; 51 private String property = null; 52 private String mappedProperty = null; 53 private String list = null; 54 private final List filesets = new ArrayList (); 55 private final List dirsets = new ArrayList (); 56 private final List params = new ArrayList (); 57 private Mapper mapperElement; 58 59 60 public void setMacro(String macroName) { 61 this.macroName = macroName; 62 } 63 64 67 public void setProperty(String property) { 68 this.property = property; 69 } 70 71 74 public void setMappedproperty(String property) { 75 this.mappedProperty = property; 76 } 77 78 81 public void setList(String list) { 82 this.list = list; 83 } 84 85 88 public void setDelims(String delims) { 89 this.delims = delims; 90 } 91 92 95 public void addFileset(FileSet set) { 96 filesets.add(set); 97 } 98 99 102 public void addDirset(FileSet set) { 103 dirsets.add(set); 104 } 105 106 109 public void addParam(Param param) { 110 params.add(param); 111 } 112 113 116 public Mapper createMapper() throws BuildException { 117 if (mapperElement != null) { 118 throw new BuildException("Cannot define more than one mapper", 119 getLocation()); 120 } else { 121 mapperElement = new Mapper(getProject()); 122 return mapperElement; 123 } 124 } 125 126 129 public void execute() throws BuildException { 130 validate(); 131 132 FileNameMapper mapper = null; 134 if (mapperElement != null) { 135 mapper = mapperElement.getImplementation(); 136 } else { 137 mapper = new IdentityMapper(); 138 } 139 140 if (list != null) { 142 for (StringTokenizer st = new StringTokenizer (list, delims); 143 st.hasMoreTokens(); ) { 144 String val = st.nextToken(); 145 String [] val2 = mapper.mapFileName(val); 146 147 callTarget(val, val2); 148 } 149 } 150 151 if (filesets.size() != 0) { 153 154 for (Iterator sets = filesets.iterator(); sets.hasNext(); ) { 156 FileSet fs = (FileSet) sets.next(); 157 File fromDir = fs.getDir(getProject()); 158 ArrayList fromFiles = new ArrayList (); 159 160 fromFiles.addAll( 161 Arrays.asList(fs.getDirectoryScanner(getProject()).getIncludedFiles())); 162 163 for (Iterator files = fromFiles.iterator(); files.hasNext(); ) { 164 String f = (String ) files.next(); 165 String val = new File (fromDir, f).getPath(); 166 String [] val2 = mapper.mapFileName(val); 167 168 callTarget(val, val2); 169 } 170 } 171 } 172 173 if (dirsets.size() != 0) { 175 176 for (Iterator sets = dirsets.iterator(); sets.hasNext(); ) { 178 FileSet fs = (FileSet) sets.next(); 179 File fromDir = fs.getDir(getProject()); 180 ArrayList fromFiles = new ArrayList (); 181 182 fromFiles.addAll( 183 Arrays.asList(fs.getDirectoryScanner(getProject()).getIncludedDirectories())); 184 185 for (Iterator files = fromFiles.iterator(); files.hasNext(); ) { 186 String f = (String ) files.next(); 187 String val = new File (fromDir, f).getPath(); 188 String [] val2 = mapper.mapFileName(val); 189 190 callTarget(val, val2); 191 } 192 } 193 } 194 } 195 196 private void validate() { 197 if (property == null && mappedProperty == null) { 198 throw new BuildException( "Specify a property or mappedproperty name.", 199 getLocation()); 200 } 201 202 if (!(list == null ^ (filesets.size() == 0 && dirsets.size() == 0))) { 203 throw new BuildException("Specify either list or a fileset.", getLocation()); 204 } 205 } 206 207 210 private void callTarget(String val, String [] val2) { 211 if (val2 == null || val2.length == 0) { 212 log("Skipping because no mapped property", Project.MSG_DEBUG); 213 return; 214 } 215 216 Task task = getProject().createTask(macroName); 217 if (!(task instanceof MacroInstance)) { 218 throw new BuildException("Only macros can be used with the marcoforeach task"); 219 } 220 221 for (int j = 0; j < val2.length; ++j) { 222 MacroInstance macro = (MacroInstance) task; 223 if (property != null) { 224 macro.setDynamicAttribute(property, val); 225 } 226 227 if (mappedProperty != null) { 228 macro.setDynamicAttribute(mappedProperty, val2[j]); 229 } 230 231 for (Iterator iter = params.iterator(); iter.hasNext(); ) { 233 Param param = (Param) iter.next(); 234 macro.setDynamicAttribute(param.getName(), param.getValue()); 235 } 236 237 macro.perform(); 238 } 239 } 240 241 242 245 public static class Param { 246 private String name; 247 private String value; 248 249 public String getName() { 250 return name; 251 } 252 253 public void setName(String name) { 254 this.name = name; 255 } 256 257 public String getValue() { 258 return value; 259 } 260 261 public void setValue(String value) { 262 this.value = value; 263 } 264 } 265 } | Popular Tags |