1 28 29 package com.idaremedia.antx.ownhelpers; 30 31 import java.util.Iterator ; 32 import java.util.Map ; 33 34 import org.apache.tools.ant.Project; 35 import org.apache.tools.ant.Task; 36 37 import com.idaremedia.antx.AntX; 38 import com.idaremedia.antx.Iteration; 39 import com.idaremedia.antx.go.Go; 40 import com.idaremedia.antx.go.Iff; 41 import com.idaremedia.antx.go.IffAll; 42 import com.idaremedia.antx.go.IffAnt; 43 import com.idaremedia.antx.go.IffOs; 44 import com.idaremedia.antx.go.IffValue; 45 import com.idaremedia.antx.parameters.Conditional; 46 import com.idaremedia.antx.parameters.FlexConditional; 47 48 64 65 public class OptionalExecuteHelper extends ProjectDependentSkeleton 66 implements FlexConditional 67 { 68 private static final String IAM_= 69 AntX.AntX+"OptionalExecuteHelper:"; 70 71 72 75 public OptionalExecuteHelper() 76 { 77 } 78 79 80 85 public OptionalExecuteHelper(Project P) 86 { 87 setProject(P); 88 } 89 90 91 98 public OptionalExecuteHelper(Map attrs) 99 { 100 AntX.require_(attrs!=null,IAM_,"ctor- nonzro attrs"); 101 String v; 102 103 v = (String )attrs.get("if"); 104 if (v!=null) { setIf(v); } 105 v = (String )attrs.get("unless"); 106 if (v!=null) { setUnless(v); } 107 108 v = (String )attrs.get("true"); 109 if (v!=null) { setTrue(v); } 110 v = (String )attrs.get("false"); 111 if (v!=null) { setFalse(v); } 112 113 v = (String )attrs.get("ifos"); 114 if (v!=null) { setIfOS(v); } 115 v = (String )attrs.get("unlessos"); 116 if (v!=null) { setUnlessOS(v); } 117 118 v = (String )attrs.get("ifantlike"); 119 if (v!=null) { setIfAntLike(v); } 120 v = (String )attrs.get("unlessantlike"); 121 if (v!=null) { setUnlessAntLike(v); } 122 } 123 124 125 129 public void addIfTest(Go.Test t) 130 { 131 m_ifTests.add(t); 132 } 133 134 135 139 public void addUnlessTest(Go.Test t) 140 { 141 m_unlessTests.add(t); 142 } 143 144 145 148 public final void clearTests() 149 { 150 m_ifTests.clear(); 151 m_unlessTests.clear(); 152 } 153 154 155 public void setIf(String property) 156 { 157 addIfTest(new Iff.Exists(property)); 158 } 159 160 161 public void setUnless(String property) 162 { 163 addUnlessTest(new Iff.NotExists(property)); 164 } 165 166 167 public void setTrue(String booleanString) 168 { 169 addIfTest(new IffValue.IsTrue(booleanString)); 170 } 171 172 public void setFalse(String booleanString) 173 { 174 addIfTest(new IffValue.IsNotTrue(booleanString)); 175 } 176 177 178 public void setIfTrue(String property) 179 { 180 addIfTest(new Iff.IsTrue(property)); 181 } 182 183 184 public void setUnlessTrue(String property) 185 { 186 addUnlessTest(new Iff.IsNotTrue(property)); 187 } 188 189 190 public void setIfAll(String properties) 191 { 192 addIfTest(new IffAll.Exists(properties)); 193 } 194 195 196 public void setUnlessAll(String properties) 197 { 198 addUnlessTest(new IffAll.NotExists(properties)); 199 } 200 201 202 public void setIfAllTrue(String properties) 203 { 204 addIfTest(new IffAll.IsTrue(properties)); 205 } 206 207 208 public void setUnlessAllTrue(String properties) 209 { 210 addUnlessTest(new IffAll.IsNotTrue(properties)); 211 } 212 213 214 public void setIfOS(String choice) 215 { 216 addIfTest(new IffOs.Is(choice)); 217 } 218 219 220 public void setUnlessOS(String choice) 221 { 222 addUnlessTest(new IffOs.IsNot(choice)); 223 } 224 225 226 public void setIfAntLike(String versionRE) 227 { 228 addIfTest(new IffAnt.IsLike(versionRE)); 229 } 230 231 232 public void setUnlessAntLike(String versionRE) 233 { 234 addUnlessTest(new IffAnt.IsNotLike(versionRE)); 235 } 236 237 238 public String getIfProperty() 239 { 240 String p= ""; 241 int N= m_ifTests.size(); 242 if (N>0) { 243 if (N==1) { 244 p= m_ifTests.get(0).getParameter(); 245 } 246 else { 248 Go.Test t= m_ifTests.get(Iff.Exists.class); 249 if (t==null) { 250 t= m_ifTests.get(Iff.IsTrue.class); 251 } 252 if (t!=null) { 253 p = t.getParameter(); 254 } 255 } 256 } 257 return p; 258 } 259 260 261 public String getUnlessProperty() 262 { 263 String p= ""; 264 int N= m_unlessTests.size(); 265 if (N>0) { 266 if (N==1) { 267 p= m_unlessTests.get(0).getParameter(); 268 } 269 else { 271 Go.Test t= m_unlessTests.get(Iff.NotExists.class); 272 if (t==null) { 273 t= m_unlessTests.get(Iff.IsNotTrue.class); 274 } 275 if (t!=null) { 276 p = t.getParameter(); 277 } 278 } 279 } 280 return p; 281 } 282 283 284 292 public static String testTests(Project P, Go.TestList tests) 293 { 294 AntX.require_(P!=null && tests!=null,IAM_,"test- nonzro proj+list"); 295 if (!tests.isEmpty()) { 296 Iterator itr= tests.iterator(); 297 while (itr.hasNext()) { 298 Go.Test t= (Go.Test)itr.next(); 299 if (!t.pass(P)) { 300 return t.toString(); 301 } 302 } 303 itr = null; 304 } 305 return null; 306 } 307 308 309 310 316 public final String getLastFailure() 317 { 318 return m_lastFailure; 319 } 320 321 322 323 328 public final void setLastFailure(String lastFailure) 329 { 330 m_lastFailure = lastFailure==null ? "" : lastFailure; 331 } 332 333 334 335 342 public boolean testIfCondition() 343 { 344 String failure = testTests(getProjectNoNull(), m_ifTests); 345 if (failure!=null) { 346 m_lastFailure = failure; 347 } 348 return failure==null; 349 } 350 351 352 359 public boolean testUnlessCondition() 360 { 361 String failure = testTests(getProjectNoNull(), m_unlessTests); 362 if (failure!=null) { 363 m_lastFailure = failure; 364 } 365 return failure==null; 366 } 367 368 369 373 public final boolean isSimple() 374 { 375 return m_ifTests.size()<=1 && m_unlessTests.size()<=1; 376 } 377 378 379 380 384 public final boolean isEmpty() 385 { 386 return m_ifTests.isEmpty() && m_unlessTests.isEmpty(); 387 } 388 389 390 391 400 public void logSkippedBecause(Task from, 401 boolean passedIf, int lvl) 402 { 403 Project P= getProject(); 404 405 if (!passedIf) { 406 P.log(from, Iteration.uistrs().get 407 ("task.skipped.if.failed", from.getTaskName(), getLastFailure()), 408 lvl); 409 } 410 else { 411 P.log(from, Iteration.uistrs().get 412 ("task.skipped.unless.failed", from.getTaskName(), getLastFailure()), 413 lvl); 414 } 415 } 416 417 418 419 429 public static boolean include(final Project P, Conditional param) 430 { 431 boolean can = true; 432 String ifProp = param.getIfProperty(); 433 String unProp = param.getUnlessProperty(); 434 435 if (ifProp!=null || unProp!=null) { 436 OptionalExecuteHelper go = new OptionalExecuteHelper(P); 437 if (ifProp!=null) { 438 go.setIf(ifProp); 439 } 440 if (unProp!=null) { 441 go.setUnless(unProp); 442 } 443 can = go.testIfCondition() && go.testUnlessCondition(); 444 } 445 return can; 446 } 447 448 449 450 private Go.TestList m_ifTests= Go.TestList.newList(4); 451 private Go.TestList m_unlessTests= Go.TestList.newList(4); 452 private String m_lastFailure;} 454 455 456 | Popular Tags |