1 28 29 package com.idaremedia.antx.feedback; 30 31 import java.text.DateFormat ; 32 import java.text.FieldPosition ; 33 import java.text.SimpleDateFormat ; 34 import java.util.Date ; 35 36 import org.apache.tools.ant.BuildException; 37 import org.apache.tools.ant.Project; 38 import org.apache.tools.ant.types.Reference; 39 40 import com.idaremedia.apis.DiagnosticsEmitter; 41 import com.idaremedia.apis.DiagnosticsEmitterFactory; 42 43 import com.idaremedia.antx.AntX; 44 import com.idaremedia.antx.AssertableDataType; 45 import com.idaremedia.antx.FixtureComponent; 46 import com.idaremedia.antx.NoiseLevel; 47 import com.idaremedia.antx.helpers.DateTimeFormat; 48 import com.idaremedia.antx.helpers.Setting; 49 import com.idaremedia.antx.helpers.Strings; 50 import com.idaremedia.antx.helpers.Tk; 51 52 102 103 public class EmitConfigurationType extends AssertableDataType 104 implements EmitConfiguration, FixtureComponent, Cloneable 105 { 106 109 public EmitConfigurationType() 110 { 111 super(AntX.feedback+"configure"); 112 } 113 114 115 119 public EmitConfigurationType(String iam) 120 { 121 super(iam); 122 } 123 124 125 131 public final void setController(Object controller) 132 { 133 m_controller = controller; 134 } 135 136 137 142 protected final EmitConfigurationType getOtherEC() 143 { 144 return (EmitConfigurationType)getCheckedRef 145 (EmitConfigurationType.class, "emitconfiguration"); 146 } 147 148 149 154 public synchronized Object clone() 155 { 156 if (isReference()) { 157 return getOtherEC().clone(); 158 } 159 try { 160 EmitConfigurationType cloned = (EmitConfigurationType)super.clone(); 161 if (getOwnEmitterFactory()!=null) { 162 cloned.m_emitterFactory = null; 163 cloned.setEmitterFactory(getOwnEmitterFactory().getClass()); 164 } 165 if (getOwnTimestampFormat()!=null) { 166 if (m_df instanceof SimpleDateFormat ) { 167 cloned.m_df = (DateFormat )((SimpleDateFormat )m_df).clone(); 168 } else { 169 cloned.m_timestampFormat = null; 170 cloned.setTimestampFormat(getOwnTimestampFormat()); 171 } 172 } 173 cloned.m_defaultsEC= null; return cloned; 175 176 } catch(CloneNotSupportedException clnx) { 177 throw new Error (uistrs().get(AntX.CLONE_BROKEN_MSGID)); 178 } 179 } 180 181 182 186 public void setId(String id) 187 { 188 m_Id= id; 189 } 190 191 192 198 public final String getId() 199 { 200 if (m_Id!=null) { 201 return m_Id; 202 } 203 if (isReference()) { 204 return getOtherEC().getId(); 205 } 206 return super.getId(); 207 } 208 209 210 214 220 public void setDefaults(Reference r) 221 { 222 require_(r!=null,"setDflt- nonzro ref"); 223 if (isReference()) { 224 throw tooManyAttributes(); 225 } 226 m_defaultsRef = r; 227 edited(); 228 } 229 230 231 237 public final Reference getOwnDefaults() 238 { 239 if (isReference()) { 240 return getOtherEC().getOwnDefaults(); 241 } 242 return m_defaultsRef; 243 } 244 245 246 252 public synchronized void setDefaultConfiguration(EmitConfiguration defaults) 253 { 254 m_defaultsEC = defaults; 255 } 256 257 258 264 protected synchronized EmitConfiguration getDefaults() 265 { 266 if (m_defaultsEC!=null) { 267 return m_defaultsEC; 268 } 269 if (m_defaultsRef!=null) { 270 EmitConfiguration ec = (EmitConfiguration)getReferencedObject 271 (getProject(),m_defaultsRef.getRefId(),EmitConfiguration.class); 272 verifyNotCircular(ec); 273 m_defaultsEC = ec; 274 return m_defaultsEC; 275 } 276 return EmitContext.getDefaultConfiguration(); 277 } 278 279 280 284 290 public void setFrom(String grpId) 291 { 292 require_(grpId!=null,"setFrom- nonzro grpId"); 293 if (isReference()) { 294 throw tooManyAttributes(); 295 } 296 m_grpId = grpId; 297 edited(); 298 } 299 300 301 309 public final String getOwnFrom() 310 { 311 if (isReference()) { 312 return getOtherEC().getOwnFrom(); 313 } 314 return m_grpId; 315 } 316 317 318 329 public String getFrom() 330 { 331 if (isReference()) { 332 return getOtherEC().getFrom(); 333 } 334 335 String from = getOwnFrom(); 336 String base = null; 337 String ISEP = getGroupingPathSeparator(); 338 339 if (getOwnWrt()!=null) { 340 if (m_wrtImpl!=null) { 341 base = m_wrtImpl; 342 } else { 343 String wrt = Tk.lowercaseFrom(getOwnWrt()); 344 if (Strings.DEFAULT.equals(wrt)) { 345 EmitConfiguration ec = getDefaults(); 346 base = ec.getFrom(); 347 ISEP = ec.getGroupingPathSeparator(); 348 } 349 else if (wrt.length()==0 || "root".equals(wrt)) { 350 base = ""; 351 } 352 else if (Strings.ENCLOSING.equals(wrt)) { 353 EmitConfiguration ec = EmitContext.getConfigurationNoNull(); verifyNotCircular(ec); 355 base = ec.getFrom(); 356 ISEP = ec.getGroupingPathSeparator(); 357 } 358 else { 359 boolean isIt=false; 360 if (getProject().getReference(wrt)!=null) { 361 Object o = getProject().getReference(wrt); 362 if (o instanceof EmitConfiguration) { 363 EmitConfiguration ec = (EmitConfiguration)o; 364 verifyNotCircular(ec); 365 base = ec.getFrom(); 366 ISEP = ec.getGroupingPathSeparator(); 367 isIt = true; 368 } 369 } 370 if (base==null && !isIt) { 371 String warning = uistrs().get("emit.invalid.wrt",getId(),wrt); 372 log(warning,Project.MSG_WARN); 373 } 374 } } 376 } 377 if (base!=null) { 378 if (from==null || from.length()==0) { 379 from = base; 380 } 381 else if (base.length()>0) { 382 from = base + ISEP + from; 383 } 384 } 385 return from; 386 } 387 388 389 397 public void setWrt(String wrt) 398 { 399 if (isReference()) { 400 throw tooManyAttributes(); 401 } 402 m_wrt = wrt; 403 m_wrtImpl = null; 404 if (wrt!=null) { 405 wrt = Tk.lowercaseFrom(wrt); 406 if ("longdate".equals(wrt)) { 407 synchronized(DateTimeFormat.STANDARD_DATE) { 408 m_wrtImpl = DateTimeFormat.STANDARD_DATE.format(new Date ()); 409 } 410 } else if ("shortdate".equals(wrt)) { 411 synchronized(DateTimeFormat.ABBREV_DATE) { 412 m_wrtImpl = DateTimeFormat.ABBREV_DATE.format(new Date ()); 413 } 414 } 415 } 416 edited(); 417 } 418 419 420 427 public String getOwnWrt() 428 { 429 if (isReference()) { 430 return getOtherEC().getOwnWrt(); 431 } 432 return m_wrt; 433 } 434 435 436 441 public void setGroupingSeparator(String separator) 442 { 443 require_(separator!=null,"setGrpSep- nonzro sep"); 444 if (isReference()) { 445 throw tooManyAttributes(); 446 } 447 m_emitterSeparator = separator; 448 edited(); 449 } 450 451 452 453 460 public String getGroupingPathSeparator() 461 { 462 if (isReference()) { 463 return getOtherEC().getGroupingPathSeparator(); 464 } 465 return m_emitterSeparator!=null ? m_emitterSeparator 466 : getDefaults().getGroupingPathSeparator(); 467 } 468 469 470 474 479 public void setProperties(String nameList) 480 { 481 require_(nameList!=null,"setProps- nonzro names"); 482 if (isReference()) { 483 throw tooManyAttributes(); 484 } 485 m_propertyNames= nameList; 486 edited(); 487 } 488 489 490 495 public final String getOwnProperties() 496 { 497 if (isReference()) { 498 return getOtherEC().getOwnProperties(); 499 } 500 return m_propertyNames; 501 } 502 503 504 513 public boolean getPropertiesNameList(StringBuffer list) 514 { 515 require_(list!=null,"getNams- nonzro listbuf"); 516 if (isReference()) { 517 return getOtherEC().getPropertiesNameList(list); 518 } 519 boolean dflts = getDefaults().getPropertiesNameList(list); 520 521 String myList = getOwnProperties(); 522 if (myList!=null) { 523 if (list.length()>0) { 524 list.append(","); 525 } 526 list.append(myList); 527 } 528 return dflts || myList!=null; 529 } 530 531 532 536 540 public void setNoiseLevel(NoiseLevel nl) 541 { 542 require_(nl!=null,"setNoiz- nonzro lvl"); 543 if (isReference()) { 544 throw tooManyAttributes(); 545 } 546 m_noiseLevel = nl; 547 edited(); 548 } 549 550 551 555 public NoiseLevel getOwnNoiseLevel() 556 { 557 if (isReference()) { 558 return getOtherEC().getOwnNoiseLevel(); 559 } 560 return m_noiseLevel; 561 } 562 563 564 569 public NoiseLevel getNoiseLevel() 570 { 571 if (isReference()) { 572 return getOtherEC().getNoiseLevel(); 573 } 574 return m_noiseLevel!=null ? m_noiseLevel : getDefaults().getNoiseLevel(); 575 } 576 577 578 583 public void setEcho(String wantIt) 584 { 585 require_(wantIt!=null,"setEcho- nonzro setin"); 586 if (isReference()) { 587 throw tooManyAttributes(); 588 } 589 m_echoPref= Setting.from(wantIt, Setting.INHERITED); 590 edited(); 591 } 592 593 594 598 public final Setting getOwnEcho() 599 { 600 if (isReference()) { 601 return getOtherEC().getOwnEcho(); 602 } 603 return m_echoPref; 604 } 605 606 607 612 public boolean shouldEcho() 613 { 614 if (isReference()) { 615 return getOtherEC().shouldEcho(); 616 } 617 switch (getOwnEcho().getIndex()) { 618 case Setting.ON_INDEX: { 619 return true; 620 } 621 case Setting.OFF_INDEX:{ 622 return false; 623 } 624 } 625 return getDefaults().shouldEcho(); 626 } 627 628 632 641 public void setTimestampFormat(String format) 642 { 643 require_(format!=null,"setDtTmFmt- nonzro fmt"); 644 if (isReference()) { 645 throw tooManyAttributes(); 646 } 647 String normal= Tk.lowercaseFrom(format); 648 if ("short".equals(normal)) { 649 m_df = DateTimeFormat.ABBREV; 650 } else if ("long".equals(normal)) { 651 m_df = DateTimeFormat.STANDARD; 652 } else if ("gmt".equals(normal)) { 653 m_df = DateTimeFormat.GMT; 654 } 655 m_timestampFormat = format; 656 edited(); 657 } 658 659 660 664 public final String getOwnTimestampFormat() 665 { 666 if (isReference()) { 667 return getOtherEC().getOwnTimestampFormat(); 668 } 669 return m_timestampFormat; 670 } 671 672 673 679 protected final synchronized DateFormat getDateTimeFormatter() 680 { 681 if (m_df!=null) { 682 return m_df; 683 } 684 String fmt = getOwnTimestampFormat(); 685 if (!Tk.isWhitespace(fmt)) { 686 m_df = new SimpleDateFormat (fmt); 687 return m_df; 688 } 689 return null; 690 } 691 692 693 701 public String stampify(long ms) 702 { 703 if (isReference()) { 704 return getOtherEC().stampify(ms); 705 } 706 DateFormat df= getDateTimeFormatter(); 707 if (df!=null) { 708 synchronized(df) { 709 StringBuffer sb = new StringBuffer (40); 710 df.format(new Date (ms),sb,new FieldPosition (0)); 711 return sb.substring(0); 712 } 713 } 714 return getDefaults().stampify(ms); 715 } 716 717 718 722 public void setTimestamp(String wantIt) 723 { 724 require_(wantIt!=null,"setTimstmp- nonzro setin"); 725 if (isReference()) { 726 throw tooManyAttributes(); 727 } 728 m_timestampPref= Setting.from(wantIt, Setting.INHERITED); 729 edited(); 730 } 731 732 733 737 public final Setting getOwnTimestamp() 738 { 739 if (isReference()) { 740 return getOtherEC().getOwnTimestamp(); 741 } 742 return m_timestampPref; 743 } 744 745 746 751 public boolean wantTimestamp() 752 { 753 if (isReference()) { 754 return getOtherEC().wantTimestamp(); 755 } 756 switch (getOwnTimestamp().getIndex()) { 757 case Setting.ON_INDEX: { 758 return true; 759 } 760 case Setting.OFF_INDEX:{ 761 return false; 762 } 763 } 764 return getDefaults().wantTimestamp(); 765 } 766 767 771 779 public DiagnosticsEmitter getEmitter() 780 { 781 if (isReference()) { 782 return getOtherEC().getEmitter(); 783 } 784 String from = getFrom(); 785 boolean root = Tk.isWhitespace(from); 786 787 if (getOwnEmitterFactory()!=null) { 788 if (root) { 789 return getOwnEmitterFactory().newEmitter(); 790 } 791 return getOwnEmitterFactory().newEmitter(from); 792 } else if (root) { 793 return getDefaults().getEmitter(); 794 } else { 795 return getDefaults().getCustomEmitter(from); 796 } 797 } 798 799 800 808 public DiagnosticsEmitter getCustomEmitter(String grpId) 809 { 810 if (isReference()) { 811 return getOtherEC().getCustomEmitter(grpId); 812 } 813 if (getOwnEmitterFactory()!=null) { 814 return getOwnEmitterFactory().newEmitter(grpId); 815 } 816 return getDefaults().getCustomEmitter(grpId); 817 } 818 819 820 826 public void setEmitterFactory(Class emitterFactoryClass) 827 { 828 require_(emitterFactoryClass!=null,"setEmitFctry- nonzro clas"); 829 if (isReference()) { 830 throw tooManyAttributes(); 831 } 832 try { 833 m_emitterFactory = (DiagnosticsEmitterFactory) 834 emitterFactoryClass.newInstance(); 835 } catch (Exception anyX) { 836 log(AntX.uistrs().get("emit.invalid.factory", 837 emitterFactoryClass.getName()), 838 Project.MSG_ERR); 839 throw new BuildException(anyX); 840 } 841 edited(); 842 } 843 844 845 850 public final DiagnosticsEmitterFactory getOwnEmitterFactory() 851 { 852 if (isReference()) { 853 return getOtherEC().getOwnEmitterFactory(); 854 } 855 return m_emitterFactory; 856 } 857 858 859 860 private void verifyNotCircular(EmitConfiguration ec) 861 { 862 if (ec==this || ec==m_controller) { 864 String ermsg = uistrs().get("emit.circular.defaults"); 865 log(ermsg, Project.MSG_ERR); 866 throw new BuildException(ermsg); 867 } 868 } 869 870 871 873 private String m_Id; 874 private Reference m_defaultsRef; 875 private EmitConfiguration m_defaultsEC; 877 private NoiseLevel m_noiseLevel; private String m_propertyNames; private Setting m_echoPref = Setting.INHERITED; private Setting m_timestampPref=Setting.INHERITED; private String m_timestampFormat; private DateFormat m_df; 884 private String m_grpId; private String m_wrt="", m_wrtImpl; private DiagnosticsEmitterFactory m_emitterFactory; private String m_emitterSeparator; private Object m_controller;} 890 891 892 | Popular Tags |