1 18 package org.apache.tools.ant.taskdefs; 19 20 import org.apache.tools.ant.Task; 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.taskdefs.condition.Condition; 23 import org.apache.tools.ant.types.Reference; 24 import org.apache.tools.ant.types.Comparison; 25 import org.apache.tools.ant.types.ResourceCollection; 26 27 32 public class ResourceCount extends Task implements Condition { 33 34 private static final String ONE_NESTED_MESSAGE 35 = "ResourceCount can count resources from exactly one nested ResourceCollection."; 36 37 private static final String COUNT_REQUIRED 38 = "Use of the ResourceCount condition requires that the count attribute be set."; 39 40 private ResourceCollection rc; 41 private Comparison when = Comparison.EQUAL; 42 private Integer count; 43 private String property; 44 45 50 public void add(ResourceCollection r) { 51 if (rc != null) { 52 throw new BuildException(ONE_NESTED_MESSAGE); 53 } 54 rc = r; 55 } 56 57 61 public void setRefid(Reference r) { 62 Object o = r.getReferencedObject(); 63 if (!(o instanceof ResourceCollection)) { 64 throw new BuildException(r.getRefId() 65 + " doesn\'t denote a ResourceCollection"); 66 } 67 add((ResourceCollection) o); 68 } 69 70 73 public void execute() { 74 if (rc == null) { 75 throw new BuildException(ONE_NESTED_MESSAGE); 76 } 77 if (property == null) { 78 log("resource count = " + rc.size()); 79 } else { 80 getProject().setNewProperty(property, Integer.toString(rc.size())); 81 } 82 } 83 84 89 public boolean eval() { 90 if (rc == null) { 91 throw new BuildException(ONE_NESTED_MESSAGE); 92 } 93 if (count == null) { 94 throw new BuildException(COUNT_REQUIRED); 95 } 96 return when.evaluate(new Integer (rc.size()).compareTo(count)); 97 } 98 99 103 public void setCount(int c) { 104 count = new Integer (c); 105 } 106 107 112 public void setWhen(Comparison c) { 113 when = c; 114 } 115 116 120 public void setProperty(String p) { 121 property = p; 122 } 123 124 } 125 | Popular Tags |