1 28 29 package com.idaremedia.antx; 30 31 import java.text.MessageFormat ; 32 33 import org.apache.tools.ant.BuildException; 34 import org.apache.tools.ant.Project; 35 import org.apache.tools.ant.Task; 36 37 import com.idaremedia.apis.UIStringManager; 38 39 import com.idaremedia.antx.apis.ProjectDependent; 40 import com.idaremedia.antx.apis.Reconfigurable; 41 import com.idaremedia.antx.apis.Requester; 42 import com.idaremedia.antx.apis.ScriptLocatable; 43 import com.idaremedia.antx.helpers.Tk; 44 45 56 57 public abstract class AssertableTask extends Task 58 implements ProjectDependent, ScriptLocatable 59 { 60 63 protected AssertableTask() 64 { 65 super(); 66 Iam_=""; 67 } 68 69 70 71 75 protected AssertableTask(String iam) 76 { 77 super(); 78 Iam_= Tk.cvlabelFrom(iam); 79 } 80 81 82 83 90 public void init() 91 { 92 super.init(); 93 94 if (!m_initedOnce) { 95 initonce(); 96 m_initedOnce=true; 97 } 98 } 99 100 101 102 109 protected void initonce() 110 { 111 } 112 113 114 115 118 public void reconfigure() 119 { 120 if (this instanceof Reconfigurable) { 121 unconfigure(); 122 } else { 123 String alert = uistrs().get("cv.redoable",Iam_,getTaskType()); 124 log(alert,Project.MSG_INFO); 125 this.unconfigure(); 126 } 127 super.reconfigure(); 128 m_configuredOnce=false; 129 } 130 131 132 133 139 public void unconfigure() 140 { 141 m_msgId = null; 142 } 143 144 145 146 150 public void maybeConfigure() 151 { 152 if (!m_configuredOnce) { 153 super.maybeConfigure(); 154 this.m_configuredOnce = true; 155 } 156 } 157 158 159 160 166 protected final void syncChildTargetIfNull(Object newitem) 167 { 168 if (newitem instanceof Task) { 171 Task t = (Task)newitem; 172 if (t.getOwningTarget()==null) { 173 t.setOwningTarget(getOwningTarget()); 174 } 175 } 176 } 177 178 179 180 186 public final UIStringManager uistrs() 187 { 188 return Iteration.uistrs(); 189 } 190 191 192 196 199 public void log(String msg, int msgLevel) 200 { 201 if (getProject()!=null) { 202 getProject().log(this,msg,msgLevel); 203 } else { 204 if (msgLevel >= Project.MSG_INFO) { System.err.println(msg); 206 } 207 } 208 } 209 210 214 215 218 public final String cvlabel_() 219 { 220 return Iam_; 221 } 222 223 224 230 protected final void require_(boolean c, String msg) 231 { 232 if (!c) { 233 String ermsg = uistrs().get("cv.require",Iam_,msg); 234 log(ermsg, Project.MSG_ERR); 235 throw new IllegalArgumentException (ermsg); 236 } 237 } 238 239 240 247 protected final void ensure_(boolean c, String msg) 248 { 249 if (!c) { 250 String ermsg = uistrs().get("cv.ensure",Iam_,msg); 251 log(ermsg, Project.MSG_ERR); 252 throw new IllegalStateException (ermsg); 253 } 254 } 255 256 257 264 protected final void verify_(boolean c, String msg) 265 { 266 if (!c) { 267 String ermsg = uistrs().get("cv.verify",Iam_,msg); 268 log(ermsg, Project.MSG_ERR); 269 throw new IllegalStateException (ermsg); 270 } 271 } 272 273 274 280 protected final void unexpected_(Throwable t, String msg) 281 { 282 String ermsg = uistrs().get("cv.unexpected",Iam_,msg,t); 283 log(ermsg, Project.MSG_WARN); 284 } 285 286 287 291 protected final void verifyInProject_(String calr) 292 { 293 if (getProject()==null) { 294 String ermsg = uistrs().get("cv.verifyInP",Iam_,calr); 295 log(ermsg, Project.MSG_ERR); 296 throw new IllegalStateException (ermsg); 297 } 298 } 299 300 301 306 protected final void verifyInTarget_(String calr) 307 { 308 if (getOwningTarget()==null) { 309 String ermsg = uistrs().get("cv.verifyInT",Iam_,calr); 310 log(ermsg, Project.MSG_ERR); 311 throw new IllegalStateException (ermsg); 312 } 313 verifyInProject_(calr); 314 } 315 316 317 326 protected void verifyCanExecute_(String calr) 327 throws BuildException 328 { 329 verifyInProject_(calr); 330 } 331 332 333 334 345 protected final boolean checkIfProperty_(String property, boolean warn) 346 { 347 require_(property!=null,"chkProp- nonzro nam"); 348 349 return FixtureExaminer.checkIfProperty(getProject(), 350 new Requester.ForComponent(this), property, warn); 351 } 352 353 354 355 366 protected final boolean checkIfReference_(String refid, boolean warn) 367 { 368 require_(refid!=null,"chkRef- nonzro refid"); 369 370 return FixtureExaminer.checkIfReference(getProject(), 371 new Requester.ForComponent(this), refid, warn); 372 } 373 374 375 384 public final Object getReferencedObject(Project theProject, String refid, 385 Class requiredClass) 386 { 387 require_(refid!=null,"getRefObj- nonzro id"); 388 389 if (theProject==null) { 390 theProject= getProject(); 391 verify_(theProject!=null,"getRefObj- hav project"); 392 } 393 394 return FixtureExaminer.getReferencedObject(theProject, 395 new Requester.ForComponent(this), refid, requiredClass); 396 } 397 398 402 403 408 protected void setIgnoreProjectProperties(boolean ignored) 409 { 410 m_ignorePP= ignored ? Boolean.TRUE : Boolean.FALSE; 411 } 412 413 414 421 protected final boolean ignoreProjectProperties() 422 { 423 if (m_ignorePP==null) { 424 if (getProject()!=null) { 425 String s = getProject().getProperty(AntX.PROPERTY_MSGIDS_PROP); 426 Boolean B = Tk.string2PosBool(s); 427 if (B!=null) { 428 return !B.booleanValue(); } 430 } 431 return true; } 433 return m_ignorePP.booleanValue(); 434 } 435 436 437 442 public void setMsgId(String msgid) 443 { 444 m_msgId= msgid; 445 } 446 447 448 452 public String getMsgId() 453 { 454 return m_msgId; 455 } 456 457 458 477 public String getMsg(String msgId, MsgGetter getr) 478 { 479 require_(msgId!=null && getr!=null,"getmsg- nonzro args"); 480 verifyInProject_("getMsg"); 481 482 String msg = ignoreProjectProperties() ? null : getProject().getProperty(msgId); 484 if (msg==null) { 485 486 msg = getr.get(msgId); 488 if (Tk.isWhitespace(msg)) { 489 490 msg = msgId; 492 } 493 } else if (msg.indexOf("{")>=0) { msg = MessageFormat.format(msg,getUISMArgs()); 495 } 496 497 return msg; 498 } 499 500 501 509 public String getMsg(MsgGetter getr) 510 { 511 String msgId = getMsgId(); 512 if (msgId==null) { 513 return ""; 514 } 515 return getMsg(msgId, getr); 516 } 517 518 519 525 public String getMsg() 526 { 527 return getMsg(new MsgGetter() { 528 public String get(String msgId) { 529 return getUISM().mget(msgId,m_UISMargs,""); 530 } 531 }); 532 } 533 534 535 540 public NoiseLevel getDefaultMsgLevel() 541 { 542 return NoiseLevel.getDefault(getProject()); 543 } 544 545 546 549 private void getUISMDefaultArgs() 550 { 551 boolean shorten= Iteration.defaultdefaults().isShortLocationsEnabled(); 552 String locstr= Tk.shortStringFrom(shorten, getLocation()); 553 m_UISMargs= new Object []{getTaskName(), locstr}; 554 } 555 556 557 565 public final UIStringManager getUISM() 566 { 567 synchronized (this) { 568 if (m_UISMargs==null) { 569 getUISMDefaultArgs(); 570 } 571 } 572 return Iteration.stringManager(); 573 } 574 575 576 579 public final Object [] getUISMArgs() 580 { 581 synchronized (this) { 582 if (m_UISMargs==null) { 583 getUISMDefaultArgs(); 584 } 585 } 586 return new Object [] {m_UISMargs[0],m_UISMargs[1]}; 587 } 588 589 590 596 public final MsgGetter newMsgGetter(final String arg3) 597 { 598 return new MsgGetter() { 599 public String get(String msgId) { 600 return getUISM().mget 601 (msgId, new Object [] {m_UISMargs[0],m_UISMargs[1], 602 arg3}); 603 } 604 }; 605 } 606 607 608 615 public final MsgGetter newMsgGetter(final String arg3, final String arg4) 616 { 617 return new MsgGetter() { 618 public String get(String msgId) { 619 return getUISM().mget 620 (msgId, new Object [] {m_UISMargs[0],m_UISMargs[1], 621 arg3, arg4}); 622 } 623 }; 624 } 625 626 627 635 public final MsgGetter newMsgGetter(final String arg3, final String arg4, 636 final String arg5) 637 { 638 return new MsgGetter() { 639 public String get(String msgId) { 640 return getUISM().mget 641 (msgId, new Object [] {m_UISMargs[0],m_UISMargs[1], 642 arg3, arg4, arg5}); 643 } 644 }; 645 } 646 647 648 654 public final String getAntXMsg(String msgId) 655 { 656 require_(msgId!=null,"getAntXMsg- nonzro msgId"); 657 String msg = uistrs().get(msgId); 658 if (Tk.isWhitespace(msg)) { 659 msg = msgId; 660 } 661 return msg; 662 } 663 664 665 671 public final String getAntXMsg(String msgId, String arg1) 672 { 673 require_(msgId!=null,"getAntXMsg- nonzro msgId"); 674 String msg = uistrs().get(msgId, arg1); 675 if (Tk.isWhitespace(msg)) { 676 msg = msgId; 677 } 678 return msg; 679 } 680 681 682 690 public final String getAntXMsg(String msgId, String arg1, String arg2) 691 { 692 require_(msgId!=null,"getAntXMsg- nonzro msgId"); 693 String msg = uistrs().get(msgId, arg1,arg2); 694 if (Tk.isWhitespace(msg)) { 695 msg = msgId; 696 } 697 return msg; 698 } 699 700 701 710 public final String getAntXMsg(String msgId, String arg1, String arg2, String arg3) 711 { 712 require_(msgId!=null,"getAntXMsg- nonzro msgId"); 713 String msg = uistrs().get(msgId, arg1,arg2,arg3); 714 if (Tk.isWhitespace(msg)) { 715 msg = msgId; 716 } 717 return msg; 718 } 719 720 721 private final String Iam_; 722 private String m_msgId; 723 private Object [] m_UISMargs; 724 private Boolean m_ignorePP=null; private boolean m_initedOnce; 726 private boolean m_configuredOnce;} 728 729 730 | Popular Tags |