1 11 package org.eclipse.pde.internal.ui.compare; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.ListIterator ; 16 import java.util.Vector ; 17 18 import org.eclipse.core.runtime.IProgressMonitor; 19 20 public class OrderedMatching extends AbstractMatching { 21 22 public OrderedMatching() { 23 super(); 24 } 25 26 protected int orderedMath(XMLNode x, XMLNode y) { 27 Object [] xc = x.getChildren(); 29 Object [] yc = y.getChildren(); 30 31 ArrayList xc_elementsAL = new ArrayList (); 32 ArrayList xc_attrsAL = new ArrayList (); 33 34 ArrayList yc_elementsAL = new ArrayList (); 35 ArrayList yc_attrsAL = new ArrayList (); 36 37 for (int i = 0; i < xc.length; i++) { 39 XMLNode x_i = (XMLNode) xc[i]; 40 if (x_i.getXMLType().equals(XMLStructureCreator.TYPE_ELEMENT)) { 41 xc_elementsAL.add(x_i); 42 } else if ( 43 x_i.getXMLType().equals(XMLStructureCreator.TYPE_ATTRIBUTE)) { 44 xc_attrsAL.add(x_i); 45 } 46 } 47 48 for (int i = 0; i < yc.length; i++) { 50 XMLNode y_i = (XMLNode) yc[i]; 51 if (y_i.getXMLType().equals(XMLStructureCreator.TYPE_ELEMENT)) { 52 yc_elementsAL.add(y_i); 53 } else if ( 54 y_i.getXMLType().equals(XMLStructureCreator.TYPE_ATTRIBUTE)) { 55 yc_attrsAL.add(y_i); 56 } 57 } 58 59 Object [] xc_elements = xc_elementsAL.toArray(); 60 Object [] yc_elements = yc_elementsAL.toArray(); 61 62 ArrayList DTMatching = new ArrayList (); 63 int distance = 0; 66 if (xc_attrsAL.size() > 0 || yc_attrsAL.size() > 0) { 69 if (xc_attrsAL.size() == 0) 70 distance += yc_attrsAL.size(); 71 else if (yc_attrsAL.size() == 0) 72 distance += xc_attrsAL.size(); 73 else { 74 distance = handleAttributes(xc_attrsAL, yc_attrsAL, DTMatching); 75 } 76 } 77 78 distance = handleRangeDifferencer( 79 xc_elements, 80 yc_elements, 81 DTMatching, 82 distance); 83 84 fDT[indexOfLN(x)][indexOfRN(y)]= distance; 85 fDT_Matchings[indexOfLN(x)][indexOfRN(y)]= DTMatching; 86 return distance; 87 88 } 89 90 91 public void match(XMLNode LeftTree, XMLNode RightTree, boolean rightTreeIsAncestor, 92 IProgressMonitor monitor) { 93 94 fNLeft = new Vector (); 95 fNRight = new Vector (); 97 numberNodes(LeftTree, fNLeft); 99 numberNodes(RightTree, fNRight); 100 fDT = new int[fNLeft.size()][fNRight.size()]; 101 fDT_Matchings = new ArrayList [fNLeft.size()][fNRight.size()]; 102 for (int i = 0; i < fDT.length; i++) { 103 fDT[i] = new int[fNRight.size()]; 104 for (int j = 0; j < fDT[0].length; j++) { 105 fDT[i][j] = NO_ENTRY; 106 } 107 } 108 109 dist(LeftTree, RightTree); 110 fMatches = new Vector (); 112 if (!LeftTree.getSignature().equals(RightTree.getSignature())) { 113 } else { 115 fMatches.add(new Match(LeftTree, RightTree)); 116 for (int i_M = 0; i_M < fMatches.size(); i_M++) { 117 Match m = (Match) fMatches.elementAt(i_M); 118 if (!isLeaf(m.fx) && !isLeaf(m.fy)) { 119 if (fDT_Matchings[indexOfLN(m.fx)][indexOfRN(m.fy)] != null) 120 fMatches.addAll(fDT_Matchings[indexOfLN(m.fx)][indexOfRN(m.fy)]); 121 } 122 } 123 } 124 126 if (rightTreeIsAncestor) { 127 for (ListIterator it_M = fMatches.listIterator(); it_M.hasNext();) { 128 Match m = (Match) it_M.next(); 129 if (m.fx != null && m.fy != null) 130 m.fy.setId(m.fx.getId()); 131 } 132 } else { 133 int newId = 0; 134 for (ListIterator it_M = fMatches.listIterator(); it_M.hasNext(); newId++) { 135 Match m = (Match) it_M.next(); 136 if (m.fx != null) 137 m.fx.setId(Integer.toString(newId)); 138 if (m.fy != null) 139 m.fy.setId(Integer.toString(newId)); 140 } 141 } 142 } 143 144 public int handleAttributes(ArrayList xc_attrs, ArrayList yc_attrs, ArrayList DTMatching) { 145 int distance = 0; 146 x_for : for ( 147 Iterator iter_xc = xc_attrs.iterator(); iter_xc.hasNext();) { 148 XMLNode x_attr = (XMLNode) iter_xc.next(); 149 String x_attr_name = x_attr.getName(); 150 for (Iterator iter_yc = yc_attrs.iterator(); iter_yc.hasNext();) { 151 XMLNode y_attr = (XMLNode) iter_yc.next(); 152 if (y_attr.getName().equals(x_attr_name)) { 153 if (!y_attr.getValue().equals(x_attr.getValue())) 154 distance += 1; 155 DTMatching.add(new Match(x_attr, y_attr)); 156 yc_attrs.remove(y_attr); 157 continue x_for; 158 } 159 } 160 DTMatching.add(new Match(x_attr, null)); 161 distance += 1; 162 } 163 164 for (Iterator iter_yc = yc_attrs.iterator(); iter_yc.hasNext();) { 165 DTMatching.add(new Match(null, (XMLNode) iter_yc.next())); 166 distance += 1; 167 } 168 169 return distance; 170 } 171 172 protected int handleXandYnotLeaves(XMLNode x, XMLNode y) { 173 174 return orderedMath(x, y); 175 } 176 } 177 | Popular Tags |