1 18 package org.apache.tools.ant.types.resources.selectors; 19 20 import java.util.Stack ; 21 import java.util.Iterator ; 22 23 import org.apache.tools.ant.Project; 24 import org.apache.tools.ant.BuildException; 25 import org.apache.tools.ant.types.Comparison; 26 import org.apache.tools.ant.types.DataType; 27 import org.apache.tools.ant.types.Resource; 28 import org.apache.tools.ant.types.Quantifier; 29 import org.apache.tools.ant.types.ResourceCollection; 30 import org.apache.tools.ant.types.resources.Union; 31 import org.apache.tools.ant.types.resources.comparators.ResourceComparator; 32 import org.apache.tools.ant.types.resources.comparators.DelegatedResourceComparator; 33 34 39 public class Compare extends DataType implements ResourceSelector { 40 41 private static final String ONE_CONTROL_MESSAGE 42 = " the <control> element should be specified exactly once."; 43 44 private DelegatedResourceComparator comp = new DelegatedResourceComparator(); 45 private Quantifier against = Quantifier.ALL; 46 47 private Comparison when = Comparison.EQUAL; 48 49 private Union control; 50 51 56 public synchronized void add(ResourceComparator c) { 57 if (isReference()) { 58 throw noChildrenAllowed(); 59 } 60 comp.add(c); 61 } 62 63 67 public synchronized void setAgainst(Quantifier against) { 68 if (isReference()) { 69 throw tooManyAttributes(); 70 } 71 this.against = against; 72 } 73 74 78 public synchronized void setWhen(Comparison when) { 79 if (isReference()) { 80 throw tooManyAttributes(); 81 } 82 this.when = when; 83 } 84 85 90 public synchronized ResourceCollection createControl() { 91 if (isReference()) { 92 throw noChildrenAllowed(); 93 } 94 if (control != null) { 95 throw oneControl(); 96 } 97 control = new Union(); 98 return control; 99 } 100 101 103 public synchronized boolean isSelected(Resource r) { 104 if (isReference()) { 105 return ((ResourceSelector) getCheckedRef()).isSelected(r); 106 } 107 if (control == null) { 108 throw oneControl(); 109 } 110 int t = 0, f = 0; 111 for (Iterator it = control.iterator(); it.hasNext();) { 112 if (when.evaluate(comp.compare(r, (Resource) it.next()))) { 113 t++; 114 } else { 115 f++; 116 } 117 } 118 return against.evaluate(t, f); 119 } 120 121 128 protected synchronized void dieOnCircularReference(Stack stk, Project p) 129 throws BuildException { 130 if (isChecked()) { 131 return; 132 } 133 if (isReference()) { 134 super.dieOnCircularReference(stk, p); 135 } else { 136 if (control != null) { 137 DataType.invokeCircularReferenceCheck(control, stk, p); 138 } 139 DataType.invokeCircularReferenceCheck(comp, stk, p); 140 setChecked(true); 141 } 142 } 143 144 private BuildException oneControl() { 145 return new BuildException(super.toString() + ONE_CONTROL_MESSAGE); 146 } 147 } 148 | Popular Tags |