1 18 19 package org.apache.tools.ant; 20 21 import java.util.ArrayList ; 22 import java.util.Collections ; 23 import java.util.Enumeration ; 24 import java.util.Hashtable ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.StringTokenizer ; 28 29 import org.apache.tools.ant.util.CollectionUtils; 30 31 35 public class Target implements TaskContainer { 36 37 38 private String name; 39 40 private String ifCondition = ""; 41 42 private String unlessCondition = ""; 43 44 private List dependencies = null; 45 46 private List children = new ArrayList (); 47 48 private Location location = Location.UNKNOWN_LOCATION; 49 50 51 private Project project; 52 53 54 private String description = null; 55 56 57 public Target() { 58 } 60 61 65 public Target(Target other) { 66 this.name = other.name; 67 this.ifCondition = other.ifCondition; 68 this.unlessCondition = other.unlessCondition; 69 this.dependencies = other.dependencies; 70 this.location = other.location; 71 this.project = other.project; 72 this.description = other.description; 73 this.children = other.children; 75 } 76 77 83 public void setProject(Project project) { 84 this.project = project; 85 } 86 87 93 public Project getProject() { 94 return project; 95 } 96 97 103 public void setLocation(Location location) { 104 this.location = location; 105 } 106 107 113 public Location getLocation() { 114 return location; 115 } 116 117 124 public void setDepends(String depS) { 125 if (depS.length() > 0) { 126 StringTokenizer tok = 127 new StringTokenizer (depS, ",", true); 128 while (tok.hasMoreTokens()) { 129 String token = tok.nextToken().trim(); 130 131 if ("".equals(token) || ",".equals(token)) { 133 throw new BuildException("Syntax Error: depends " 134 + "attribute of target \"" + getName() 135 + "\" has an empty string as dependency."); 136 } 137 138 addDependency(token); 139 140 if (tok.hasMoreTokens()) { 143 token = tok.nextToken(); 144 if (!tok.hasMoreTokens() || !",".equals(token)) { 145 throw new BuildException("Syntax Error: Depend " 146 + "attribute for target \"" + getName() 147 + "\" ends with a , character"); 148 } 149 } 150 } 151 } 152 } 153 154 159 public void setName(String name) { 160 this.name = name; 161 } 162 163 169 public String getName() { 170 return name; 171 } 172 173 178 public void addTask(Task task) { 179 children.add(task); 180 } 181 182 188 public void addDataType(RuntimeConfigurable r) { 189 children.add(r); 190 } 191 192 197 public Task[] getTasks() { 198 List tasks = new ArrayList (children.size()); 199 Iterator it = children.iterator(); 200 while (it.hasNext()) { 201 Object o = it.next(); 202 if (o instanceof Task) { 203 tasks.add(o); 204 } 205 } 206 207 return (Task[]) tasks.toArray(new Task[tasks.size()]); 208 } 209 210 216 public void addDependency(String dependency) { 217 if (dependencies == null) { 218 dependencies = new ArrayList (2); 219 } 220 dependencies.add(dependency); 221 } 222 223 228 public Enumeration getDependencies() { 229 return (dependencies != null ? Collections.enumeration(dependencies) 230 : new CollectionUtils.EmptyEnumeration()); 231 } 232 233 239 public boolean dependsOn(String other) { 240 Project p = getProject(); 241 Hashtable t = (p == null) ? null : p.getTargets(); 242 return (p != null 243 && p.topoSort(getName(), t, false).contains(t.get(other))); 244 } 245 246 259 public void setIf(String property) { 260 ifCondition = (property == null) ? "" : property; 261 } 262 263 270 public String getIf() { 271 return ("".equals(ifCondition) ? null : ifCondition); 272 } 273 274 287 public void setUnless(String property) { 288 unlessCondition = (property == null) ? "" : property; 289 } 290 291 298 public String getUnless() { 299 return ("".equals(unlessCondition) ? null : unlessCondition); 300 } 301 302 309 public void setDescription(String description) { 310 this.description = description; 311 } 312 313 319 public String getDescription() { 320 return description; 321 } 322 323 329 public String toString() { 330 return name; 331 } 332 333 349 public void execute() throws BuildException { 350 if (testIfCondition() && testUnlessCondition()) { 351 for (int taskPosition = 0; 352 taskPosition < children.size(); 353 ++taskPosition) { 354 Object o = children.get(taskPosition); 355 if (o instanceof Task) { 356 Task task = (Task) o; 357 task.perform(); 358 } else { 359 RuntimeConfigurable r = (RuntimeConfigurable) o; 360 r.maybeConfigure(project); 361 } 362 } 363 } else if (!testIfCondition()) { 364 project.log(this, "Skipped because property '" 365 + project.replaceProperties(ifCondition) 366 + "' not set.", Project.MSG_VERBOSE); 367 } else { 368 project.log(this, "Skipped because property '" 369 + project.replaceProperties(unlessCondition) 370 + "' set.", Project.MSG_VERBOSE); 371 } 372 } 373 374 381 public final void performTasks() { 382 RuntimeException thrown = null; 383 project.fireTargetStarted(this); 384 try { 385 execute(); 386 } catch (RuntimeException exc) { 387 thrown = exc; 388 throw exc; 389 } finally { 390 project.fireTargetFinished(this, thrown); 391 } 392 } 393 394 402 void replaceChild(Task el, RuntimeConfigurable o) { 403 int index; 404 while ((index = children.indexOf(el)) >= 0) { 405 children.set(index, o); 406 } 407 } 408 409 417 void replaceChild(Task el, Task o) { 418 int index; 419 while ((index = children.indexOf(el)) >= 0) { 420 children.set(index, o); 421 } 422 } 423 424 433 private boolean testIfCondition() { 434 if ("".equals(ifCondition)) { 435 return true; 436 } 437 438 String test = project.replaceProperties(ifCondition); 439 return project.getProperty(test) != null; 440 } 441 442 451 private boolean testUnlessCondition() { 452 if ("".equals(unlessCondition)) { 453 return true; 454 } 455 String test = project.replaceProperties(unlessCondition); 456 return project.getProperty(test) == null; 457 } 458 } 459 | Popular Tags |