1 18 package sync4j.test.tools.ant; 19 20 import java.util.StringTokenizer ; 21 22 import org.apache.tools.ant.Task; 23 import org.apache.tools.ant.BuildException; 24 import org.apache.tools.ant.taskdefs.Ant; 25 import org.apache.tools.ant.taskdefs.Property; 26 27 28 95 public class IterateTask extends Task { 96 97 99 public IterateTask() {} 100 101 110 public void setItems(String items) { 111 this.items = items; 112 } 113 114 119 public void setTarget(String targetName) { 120 this.targetName = targetName; 121 } 122 123 128 public void setInheritAll(boolean inheritAll) { 129 this.inheritAll = inheritAll; 130 } 131 132 137 public void setProperty(String property) { 138 this.property = property; 139 } 140 141 146 public void setDelimiter(String delimiter) { 147 this.delimiter = delimiter; 148 } 149 150 154 public void execute() throws BuildException { 155 156 validateAttributes(); 157 158 task.setDir(getProject().getBaseDir()); 160 task.setAntfile(getProject().getProperty("ant.file")); 161 task.setTarget(targetName); 162 task.setInheritAll(inheritAll); 163 164 StringTokenizer st = new StringTokenizer (items, delimiter); 166 while (st.hasMoreTokens()) { 167 getProject().setProperty(property, (st.nextToken().trim())); 168 task.execute(); 169 } 170 } 171 172 176 public void init() throws BuildException { 177 super.init(); 178 179 task = (Ant) getProject().createTask("ant"); 181 task.setOwningTarget(getOwningTarget()); 182 task.setTaskName(targetName); 183 task.setLocation(getLocation()); 184 task.init(); 185 } 186 187 192 public Property createParam() { 193 return task.createProperty(); 194 } 195 196 197 199 private void validateAttributes() throws BuildException { 200 if (isEmpty(targetName)) { 201 throw new BuildException("Attribute target is required.", getLocation()); 202 } 203 204 if (isEmpty(property)) { 205 throw new BuildException("Attribute property is required.", getLocation()); 206 } 207 208 if (items == null) { 209 throw new BuildException("Attribute items is required.", getLocation()); 210 } 211 } 212 213 private boolean isEmpty(String s) { 214 return ((s == null) || (s.length() == 0)); 215 } 216 217 219 private String items; 220 private String targetName; 221 private boolean inheritAll = true; 222 private Ant task; 223 private String property; 224 private String delimiter = ","; 225 226 } | Popular Tags |