1 48 package net.sf.antcontrib.logic; 49 50 import org.apache.tools.ant.BuildException; 51 import org.apache.tools.ant.Task; 52 import org.apache.tools.ant.taskdefs.Sequential; 53 import java.util.Vector ; 54 55 119 public class Switch extends Task 120 { 121 private String value; 122 private Vector cases; 123 private Sequential defaultCase; 124 private boolean caseInsensitive; 125 126 129 public Switch() 130 { 131 cases = new Vector (); 132 } 133 134 public void execute() 135 throws BuildException 136 { 137 if (value == null) 138 throw new BuildException("Value is missing"); 139 if (cases.size() == 0 && defaultCase == null) 140 throw new BuildException("No cases supplied"); 141 142 Sequential selectedCase = defaultCase; 143 144 int sz = cases.size(); 145 for (int i=0;i<sz;i++) 146 { 147 Case c = (Case)(cases.elementAt(i)); 148 149 String cvalue = c.value; 150 if (cvalue == null) { 151 throw new BuildException("Value is required for case."); 152 } 153 String mvalue = value; 154 155 if (caseInsensitive) 156 { 157 cvalue = cvalue.toUpperCase(); 158 mvalue = mvalue.toUpperCase(); 159 } 160 161 if (cvalue.equals(mvalue) && c != defaultCase) 162 selectedCase = c; 163 } 164 165 if (selectedCase == null) { 166 throw new BuildException("No case matched the value " + value 167 + " and no default has been specified."); 168 } 169 selectedCase.perform(); 170 } 171 172 175 public void setValue(String value) 176 { 177 this.value = value; 178 } 179 180 public void setCaseInsensitive(boolean c) 181 { 182 caseInsensitive = c; 183 } 184 185 public final class Case extends Sequential 186 { 187 private String value; 188 189 public Case() 190 { 191 super(); 192 } 193 194 public void setValue(String value) 195 { 196 this.value = value; 197 } 198 199 public void execute() 200 throws BuildException 201 { 202 super.execute(); 203 } 204 205 public boolean equals(Object o) 206 { 207 boolean res = false; 208 Case c = (Case)o; 209 if (c.value.equals(value)) 210 res = true; 211 return res; 212 } 213 } 214 215 218 public Switch.Case createCase() 219 throws BuildException 220 { 221 Switch.Case res = new Switch.Case(); 222 cases.add(res); 223 return res; 224 } 225 226 229 public void addDefault(Sequential res) 230 throws BuildException 231 { 232 if (defaultCase != null) 233 throw new BuildException("Cannot specify multiple default cases"); 234 235 defaultCase = res; 236 } 237 238 } 239 | Popular Tags |