1 18 19 package org.apache.tools.ant.types.selectors; 20 21 import java.util.Enumeration ; 22 import java.io.File ; 23 24 import org.apache.tools.ant.Project; 25 26 36 public class SelectSelector extends BaseSelectorContainer { 37 38 private String ifProperty; 39 private String unlessProperty; 40 41 44 public SelectSelector() { 45 } 46 47 50 public String toString() { 51 StringBuffer buf = new StringBuffer (); 52 if (hasSelectors()) { 53 buf.append("{select"); 54 if (ifProperty != null) { 55 buf.append(" if: "); 56 buf.append(ifProperty); 57 } 58 if (unlessProperty != null) { 59 buf.append(" unless: "); 60 buf.append(unlessProperty); 61 } 62 buf.append(" "); 63 buf.append(super.toString()); 64 buf.append("}"); 65 } 66 return buf.toString(); 67 } 68 69 73 private SelectSelector getRef() { 74 Object o = getCheckedRef(this.getClass(), "SelectSelector"); 75 return (SelectSelector) o; 76 } 77 78 82 public boolean hasSelectors() { 83 if (isReference()) { 84 return getRef().hasSelectors(); 85 } 86 return super.hasSelectors(); 87 } 88 89 93 public int selectorCount() { 94 if (isReference()) { 95 return getRef().selectorCount(); 96 } 97 return super.selectorCount(); 98 } 99 100 105 public FileSelector[] getSelectors(Project p) { 106 if (isReference()) { 107 return getRef().getSelectors(p); 108 } 109 return super.getSelectors(p); 110 } 111 112 116 public Enumeration selectorElements() { 117 if (isReference()) { 118 return getRef().selectorElements(); 119 } 120 return super.selectorElements(); 121 } 122 123 128 public void appendSelector(FileSelector selector) { 129 if (isReference()) { 130 throw noChildrenAllowed(); 131 } 132 super.appendSelector(selector); 133 } 134 135 136 140 public void verifySettings() { 141 int cnt = selectorCount(); 142 if (cnt < 0 || cnt > 1) { 143 setError("Only one selector is allowed within the " 144 + "<selector> tag"); 145 } 146 } 147 148 153 public boolean passesConditions() { 154 if (ifProperty != null 155 && getProject().getProperty(ifProperty) == null) { 156 return false; 157 } else if (unlessProperty != null 158 && getProject().getProperty(unlessProperty) != null) { 159 return false; 160 } 161 return true; 162 } 163 164 169 public void setIf(String ifProperty) { 170 this.ifProperty = ifProperty; 171 } 172 173 178 public void setUnless(String unlessProperty) { 179 this.unlessProperty = unlessProperty; 180 } 181 182 195 public boolean isSelected(File basedir, String filename, File file) { 196 validate(); 197 198 if (!(passesConditions())) { 200 return false; 201 } 202 203 Enumeration e = selectorElements(); 204 if (!(e.hasMoreElements())) { 205 return true; 206 } 207 FileSelector f = (FileSelector) e.nextElement(); 208 return f.isSelected(basedir, filename, file); 209 } 210 } 211 212 | Popular Tags |