1 28 29 package com.idaremedia.antx.feedback; 30 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 import org.apache.tools.ant.BuildEvent; 35 import org.apache.tools.ant.BuildException; 36 import org.apache.tools.ant.Project; 37 import org.apache.tools.ant.Target; 38 import org.apache.tools.ant.Task; 39 import org.apache.tools.ant.types.Reference; 40 import org.apache.tools.ant.types.RegularExpression; 41 import org.apache.tools.ant.util.RegexpPatternMapper; 42 import org.apache.tools.ant.util.regexp.Regexp; 43 44 import com.idaremedia.antx.AntX; 45 import com.idaremedia.antx.AntXFixture; 46 import com.idaremedia.antx.AssertableDataType; 47 import com.idaremedia.antx.FixtureComponent; 48 import com.idaremedia.antx.apis.Nameable; 49 import com.idaremedia.antx.helpers.Tk; 50 import com.idaremedia.antx.parameters.IgnoreCaseEnabled; 51 52 87 88 public class GroupingMapper extends AssertableDataType 89 implements FixtureComponent, Cloneable 90 { 91 109 public static class Item extends AssertableDataType 110 implements Cloneable , IgnoreCaseEnabled 111 { 112 private static final String ONLY_ONE_ERR= "task.only.oneof.attr"; 113 114 115 120 public Item() 121 { 122 super(AntX.feedback); 123 } 124 125 130 public Object clone() 131 { 132 try { 133 Item cloned = (Item)super.clone(); 134 if (m_re!=null) { 135 cloned.setLike(getLike()); 136 } 137 return cloned; 138 } catch(CloneNotSupportedException clnx) { 139 throw new Error (uistrs().get(AntX.CLONE_BROKEN_MSGID)); 140 } 141 } 142 143 147 public void setId(String id) 148 { 149 m_Id= id; 150 } 151 152 156 public String getId() 157 { 158 if (m_Id!=null) { 159 return m_Id; 160 } 161 return super.getId(); 162 } 163 164 169 public void setType(String f) 170 { 171 require_(f!=null,"setTyp- nonzro fld"); 172 m_field = MainField.from(f,MainField.UNKNOWN); } 174 175 180 public final MainField getType() 181 { 182 return m_field; 183 } 184 185 192 public void setName(String name) 193 { 194 require_(name!=null,"setNam- nonzro nam"); 195 if (m_re!=null || m_names!=null) { 196 String error = uistrs().get(ONLY_ONE_ERR,"name|names|like"); 197 throw new BuildException(error); 198 } 199 m_name = name; 200 } 201 202 206 public final String getName() 207 { 208 return m_name; 209 } 210 211 218 public void setNames(String list) 219 { 220 require_(list!=null,"setNamLst- nonzro liststr"); 221 if (m_re!=null || m_name!=null) { 222 String error = uistrs().get(ONLY_ONE_ERR,"name|names|like"); 223 throw new BuildException(error); 224 } 225 List l= Tk.splitList(list); 226 if (l.isEmpty()) { 227 throw new BuildException(uistrs().get("emit.mapr.bad.namelist")); 228 } 229 m_names= (String [])l.toArray(new String [l.size()]); 230 l=null; 231 } 232 233 239 public final String [] getNames() 240 { 241 String [] copy=null; 242 if (m_names!=null) { 243 copy= new String [m_names.length]; 244 for (int i=0;i<copy.length;i++) { 245 copy[i]= m_names[i]; 246 } 247 } 248 return copy; 249 } 250 251 259 public void setLike(String rexp) 260 { 261 require_(rexp!=null,"setRE- nonzro RE"); 262 if (m_name!=null || m_names!=null) { 263 String error = uistrs().get(ONLY_ONE_ERR,"name|names|like"); 264 throw new BuildException(error); 265 } 266 m_re = new RegularExpression(); 267 m_re.setPattern(rexp); 268 } 269 270 274 public final String getLike() 275 { 276 return (m_re!=null) ? m_re.getPattern(getProject()) : null; 277 } 278 279 283 public final Regexp getLikeRE() 284 { 285 return (m_re!=null) ? m_re.getRegexp(getProject()) : null; 286 } 287 288 293 public void setLabel(String label) 294 { 295 require_(label!=null,"setLbl- nonzro lbl"); 296 if (m_isMapper!=null) { 297 String error = uistrs().get(ONLY_ONE_ERR,"label|labelexpr"); 298 throw new BuildException(error); 299 } 300 m_label = label; 301 m_isMapper = Boolean.FALSE; 302 } 303 304 310 public void setLabelExpr(String re) 311 { 312 require_(re!=null,"setLblExpr- nonzro re"); 313 if (m_isMapper!=null) { 314 String error = uistrs().get(ONLY_ONE_ERR,"label|labelexpr"); 315 throw new BuildException(error); 316 } 317 m_label = re; 318 m_isMapper = Boolean.TRUE; 319 } 320 321 322 326 public String getLabel() 327 { 328 return m_label; 329 } 330 331 336 public String isMatch(String s) 337 { 338 if (s==null) { 339 return null; 340 } 341 if (m_isMapper!=null && m_isMapper.booleanValue()) { 342 RegexpPatternMapper helper= new RegexpPatternMapper(); 343 helper.setTo(getLabel()); 344 helper.setFrom(getLike()); 345 String [] rslt = helper.mapFileName(s); 346 helper = null; 347 return (rslt!=null) ? rslt[0] : null; 348 } 349 350 boolean hit = false;; 351 Regexp RE = getLikeRE(); 352 if (RE!=null) { 353 hit= RE.matches(s,isIgnoreCase() ? Regexp.MATCH_CASE_INSENSITIVE: 0); 354 } 355 else if (m_names!=null) { 356 boolean caseins= isIgnoreCase(); 357 for (int i=0;i<m_names.length && !hit;i++) { 358 if (caseins) { 359 if (m_names[i].equalsIgnoreCase(s)) { 360 hit = true; 361 } 362 } else { 363 if (m_names[i].equals(s)) { 364 hit = true; 365 } 366 } 367 } 368 } 369 else { 370 hit = isIgnoreCase() ? m_name.equalsIgnoreCase(s) : m_name.equals(s); 371 } 372 return hit ? getLabel() : null; 373 } 374 375 380 public void setIgnoreCase(boolean ignore) 381 { 382 m_ignoreCase = ignore; 383 } 384 385 389 public boolean isIgnoreCase() 390 { 391 return m_ignoreCase; 392 } 393 394 398 public void setRefid(String xxx) 399 { 400 throw new BuildException 401 (uistrs().get("task.unsupported.attr","mapping/item", 402 "refid")); 403 } 404 405 private String m_Id; 406 private MainField m_field= MainField.UNKNOWN; 407 private String m_label= ""; 408 private String m_name, m_names[]; 409 private RegularExpression m_re; 410 private boolean m_ignoreCase; 411 private Boolean m_isMapper; 412 } 413 414 418 421 public GroupingMapper() 422 { 423 super(AntX.feedback); 424 } 425 426 427 428 432 protected final GroupingMapper getOtherMapper() 433 { 434 return (GroupingMapper)getCheckedRef(GroupingMapper.class,"mappings"); 435 } 436 437 438 439 445 public Object clone() 446 { 447 if (isReference()) { 448 return getOtherMapper().clone(); 449 } 450 try { 451 GroupingMapper cloned = (GroupingMapper)super.clone(); 452 cloned.m_items = AntXFixture.newListCopy(m_items); 453 for (int i=0,N=cloned.m_items.size();i<N;i++) { 454 cloned.m_items.set(i,((Item)m_items.get(i)).clone()); 455 } 456 return cloned; 457 } catch(CloneNotSupportedException clnx) { 458 throw new Error (uistrs().get(AntX.CLONE_BROKEN_MSGID)); 459 } 460 } 461 462 463 464 468 public void setId(String id) 469 { 470 m_Id= id; 471 } 472 473 474 475 478 public final String getId() 479 { 480 if (m_Id!=null) { 481 return m_Id; 482 } 483 if (isReference()) { 484 return getOtherMapper().getId(); 485 } 486 return super.getId(); 487 } 488 489 490 491 497 public void setInherit(Reference refid) 498 { 499 require_(refid!=null,"setInherited- nonzro refid"); 500 m_referToMapper = refid; 501 } 502 503 504 505 511 public synchronized final GroupingMapper getInheritedMapper() 512 { 513 if (m_referToMapper instanceof Reference) { 514 m_referToMapper = getReferencedObject 515 (null, ((Reference)m_referToMapper).getRefId(), 516 GroupingMapper.class); 517 518 verify_(m_referToMapper!=this,"inherit- not circular"); 520 } 521 return (GroupingMapper)m_referToMapper; 522 } 523 524 525 526 531 public void addConfiguredMapping(Item itm) 532 { 533 require_(itm!=null,"addMapin- nonzro itm"); 534 if (isReference()) { 535 throw tooManyAttributes(); 536 } 537 getItems().add(itm); 538 edited(); 539 } 540 541 542 543 546 public final boolean isEmpty() 547 { 548 if (isReference()) { 549 return getOtherMapper().isEmpty(); 550 } 551 return m_items.isEmpty(); 552 } 553 554 555 556 560 protected final List getItems() 561 { 562 return m_items; 563 } 564 565 566 570 571 575 private static String projectName(Project project) 576 { 577 String pn = project.getName(); 578 if (pn==null) { 579 pn = project.getProperty("ant.project.name"); 580 if (pn==null) { 581 pn= "project@"+System.identityHashCode(project); 582 } 583 } 584 return pn; 585 } 586 587 588 589 596 public boolean isMatch(Item itm, BuildEvent e, IndicatorZone iz) 597 { 598 require_(itm!=null && e!=null,"isMatch- nonzro event+item"); 599 require_(iz!=null,"isMatch- nonzro izone"); 600 601 if (isReference()) { 602 return getOtherMapper().isMatch(itm,e,iz); 603 } 604 605 return isMatch_(itm,e,iz)!=null; 606 } 607 608 609 610 615 protected final String isMatch_(Item itm, BuildEvent e, IndicatorZone iz) 616 { 617 switch(itm.getType().getIndex()) { 618 case MainField.TASK_INDEX: { 619 if (e.getTask()!=null) { 620 return itm.isMatch(e.getTask().getTaskName()); 621 } 622 break; 623 } 624 case MainField.TARGET_INDEX: { 625 if (e.getTarget()!=null) { 626 return itm.isMatch(e.getTarget().getName()); 627 } 628 break; 629 } 630 case MainField.PROJECT_INDEX: { 631 if (e.getProject()!=null) { 632 return itm.isMatch(projectName(e.getProject())); 633 } 634 break; 635 } 636 case MainField.INDICATOR_INDEX: { 637 return itm.isMatch(iz.getValue()); 638 } 639 case MainField.MESSAGE_INDEX: { 640 if (e.getMessage()!=null) { 641 return itm.isMatch(e.getMessage()); 642 } 643 break; 644 } 645 } return null; 647 } 648 649 653 661 public String getLabel(MainField want, BuildEvent e, IndicatorZone iz) 662 { 663 require_(want!=null && e!=null,"getLabel- nonzro event+field"); 664 require_(iz!=null,"isMatch- nonzro izone"); 665 666 if (isReference()) { 667 return getOtherMapper().getLabel(want,e,iz); 668 } 669 return getLabel_(want,e,iz); 670 } 671 672 673 674 679 protected final String getLabel_(MainField want, BuildEvent e, IndicatorZone iz) 680 { 681 if (!m_items.isEmpty()) { 682 Iterator itr= m_items.iterator(); 683 while (itr.hasNext()) { 684 Item itm = (Item)itr.next(); 685 if (itm.getType()==want) { 686 String label = isMatch_(itm, e, iz); 687 if (label!=null) { 688 return label; 689 } 690 } 691 } 692 } 693 if (getInheritedMapper()!=null) { 694 return getInheritedMapper().getLabel_(want,e,iz); 695 } 696 return null; 697 } 698 699 700 701 712 public String pathFrom(BuildEvent e, IndicatorZone iz, 713 int allowed, String fieldseparator) 714 { 715 require_(e!=null && iz!=null, "pathFrom- nonzro event+izone"); 716 717 if (isReference()) { 718 return getOtherMapper().pathFrom(e,iz,allowed,fieldseparator); 719 } 720 721 final String SEP= (fieldseparator!=null) ? fieldseparator : 722 DefaultEmitConfiguration.INSTANCE.getGroupingPathSeparator(); 723 724 StringBuffer sb = AntXFixture.newStringBuffer(); 725 String field; 726 727 sb.append(indicatorLabel(e,iz,allowed)); 729 if ((allowed&MainField.INCLUDE_PROJECT)!=0) { 730 field = projectLabel(e,iz,allowed); 731 if (field!=null) { 732 sb.append(SEP); 733 sb.append(field); 734 } 735 } 736 if ((allowed&MainField.INCLUDE_TARGET)!=0) { 737 field = targetLabel(e,iz,allowed); 738 if (field!=null) { 739 sb.append(SEP); 740 sb.append(field); 741 } 742 } 743 if ((allowed&MainField.INCLUDE_TASK)!=0) { 744 field = taskLabel(e,iz,allowed); 745 if (field!=null) { 746 sb.append(SEP); 747 sb.append(field); 748 } 749 } 750 751 return sb.substring(0); 752 } 753 754 755 756 767 public String pathFromAllFields(BuildEvent e, IndicatorZone iz, 768 int allowed, String fieldseparator) 769 { 770 require_(e!=null && iz!=null, "pathFrom- nonzro event+izone"); 771 772 if (isReference()) { 773 return getOtherMapper().pathFrom(e,iz,allowed,fieldseparator); 774 } 775 776 final String SEP= (fieldseparator!=null) ? fieldseparator : 777 DefaultEmitConfiguration.INSTANCE.getGroupingPathSeparator(); 778 779 StringBuffer sb = AntXFixture.newStringBuffer(); 780 781 sb.append(indicatorLabel(e,iz,allowed)); 782 783 if ((allowed&MainField.INCLUDE_PROJECT)!=0) { 784 sb.append(SEP); 785 sb.append(projectLabelNoNull(e,iz,allowed)); 786 } 787 if ((allowed&MainField.INCLUDE_TARGET)!=0) { 788 sb.append(SEP); 789 sb.append(targetLabelNoNull(e,iz,allowed)); 790 } 791 if ((allowed&MainField.INCLUDE_TASK)!=0) { 792 sb.append(SEP); 793 sb.append(taskLabelNoNull(e,iz,allowed)); 794 } 795 796 return sb.substring(0); 797 } 798 799 800 803 private String indicatorLabel(BuildEvent e, IndicatorZone iz, int mask) 804 { 805 String s = getLabel_(MainField.INDICATOR,e,iz); 806 return (s!=null) ? s : iz.getValue(); 807 } 808 809 810 814 private String projectLabel(BuildEvent e, IndicatorZone iz, int mask) 815 { 816 Project myproject = e.getProject(); 817 if (myproject==null) { 818 return null; 819 } 820 String s = getLabel_(MainField.PROJECT,e,iz); 821 if (s==null) { 822 s = projectName(myproject); 823 } 824 return s; 825 } 826 827 828 832 private String projectLabelNoNull(BuildEvent e, IndicatorZone iz, int mask) 833 { 834 String label = projectLabel(e,iz,mask); 835 if (label==null) { 836 label = uistrs().get("emit.unknown.project.grp"); 837 } 838 return label; 839 } 840 841 842 846 private String targetLabel(BuildEvent e, IndicatorZone iz, int mask) 847 { 848 Target target = e.getTarget(); 849 if (target==null) { 850 return null; 851 } 852 String s = getLabel_(MainField.TARGET,e,iz); 853 if (s==null) { 854 s = target.getName(); 855 if (s==null) { 856 s = "target@"+System.identityHashCode(target); 857 } 858 } 859 return s; 860 } 861 862 863 867 private String targetLabelNoNull(BuildEvent e, IndicatorZone iz, int mask) 868 { 869 String label = targetLabel(e,iz,mask); 870 if (label==null) { 871 label = uistrs().get("emit.unknown.target.grp"); 872 } 873 return label; 874 } 875 876 877 881 private String taskLabel(BuildEvent e, IndicatorZone iz, int mask) 882 { 883 Task task = e.getTask(); 884 if (task==null) { 885 return null; 886 } 887 String s = getLabel_(MainField.TASK,e,iz); 888 if (s==null) { 889 if ((task instanceof Nameable) && (mask&MainField.INCLUDE_NESTED)!=0) { 890 String name = ((Nameable)task).getName(); 891 if (name!=null) { 892 return name; 893 } 894 } 895 if (task.getTaskName()==null) { 896 return "task@"+System.identityHashCode(task); 897 } 898 return task.getTaskName(); 899 } 900 return s; 901 } 902 903 904 908 private String taskLabelNoNull(BuildEvent e, IndicatorZone iz, int mask) 909 { 910 String label = taskLabel(e,iz,mask); 911 if (label==null) { 912 label = uistrs().get("emit.unknown.task.grp"); 913 } 914 return label; 915 } 916 917 919 private List m_items = AntXFixture.newList(); 920 private String m_Id; 921 private Object m_referToMapper; 922 } 923 924 925 | Popular Tags |