1 17 package org.eclipse.emf.codegen.jmerge; 18 19 20 import java.io.BufferedInputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.lang.reflect.InvocationTargetException ; 24 import java.lang.reflect.Method ; 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.Collections ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.StringTokenizer ; 35 import java.util.regex.Matcher ; 36 import java.util.regex.Pattern ; 37 38 import org.eclipse.core.runtime.IPlatformRunnable; 39 import org.eclipse.core.runtime.IProgressMonitor; 40 import org.eclipse.core.runtime.NullProgressMonitor; 41 import org.eclipse.jdt.core.jdom.DOMFactory; 42 import org.eclipse.jdt.core.jdom.IDOMCompilationUnit; 43 import org.eclipse.jdt.core.jdom.IDOMField; 44 import org.eclipse.jdt.core.jdom.IDOMImport; 45 import org.eclipse.jdt.core.jdom.IDOMInitializer; 46 import org.eclipse.jdt.core.jdom.IDOMMember; 47 import org.eclipse.jdt.core.jdom.IDOMMethod; 48 import org.eclipse.jdt.core.jdom.IDOMNode; 49 import org.eclipse.jdt.core.jdom.IDOMPackage; 50 import org.eclipse.jdt.core.jdom.IDOMType; 51 52 53 57 public class JMerger implements IPlatformRunnable 58 { 59 protected DOMFactory jdomFactory = new DOMFactory(); 60 protected JControlModel jControlModel; 61 protected IDOMCompilationUnit sourceCompilationUnit; 62 protected IDOMCompilationUnit targetCompilationUnit; 63 protected JPatternDictionary sourcePatternDictionary; 64 protected JPatternDictionary targetPatternDictionary; 65 protected Map sourceToTargetMap = new HashMap (); 66 protected Map targetToSourceMap = new HashMap (); 67 protected Map orderedSourceChildrenMap = new HashMap (); 68 protected boolean isBlocked; 69 70 73 public JMerger() 74 { 75 } 76 77 public JMerger(JControlModel jControlModel, IDOMCompilationUnit sourceCompilationUnit, IDOMCompilationUnit targetCompilationUnit) 78 { 79 this.jControlModel = jControlModel; 80 setSourceCompilationUnit(sourceCompilationUnit); 81 setTargetCompilationUnit(targetCompilationUnit); 82 } 83 84 public void merge() 85 { 86 pullTargetCompilationUnit(); 87 if (!isBlocked) 88 { 89 pushSourceCompilationUnit(); 90 sweepTargetCompilationUnit(); 91 sortTargetCompilationUnit(); 92 } 93 } 94 95 public void remerge() 96 { 97 sourceToTargetMap.clear(); 98 targetToSourceMap.clear(); 99 orderedSourceChildrenMap.clear(); 100 isBlocked = false; 101 merge(); 102 } 103 104 public JControlModel getControlModel() 105 { 106 return jControlModel; 107 } 108 109 public void setControlModel(JControlModel jControlModel) 110 { 111 this.jControlModel = jControlModel; 112 } 113 114 public String getSourceCompilationUnitContents() 115 { 116 return sourceCompilationUnit.getContents(); 117 } 118 119 public IDOMCompilationUnit getSourceCompilationUnit() 120 { 121 return sourceCompilationUnit; 122 } 123 124 public void setSourceCompilationUnit(IDOMCompilationUnit sourceCompilationUnit) 125 { 126 this.sourceCompilationUnit = sourceCompilationUnit; 127 sourcePatternDictionary = new JPatternDictionary(sourceCompilationUnit, jControlModel); 128 } 131 132 public String getTargetCompilationUnitContents() 133 { 134 return targetCompilationUnit.getContents(); 135 } 136 137 public IDOMCompilationUnit getTargetCompilationUnit() 138 { 139 return targetCompilationUnit; 140 } 141 142 public void setTargetCompilationUnit(IDOMCompilationUnit targetCompilationUnit) 143 { 144 this.targetCompilationUnit = targetCompilationUnit; 145 targetPatternDictionary = new JPatternDictionary(targetCompilationUnit, jControlModel); 146 } 149 150 public JPatternDictionary getSourcePatternDictionary() 151 { 152 return sourcePatternDictionary; 153 } 154 155 public void setSourcePatternDictionary(JPatternDictionary sourcePatternDictionary) 156 { 157 this.sourcePatternDictionary = sourcePatternDictionary; 158 } 159 160 public JPatternDictionary getTargetPatternDictionary() 161 { 162 return targetPatternDictionary; 163 } 164 165 public void setTargetPatternDictionary(JPatternDictionary targetPatternDictionary) 166 { 167 this.targetPatternDictionary = targetPatternDictionary; 168 } 169 170 public Map getSourceToTargetMap() 171 { 172 return sourceToTargetMap; 173 } 174 175 public void setSourceToTargetMap(Map sourceToTargetMap) 176 { 177 this.sourceToTargetMap = sourceToTargetMap; 178 } 179 180 183 public IDOMCompilationUnit createCompilationUnitForURI(String uri) 184 { 185 try 186 { 187 URL url = null; 188 try 189 { 190 url = new URL (uri); 191 } 192 catch (MalformedURLException exception) 193 { 194 url = new URL ("file:" + uri); 195 } 196 if (url != null) 197 { 198 BufferedInputStream bufferedInputStream = new BufferedInputStream (url.openStream()); 199 byte [] input = new byte [bufferedInputStream.available()]; 200 bufferedInputStream.read(input); 201 bufferedInputStream.close(); 202 return jdomFactory.createCompilationUnit(new String (input), url.toString()); 203 } 204 } 205 catch (IOException exception) 206 { 207 } 209 210 return null; 211 } 212 213 public IDOMCompilationUnit createCompilationUnitForInputStream(InputStream inputStream) 214 { 215 try 216 { 217 BufferedInputStream bufferedInputStream = new BufferedInputStream (inputStream); 218 byte [] input = new byte [bufferedInputStream.available()]; 219 bufferedInputStream.read(input); 220 bufferedInputStream.close(); 221 return jdomFactory.createCompilationUnit(new String (input), "NAME"); 222 } 223 catch (IOException exception) 224 { 225 } 227 return null; 228 } 229 230 233 public IDOMCompilationUnit createCompilationUnitForContents(String contents) 234 { 235 return jdomFactory.createCompilationUnit(contents, "NAME"); 236 } 237 238 239 241 protected void pullTargetCompilationUnit() 242 { 243 if (targetCompilationUnit == null) 244 { 245 setTargetCompilationUnit((IDOMCompilationUnit)insertClone(sourceCompilationUnit)); 246 } 247 else 248 { 249 map(sourceCompilationUnit, targetCompilationUnit); 250 applyPullRules(sourceCompilationUnit, targetCompilationUnit); 251 252 262 for (IDOMNode child = targetCompilationUnit.getFirstChild(); child != null; child = child.getNextNode()) 263 { 264 switch (child.getNodeType()) 265 { 266 case IDOMNode.PACKAGE: 267 { 268 pullTargetPackage((IDOMPackage)child); 269 break; 270 } 271 case IDOMNode.IMPORT: 272 { 273 pullTargetImport((IDOMImport)child); 274 break; 275 } 276 case IDOMNode.TYPE: 277 { 278 IDOMType type = (IDOMType)child; 279 isBlocked = 280 jControlModel.getBlockPattern() != null && 281 type.getComment() != null && 282 jControlModel.getBlockPattern().matcher(type.getComment()).find(); 283 if (!isBlocked) 284 { 285 pullTargetType(type); 286 } 287 break; 288 } 289 } 290 } 291 } 292 } 293 294 protected void pullTargetPackage(IDOMPackage targetPackage) 295 { 296 IDOMPackage sourcePackage = sourcePatternDictionary.getPackage(); 297 map(sourcePackage, targetPackage); 298 applyPullRules(sourcePackage, targetPackage); 299 300 305 } 306 307 protected void pullTargetImport(IDOMImport targetImport) 308 { 309 IDOMImport sourceImport = 310 (IDOMImport)sourcePatternDictionary.getImportMap().get(targetPatternDictionary.getQualifiedName(targetImport)); 311 map(sourceImport, targetImport); 312 if (sourceImport != null) 313 { 314 applyPullRules(sourceImport, targetImport); 315 } 316 } 317 318 protected void pullTargetType(IDOMType targetType) 319 { 320 IDOMType sourceType = (IDOMType)sourcePatternDictionary.getTypeMap().get(targetPatternDictionary.getQualifiedName(targetType)); 321 322 map(sourceType, targetType); 323 if (sourceType != null) 324 { 325 applyPullRules(sourceType, targetType); 326 327 336 365 } 366 367 for (IDOMNode child = targetType.getFirstChild(); child != null; child = child.getNextNode()) 368 { 369 switch (child.getNodeType()) 370 { 371 case IDOMNode.INITIALIZER: 372 { 373 pullTargetInitializer((IDOMInitializer)child); 374 break; 375 } 376 case IDOMNode.FIELD: 377 { 378 pullTargetField((IDOMField)child); 379 break; 380 } 381 case IDOMNode.METHOD: 382 { 383 pullTargetMethod((IDOMMethod)child); 384 break; 385 } 386 case IDOMNode.TYPE: 387 { 388 pullTargetType((IDOMType)child); 389 break; 390 } 391 } 392 } 393 } 394 395 protected void pullTargetInitializer(IDOMInitializer targetInitializer) 396 { 397 IDOMInitializer sourceInitializer = 398 (IDOMInitializer)sourcePatternDictionary.getInitializerMap().get(targetPatternDictionary.getQualifiedName(targetInitializer)); 399 map(sourceInitializer, targetInitializer); 400 if (sourceInitializer != null) 401 { 402 applyPullRules(sourceInitializer, targetInitializer); 403 404 413 414 423 } 424 } 425 426 protected void pullTargetField(IDOMField targetField) 427 { 428 IDOMField sourceField = 429 (IDOMField)sourcePatternDictionary.getFieldMap().get(targetPatternDictionary.getQualifiedName(targetField)); 430 431 map(sourceField, targetField); 432 if (sourceField != null) 433 { 434 applyPullRules(sourceField, targetField); 435 436 445 446 468 } 469 } 470 471 protected void pullTargetMethod(IDOMMethod targetMethod) 472 { 473 String qualifiedTargetMethodName = targetPatternDictionary.getQualifiedName(targetMethod); 474 IDOMMethod sourceMethod = 475 (IDOMMethod)sourcePatternDictionary.getMethodMap().get(qualifiedTargetMethodName); 476 477 if (sourceMethod == null && 478 jControlModel.getRedirect() != null && 479 targetMethod.getName() != null && 480 targetMethod.getName().endsWith(jControlModel.getRedirect())) 481 { 482 int index = qualifiedTargetMethodName.indexOf("("); qualifiedTargetMethodName = 484 qualifiedTargetMethodName.substring(0, index - jControlModel.getRedirect().length()) + 485 qualifiedTargetMethodName.substring(index); 486 sourceMethod = 487 (IDOMMethod)sourcePatternDictionary.getMethodMap().get(qualifiedTargetMethodName); 488 } 489 490 map(sourceMethod, targetMethod); 491 if (sourceMethod != null) 492 { 493 applyPullRules(sourceMethod, targetMethod); 494 495 504 505 527 528 548 } 549 } 550 551 protected static String lineSeparator; 555 static 556 { 557 StringBuffer result = new StringBuffer (); 558 String s = System.getProperty("line.separator"); 559 for (int i = 0, len = s.length(); i < len; i++) 560 { 561 char c = s.charAt(i); 562 if (c == '\r') result.append("\\r"); 563 else if (c == '\n') result.append("\\n"); 564 else throw new RuntimeException ("Unexpected line separator character"); 565 } 566 lineSeparator = result.toString(); 567 } 568 569 protected static Pattern braceLine = Pattern.compile("(\\s*" + lineSeparator + "\\s*\\{\\s*)" + lineSeparator); protected static Pattern leadingTabs = Pattern.compile("^((\\t)+).*$", Pattern.MULTILINE); 571 572 public static abstract class FindAndReplace 573 { 574 protected Pattern pattern; 575 protected String string; 576 protected StringBuffer stringBuffer; 577 protected int current; 578 579 public FindAndReplace(Pattern pattern) 580 { 581 this.pattern = pattern; 582 } 583 584 public String apply(String string) 585 { 586 current = 0; 587 this.string = string; 588 this.stringBuffer = new StringBuffer (); 589 590 for (int start = 0, end = string.length(); start < end; ) 591 { 592 Matcher matcher = pattern.matcher(string.subSequence(start, end)); 593 if (matcher.find()) 594 { 595 if (!handleMatch(start, matcher)) 596 { 597 break; 598 } 599 start += matcher.end(); 600 } 601 else 602 { 603 break; 604 } 605 } 606 607 stringBuffer.append(string.substring(current)); 608 return stringBuffer.toString(); 609 } 610 611 public void replace(int begin, int end, String replacement) 612 { 613 stringBuffer.append(string.substring(current, begin)); 614 stringBuffer.append(replacement); 615 current = end; 616 } 617 618 public abstract boolean handleMatch(int offset, Matcher matcher); 619 } 620 621 protected String applyFormatRules(String value) 622 { 623 final String tabReplacement = jControlModel.getLeadingTabReplacement(); 624 if (tabReplacement != null) 625 { 626 FindAndReplace findAndReplaceLeadingTabs = 627 new FindAndReplace(leadingTabs) 628 { 629 public boolean handleMatch(int offset, Matcher matcher) 630 { 631 if (matcher.groupCount() >= 1) 632 { 633 int begin = offset + matcher.start(1); 634 int end = offset + matcher.end(1); 635 StringBuffer replacement = new StringBuffer (); 636 for (int i = begin; i < end; ++i) 637 { 638 replacement.append(tabReplacement); 639 } 640 replace(begin, end, replacement.toString()); 641 } 642 return true; 643 } 644 }; 645 value = findAndReplaceLeadingTabs.apply(value); 646 } 647 648 if (jControlModel.convertToStandardBraceStyle()) 649 { 650 FindAndReplace findAndReplaceLineWithJustABrace = 651 new FindAndReplace(braceLine) 652 { 653 public boolean handleMatch(int offset, Matcher matcher) 654 { 655 if (matcher.groupCount() >= 1) 656 { 657 int begin = offset + matcher.start(1); 658 659 if (current != 0 && (begin <= current || string.charAt(begin - 1) == ';')) 663 { 664 return true; 665 } 666 667 for (int i = begin - 1; i >= current; --i) 670 { 671 char character = string.charAt(i); 672 if (character == '\n' || character == '\r') 673 { 674 boolean slash = false; 675 while (++i < begin) 676 { 677 character = string.charAt(i); 678 if (character == '/') 679 { 680 if (slash) 681 { 682 return true; 683 } 684 slash = true; 685 } 686 else 687 { 688 slash = false; 689 } 690 } 691 692 break; 693 } 694 } 695 696 int end = offset + matcher.end(1); 697 replace(begin, end, " {"); } 699 return true; 700 } 701 }; 702 value = findAndReplaceLineWithJustABrace.apply(value); 703 } 704 705 return value; 706 } 707 708 protected static Object [] noArguments = new Object [0]; 709 protected void applyPullRules(IDOMNode sourceNode, IDOMNode targetNode) 710 { 711 try 712 { 713 LOOP: 714 for (Iterator pullRules = jControlModel.getPullRules().iterator(); pullRules.hasNext(); ) 715 { 716 JControlModel.PullRule pullRule = (JControlModel.PullRule)pullRules.next(); 717 if (sourcePatternDictionary.isMarkedUp(pullRule.getSourceMarkup(), sourceNode) && 718 targetPatternDictionary.isMarkedUp(pullRule.getTargetMarkup(), targetNode) && 719 pullRule.getSourceGetFeature().getFeatureClass().isInstance(sourceNode) && 720 pullRule.getTargetPutFeature().getFeatureClass().isInstance(targetNode)) 721 { 722 Method sourceGetMethod = pullRule.getSourceGetFeature().getFeatureMethod(); 723 Object value = sourceGetMethod.invoke(sourceNode, noArguments); 724 Method targetPutMethod = pullRule.getTargetPutFeature().getFeatureMethod(); 725 if (!sourceGetMethod.getReturnType().isArray() || 726 targetPutMethod.getParameterTypes()[0].isAssignableFrom(sourceGetMethod.getReturnType())) 727 { 728 if (value instanceof String ) 729 { 730 String stringValue = (String )value; 731 stringValue = applyFormatRules(stringValue); 732 Pattern sourceTransfer = pullRule.getSourceTransfer(); 733 if (sourceTransfer != null) 734 { 735 String oldStringValue = (String )sourceGetMethod.invoke(targetNode, noArguments); 736 Matcher sourceMatcher = sourceTransfer.matcher(stringValue); 737 Matcher targetMatcher = sourceTransfer.matcher(oldStringValue); 738 if (sourceMatcher.groupCount() >= 1 && targetMatcher.groupCount() >= 1) 739 { 740 StringBuffer result = new StringBuffer (); 741 int index = 0; 742 while (sourceMatcher.find() && targetMatcher.find()) 743 { 744 result.append(stringValue.substring(index, sourceMatcher.start(1))); 745 result.append(targetMatcher.group(1)); 746 index = sourceMatcher.end(1); 747 } 748 if (result.length() == 0) 751 { 752 stringValue = null; 753 } 754 else 755 { 756 result.append(stringValue.substring(index)); 757 stringValue = result.toString(); 758 } 759 } 760 else 761 { 762 stringValue = null; 763 } 764 } 765 value = stringValue; 766 } 767 if (value != null || 768 targetPutMethod.getName().equals("setInitializer") || 769 targetPutMethod.getName().equals("setSuperclass") || 770 targetPutMethod.getName().equals("setExceptions")) 771 { 772 Object oldValue = sourceGetMethod.invoke(targetNode, noArguments); 775 if (value == null ? oldValue == null : value.equals(oldValue)) 776 { 777 continue; 778 } 779 else if (targetPutMethod.getName().equals("setSuperclass")) 780 { 781 if (oldValue != null && value != null && ((String )oldValue).trim().equals(((String )value).trim())) 782 { 783 continue; 784 } 785 } 786 787 if (sourceGetMethod.getName().equals("getReturnType") && 791 jControlModel.getBlockPattern() != null && 792 ((IDOMMethod)targetNode).getComment() != null && 793 jControlModel.getBlockPattern().matcher(((IDOMMethod)targetNode).getComment()).find()) 794 { 795 continue; 796 } 797 798 targetPutMethod.invoke(targetNode, new Object [] { value }); 799 if (targetPutMethod.getName().equals("setBody") && sourceNode instanceof IDOMMethod) 800 { 801 IDOMMethod sourceMethod = (IDOMMethod)sourceNode; 802 IDOMMethod targetMethod = (IDOMMethod)targetNode; 803 String [] sourceParameterNames = sourceMethod.getParameterNames(); 804 String [] targetParameterTypes = targetMethod.getParameterTypes(); 805 targetMethod.setParameters(targetParameterTypes, sourceParameterNames); 806 } 807 } 808 } 809 else 810 { 811 ArrayList additionalStrings = new ArrayList (); 812 String [] sourceStrings = (String [])value; 813 if (sourceStrings != null) 814 { 815 additionalStrings.addAll(Arrays.asList(sourceStrings)); 816 } 817 818 if (targetPutMethod.getName().equals("addSuperInterface")) 819 { 820 Pattern sourceTransfer = pullRule.getSourceTransfer(); 821 if (sourceTransfer != null) 822 { 823 String comment = ((IDOMMember)targetNode).getComment(); 824 Matcher matcher = sourceTransfer.matcher(comment); 825 while (matcher.find() && matcher.groupCount() >= 1) 826 { 827 String clientStrings = 828 comment.substring(matcher.start(matcher.groupCount()), matcher.end(matcher.groupCount())); 829 830 for (StringTokenizer stringTokenizer = new StringTokenizer (clientStrings, ", \t\n\r\f"); 831 stringTokenizer.hasMoreTokens(); ) 832 { 833 String token = stringTokenizer.nextToken(); 834 if (!additionalStrings.contains(token)) 835 { 836 additionalStrings.add(token); 837 } 838 } 839 } 840 } 841 842 IDOMType type = (IDOMType)targetNode; 843 String [] superInterfaces = (String [])additionalStrings.toArray(new String [additionalStrings.size()]); 844 if (type.getSuperInterfaces() == null ? 845 superInterfaces.length != 0 : 846 !Arrays.equals(type.getSuperInterfaces(), superInterfaces)) 847 { 848 type.setSuperInterfaces((String [])additionalStrings.toArray(new String [additionalStrings.size()])); 849 } 850 } 851 else 852 { 853 String [] oldStringValues = (String [])sourceGetMethod.invoke(targetNode, noArguments); 854 List old = oldStringValues == null ? Collections.EMPTY_LIST : Arrays.asList(oldStringValues); 855 for (Iterator i = additionalStrings.iterator(); i.hasNext(); ) 856 { 857 String string = (String )i.next(); 858 if (!old.contains(string)) 859 { 860 targetPutMethod.invoke(targetNode, new Object [] { string }); 861 } 862 } 863 } 864 } 865 } 866 } 867 868 } 869 catch (InvocationTargetException exception) 870 { 871 } 873 catch (IllegalAccessException exception) 874 { 875 } 877 } 878 879 880 882 protected void pushSourceCompilationUnit() 883 { 884 for (IDOMNode child = sourceCompilationUnit.getFirstChild(); child != null; child = child.getNextNode()) 885 { 886 switch (child.getNodeType()) 887 { 888 case IDOMNode.PACKAGE: 889 { 890 pushSourcePackage((IDOMPackage)child); 891 break; 892 } 893 case IDOMNode.IMPORT: 894 { 895 pushSourceImport((IDOMImport)child); 896 break; 897 } 898 case IDOMNode.TYPE: 899 { 900 pushSourceType((IDOMType)child); 901 break; 902 } 903 } 904 } 905 } 906 907 protected void pushSourcePackage(IDOMPackage sourcePackage) 908 { 909 if (!sourceToTargetMap.containsKey(sourcePackage)) 910 { 911 insertClone(sourcePackage); 912 } 913 } 914 915 protected void pushSourceImport(IDOMImport sourceImport) 916 { 917 if (!sourceToTargetMap.containsKey(sourceImport) && !targetPatternDictionary.isNoImport(sourceImport)) 918 { 919 insertClone(sourceImport); 920 } 921 } 922 923 protected void pushSourceType(IDOMType sourceType) 924 { 925 if (!sourceToTargetMap.containsKey(sourceType)) 926 { 927 insertClone(sourceType); 928 } 929 else 930 { 931 for (IDOMNode child = sourceType.getFirstChild(); child != null; child = child.getNextNode()) 932 { 933 switch (child.getNodeType()) 934 { 935 case IDOMNode.INITIALIZER: 936 { 937 pushSourceInitializer((IDOMInitializer)child); 938 break; 939 } 940 case IDOMNode.FIELD: 941 { 942 pushSourceField((IDOMField)child); 943 break; 944 } 945 case IDOMNode.METHOD: 946 { 947 pushSourceMethod((IDOMMethod)child); 948 break; 949 } 950 case IDOMNode.TYPE: 951 { 952 pushSourceType((IDOMType)child); 953 break; 954 } 955 } 956 } 957 } 958 } 959 960 protected void pushSourceInitializer(IDOMInitializer sourceInitializer) 961 { 962 if (!sourceToTargetMap.containsKey(sourceInitializer)) 963 { 964 insertClone(sourceInitializer); 965 } 966 } 967 968 protected void pushSourceField(IDOMField sourceField) 969 { 970 applySortRules(sourceField); 971 if (!sourceToTargetMap.containsKey(sourceField)) 972 { 973 insertClone(sourceField); 974 } 975 } 976 977 protected void pushSourceMethod(IDOMMethod sourceMethod) 978 { 979 if (!sourceToTargetMap.containsKey(sourceMethod)) 980 { 981 insertClone(sourceMethod); 982 } 983 } 984 985 public void applySortRules(IDOMNode sourceNode) 986 { 987 LOOP: 988 for (Iterator sortRules = jControlModel.getSortRules().iterator(); sortRules.hasNext(); ) 989 { 990 JControlModel.SortRule sortRule = (JControlModel.SortRule)sortRules.next(); 991 if (sourcePatternDictionary.isMarkedUp(sortRule.getMarkup(), sourceNode) && 992 sortRule.getSelector().isInstance(sourceNode)) 993 { 994 IDOMNode parent = sourceNode.getParent(); 995 List children = (List )orderedSourceChildrenMap.get(parent); 996 if (children == null) 997 { 998 children = new ArrayList (); 999 orderedSourceChildrenMap.put(parent, children); 1000 } 1001 children.add(sourceNode); 1002 break; 1003 } 1004 } 1005 } 1006 1007 1008 1010 protected void sweepTargetCompilationUnit() 1011 { 1012 for (Iterator entries = targetToSourceMap.entrySet().iterator(); entries.hasNext(); ) 1013 { 1014 Map.Entry entry = (Map.Entry )entries.next(); 1015 if (entry.getValue() == null) 1016 { 1017 applySweepRules((IDOMNode)entry.getKey()); 1018 } 1019 } 1020 } 1021 1022 protected void applySweepRules(IDOMNode targetNode) 1023 { 1024 LOOP: 1025 for (Iterator sweepRules = jControlModel.getSweepRules().iterator(); sweepRules.hasNext(); ) 1026 { 1027 JControlModel.SweepRule sweepRule = (JControlModel.SweepRule)sweepRules.next(); 1028 if (sweepRule.getSelector() == IDOMImport.class && targetNode instanceof IDOMImport) 1029 { 1030 if (sweepRule.getMarkup().matcher(((IDOMNode)targetNode).getName()).find()) 1031 { 1032 targetNode.remove(); 1033 break; 1034 } 1035 } 1036 else if (targetPatternDictionary.isMarkedUp(sweepRule.getMarkup(), targetNode) && 1037 sweepRule.getSelector().isInstance(targetNode)) 1038 { 1039 targetNode.remove(); 1040 break; 1041 } 1042 } 1043 } 1044 1045 1047 protected void sortTargetCompilationUnit() 1048 { 1049 for (Iterator values = orderedSourceChildrenMap.values().iterator(); values.hasNext(); ) 1050 { 1051 List children = (List )values.next(); 1052 if (children.size() > 2) 1053 { 1054 Iterator i = children.iterator(); 1055 IDOMNode sourceNode = (IDOMNode)i.next(); 1056 IDOMNode previousTargetNode = (IDOMNode)sourceToTargetMap.get(sourceNode); 1057 do 1058 { 1059 sourceNode = (IDOMNode)i.next(); 1060 IDOMNode nextTargetNode = (IDOMNode)sourceToTargetMap.get(sourceNode); 1061 1062 boolean reorder = true; 1063 for (IDOMNode domNode = nextTargetNode.getPreviousNode(); domNode != null; domNode = domNode.getPreviousNode()) 1064 { 1065 if (domNode == previousTargetNode) 1066 { 1067 reorder = false; 1068 break; 1069 } 1070 } 1071 1072 if (reorder) 1073 { 1074 nextTargetNode.remove(); 1075 if (previousTargetNode.getNextNode() == null) 1076 { 1077 previousTargetNode.getParent().addChild(nextTargetNode); 1078 } 1079 else 1080 { 1081 previousTargetNode.getNextNode().insertSibling(nextTargetNode); 1082 } 1083 } 1084 1085 previousTargetNode = nextTargetNode; 1086 } 1087 while (i.hasNext()); 1088 } 1089 } 1090 } 1091 1092 1094 protected IDOMNode insertClone(IDOMNode sourceNode) 1095 { 1096 IDOMNode targetNode = null; 1097 switch (sourceNode.getNodeType()) 1098 { 1099 case IDOMNode.COMPILATION_UNIT: 1100 { 1101 targetNode = 1102 jdomFactory.createCompilationUnit 1103 (applyFormatRules(sourceNode.getContents()), ((IDOMCompilationUnit)sourceNode).getName()); 1104 break; 1105 } 1106 case IDOMNode.PACKAGE: 1107 { 1108 targetNode = jdomFactory.createPackage(applyFormatRules(sourceNode.getContents())); 1109 break; 1110 } 1111 case IDOMNode.IMPORT: 1112 { 1113 targetNode = jdomFactory.createImport(applyFormatRules(sourceNode.getContents())); 1114 break; 1115 } 1116 case IDOMNode.TYPE: 1117 { 1118 targetNode = jdomFactory.createType(applyFormatRules(sourceNode.getContents())); 1119 break; 1120 } 1121 case IDOMNode.INITIALIZER: 1122 { 1123 targetNode = jdomFactory.createInitializer(applyFormatRules(sourceNode.getContents())); 1124 break; 1125 } 1126 case IDOMNode.FIELD: 1127 { 1128 targetNode = jdomFactory.createField(applyFormatRules(sourceNode.getContents())); 1129 break; 1130 } 1131 case IDOMNode.METHOD: 1132 { 1133 targetNode = jdomFactory.createMethod(applyFormatRules(sourceNode.getContents())); 1134 break; 1135 } 1136 default: 1137 { 1138 targetNode = (IDOMNode)sourceNode.clone(); 1139 break; 1140 } 1141 } 1142 1143 if (targetNode != null) 1144 { 1145 map(sourceNode, targetNode); 1146 mapChildren(sourceNode, targetNode); 1147 } 1148 else 1149 { 1150 } 1152 for (IDOMNode previousNode = sourceNode.getPreviousNode(); previousNode != null; previousNode = previousNode.getPreviousNode()) 1153 { 1154 IDOMNode targetSibling = (IDOMNode)sourceToTargetMap.get(previousNode); 1155 if (targetSibling != null) 1156 { 1157 IDOMNode targetNextSibling = targetSibling.getNextNode(); 1158 if (targetNextSibling == null) 1159 { 1160 targetSibling.getParent().addChild(targetNode); 1161 } 1162 else 1163 { 1164 targetNextSibling.insertSibling(targetNode); 1165 } 1166 1167 return targetNode; 1168 } 1169 } 1170 if (sourceNode.getParent() != null) 1171 { 1172 IDOMNode targetParent = (IDOMNode)sourceToTargetMap.get(sourceNode.getParent()); 1173 IDOMNode targetSibling = targetParent.getFirstChild(); 1174 if (targetSibling == null) 1175 { 1176 targetParent.addChild(targetNode); 1177 } 1178 else 1179 { 1180 targetSibling.insertSibling(targetNode); 1181 } 1182 } 1183 return targetNode; 1184 } 1185 1186 protected void mapChildren(IDOMNode sourceNode, IDOMNode targetNode) 1187 { 1188 map(sourceNode, targetNode); 1189 for (IDOMNode sourceChild = sourceNode.getFirstChild(), targetChild = targetNode.getFirstChild(); 1190 sourceChild != null; 1191 sourceChild = sourceChild.getNextNode(), targetChild = targetChild.getNextNode()) 1192 { 1193 mapChildren(sourceChild, targetChild); 1194 } 1195 } 1196 1197 protected void map(IDOMNode sourceNode, IDOMNode targetNode) 1198 { 1199 if (sourceNode != null) 1200 { 1201 sourceToTargetMap.put(sourceNode, targetNode); 1202 } 1203 targetToSourceMap.put(targetNode, sourceNode); 1204 } 1205 1206 1208 1211 public Object run(Object object) 1212 { 1213 try 1214 { 1215 String contents = execute(new NullProgressMonitor(), (String [])object); 1218 1219 System.out.println("**********************************************"); 1220 System.out.println(contents); 1221 1222 return new Integer (0); 1223 } 1224 catch (Exception exception) 1225 { 1226 return new Integer (1); 1228 } 1229 } 1230 1231 1240 public String execute(IProgressMonitor progressMonitor, String [] arguments) 1241 { 1242 String mergeXML = arguments[0]; 1243 String sourceURI = arguments[1]; 1244 String targetURI = arguments[2]; 1245 1246 jControlModel = new JControlModel(mergeXML); 1249 1250 sourceCompilationUnit = createCompilationUnitForURI(sourceURI); 1253 targetCompilationUnit = createCompilationUnitForURI(targetURI); 1254 1255 sourcePatternDictionary = new JPatternDictionary(sourceCompilationUnit, jControlModel); 1258 targetPatternDictionary = new JPatternDictionary(targetCompilationUnit, jControlModel); 1259 1260 merge(); 1261 1262 return targetCompilationUnit.getContents(); 1263 } 1264} 1265 | Popular Tags |