1 28 29 package com.idaremedia.antx.flowcontrol.match; 30 31 import java.util.Iterator ; 32 33 import org.apache.tools.ant.BuildException; 34 import org.apache.tools.ant.Project; 35 import org.apache.tools.ant.Task; 36 import org.apache.tools.ant.types.Reference; 37 38 import com.idaremedia.antx.AntX; 39 import com.idaremedia.antx.FlexString; 40 import com.idaremedia.antx.helpers.Strings; 41 import com.idaremedia.antx.helpers.TaskHandle; 42 import com.idaremedia.antx.ownhelpers.TaskExaminer; 43 import com.idaremedia.antx.parameters.FlexValueSupport; 44 import com.idaremedia.antx.starters.StrictOuterTask; 45 import com.idaremedia.antx.starters.TaskSet; 46 47 97 98 public class SwitchTask extends TaskSet implements StrictOuterTask, FlexValueSupport 99 { 100 103 public SwitchTask() 104 { 105 super(AntX.flow+"switch"); 106 m_value.setLenient(false); 107 } 108 109 110 113 protected SwitchTask(String iam, boolean delayConfiguration) 114 { 115 super(iam, delayConfiguration); 116 m_value.setLenient(false); 117 } 118 119 120 124 public void setProject(Project P) 125 { 126 super.setProject(P); 127 m_value.setProject(P); 128 } 129 130 134 139 public final void setValue(String value) 140 { 141 require_(value!=null,"setValu- nonzro valu"); 142 if (!m_value.isLiteral()) { 143 throw new BuildException 144 (uistrs().get("flow.switch.needs.value", 145 getTaskName()), getLocation()); 146 } 147 m_value.set(value); 148 m_value.setIsLiteral(); 149 } 150 151 152 157 public final String getSwitchValue() 158 { 159 return m_value.isLiteral() ? m_value.get() : null; 160 } 161 162 163 164 170 public void setProperty(String property) 171 { 172 require_(property!=null,"setProp- nonzro name"); 173 if (!m_value.isUndefined() && !m_value.isProperty()) { 174 throw new BuildException 175 (uistrs().get("flow.switch.needs.value", 176 getTaskName()), getLocation()); 177 } 178 m_value.set(property); 179 m_value.setIsProperty(true); 180 } 181 182 183 184 189 public final String getSwitchProperty() 190 { 191 return m_value.isProperty() ? m_value.get() : null; 192 } 193 194 195 196 203 public void setVariable(String variable) 204 { 205 require_(variable!=null,"setVar- nonzro name"); 206 if (!m_value.isUndefined() && !m_value.isExported()) { 207 throw new BuildException 208 (uistrs().get("flow.switch.needs.value", 209 getTaskName()), getLocation()); 210 } 211 m_value.set(variable); 212 m_value.setIsExported(true); 213 } 214 215 216 217 222 public final String getSwitchVariable() 223 { 224 return m_value.isExported() ? m_value.get() : null; 225 } 226 227 228 229 237 public void setReference(String refid) 238 { 239 require_(refid!=null,"setRef- nonzro refid"); 240 if (!m_value.isUndefined() && !m_value.isReference()) { 241 throw new BuildException 242 (uistrs().get("flow.switch.needs.value", 243 getTaskName()), getLocation()); 244 } 245 m_value.set(refid); 246 m_value.setIsReference(true); 247 } 248 249 250 251 257 public final String getSwitchReference() 258 { 259 return m_value.isReference() ? m_value.get() : null; 260 } 261 262 263 264 271 public void setContext(Reference refid) 272 { 273 m_evalContext = refid; 274 } 275 276 277 278 283 public final Reference getContext() 284 { 285 return m_evalContext; 286 } 287 288 289 290 296 public final boolean isUndefined() 297 { 298 return m_value.isUndefined(); 299 } 300 301 302 303 309 public final String getVUT() 310 { 311 return m_value.getValue(getProject()); 312 } 313 314 315 316 320 private String switchvalueString() 321 { 322 String s = getSwitchValue(); 323 if (s!=null) { 324 return s; 325 } 326 s = getSwitchProperty(); 327 if (s!=null) { 328 return "property="+s; 329 } 330 s = getSwitchVariable(); 331 if (s!=null) { 332 return "variable="+s; 333 } 334 s = getSwitchReference(); 335 if (s!=null) { 336 return "reference="+s; 337 } 338 return ""; 339 } 340 341 345 352 public void setHaltIfNoMatch(boolean balk) 353 { 354 m_haltIfNoMatch = balk; 355 } 356 357 358 362 public final boolean isHaltIfNoMatch() 363 { 364 return m_haltIfNoMatch; 365 } 366 367 368 373 public void setMatchProperty(String property) 374 { 375 require_(property!=null,"setMtchProp- nonzro name"); 376 m_matchProperty = property; 377 } 378 379 380 384 public final String getMatchProperty() 385 { 386 return m_matchProperty; 387 } 388 389 390 395 public void setMatchValue(String propertyValue) 396 { 397 require_(propertyValue!=null,"setMtchVal- nonzro valu"); 398 m_matchValue = propertyValue; 399 } 400 401 402 406 public final String getMatchValue() 407 { 408 return m_matchValue; 409 } 410 411 412 416 422 public final MatchAll getDefaultChoice() 423 { 424 return m_defaultTask; 425 } 426 427 428 429 433 protected final void setTheDefaultChoice(MatchAll cDefault) 434 { 435 verifyIsUndefined_(m_defaultTask, cDefault); 436 m_defaultTask = cDefault; 437 m_defaultTask.setEnclosingTask(this); 438 } 439 440 441 442 446 protected void maybeConfigureSpecialTasks() 447 { 448 Iterator itr= getTasksList().listIterator(); 449 while (itr.hasNext()) { 450 ((ChoiceTask)itr.next()).maybeConfigure(); 451 } 452 if (getDefaultChoice()!=null) { 453 getDefaultChoice().maybeConfigure(); 454 } 455 } 456 457 458 459 467 protected boolean includeTask(TaskHandle taskH) 468 { 469 require_(!taskH.isEmpty(),"includTsk- filld hndl"); 470 Task task = taskH.getTask(); 471 472 ChoiceType type = ChoiceType.from(task.getClass()); 473 boolean ok= type!=null; 474 475 if (ok) { 476 if (m_dominantMatchType!=ChoiceType.NONE) { 477 if (type!=m_dominantMatchType) { String ermsg = uistrs().get("flow.switch.case.onekind",getTaskName()); 479 log(ermsg, Project.MSG_ERR); 480 throw new BuildException(ermsg, task.getLocation()); 481 } 482 } else { 483 m_dominantMatchType = type; 484 } 485 } 486 return ok; 487 } 488 489 490 491 496 public void addTask(Task task) 497 { 498 require_(task!=null,"addTsk- nonzro tsk"); 499 500 task = TaskExaminer.trueTask(task,COI_,this); 502 if (task instanceof MatchAll) { 503 setTheDefaultChoice((MatchAll)task); 504 addedTask(); 505 } else { 506 super.addTask(task); 507 verify_((task instanceof ChoiceTask),"addTsk- choice tsk"); 508 ((ChoiceTask)task).setEnclosingTask(this); 509 } 510 } 511 512 513 514 523 protected void performNestedTasks() 524 throws BuildException 525 { 526 String value = getVUT(); 527 Task choiceTask = null; 528 String choiceValue = Strings.DEFAULT; 529 530 if (value!=null || !m_dominantMatchType.needsLeftAndRight()) { 531 Iterator itr = getTasksList().listIterator(); 532 while (itr.hasNext()) { 533 ChoiceTask choice = (ChoiceTask)itr.next(); 534 if (choice.eval(value, m_evalContext)) { 535 choiceTask = choice; 536 choiceValue = choice.getVUT(); 537 break; 538 } 539 } 540 } 541 if (choiceTask==null && getDefaultChoice()!=null) { 542 choiceTask = getDefaultChoice(); 543 } 544 if (choiceTask!=null) { 545 choiceFound(choiceValue,(value==null ? "n/a" : value)); 546 choiceTask.perform(); 547 548 } else if (isHaltIfNoMatch()) { 549 String ermsg = uistrs().get("flow.switch.nomatch",switchvalueString()); 550 log(ermsg,Project.MSG_ERR); 551 throw new BuildException(ermsg,getLocation()); 552 } else { 553 log("Unable to match switch-value \""+switchvalueString()+"\"", 554 Project.MSG_DEBUG); 555 } 556 } 557 558 559 560 568 protected void choiceFound(String choiceValue, String switchValue) 569 { 570 log("Found hit for '"+switchValue+"' from choice value='"+ 571 choiceValue+"'", Project.MSG_DEBUG); 572 573 if (getMatchProperty()!=null) { 574 String what = getMatchValue(); 575 if (what==null) { 576 what = choiceValue; 577 } 578 getProject().setNewProperty(getMatchProperty(),what); 579 } 580 } 581 582 583 584 590 protected void verifyCanExecute_(String calr) 591 { 592 super.verifyCanExecute_(calr); 593 594 if (isUndefined() && m_dominantMatchType.needsLeftAndRight()) { 595 String ermsg = uistrs().get("flow.switch.needs.value",getTaskName()); 596 log(ermsg, Project.MSG_ERR); 597 throw new BuildException(ermsg,getLocation()); 598 } 599 } 600 601 602 603 private MatchAll m_defaultTask; 604 private boolean m_haltIfNoMatch=false; private String m_matchProperty, m_matchValue; 606 private ChoiceType m_dominantMatchType=ChoiceType.NONE; private FlexString m_value = new FlexString(); 608 private Reference m_evalContext; 609 610 615 private static final Class [] COI_= { 616 MatchAll.class, ChoiceTask.class 617 }; 618 } 619 620 621 | Popular Tags |