KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > compare > OrderedMatching


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.compare;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.ListIterator JavaDoc;
16 import java.util.Vector JavaDoc;
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         //assumes x and y have children
28
Object JavaDoc[] xc = x.getChildren();
29         Object JavaDoc[] yc = y.getChildren();
30
31         ArrayList JavaDoc xc_elementsAL = new ArrayList JavaDoc();
32         ArrayList JavaDoc xc_attrsAL = new ArrayList JavaDoc();
33
34         ArrayList JavaDoc yc_elementsAL = new ArrayList JavaDoc();
35         ArrayList JavaDoc yc_attrsAL = new ArrayList JavaDoc();
36
37         //find attributes and elements and put them in xc_elementsAL and xc_attrsAL, respectively
38
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         //do the same for yc
49
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 JavaDoc[] xc_elements = xc_elementsAL.toArray();
60         Object JavaDoc[] yc_elements = yc_elementsAL.toArray();
61
62         ArrayList JavaDoc DTMatching = new ArrayList JavaDoc();
63         // Matching to be added to Entry in fDT_Matchings
64
int distance = 0; //distance to be added to entry in fDT
65

66         // perform unordered matching on attributes
67
// this updates fDT and fDT_Matchings
68
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     /* matches two trees according to paper "X-Diff", p. 16 */
91     public void match(XMLNode LeftTree, XMLNode RightTree, boolean rightTreeIsAncestor,
92             IProgressMonitor monitor) {
93
94         fNLeft = new Vector JavaDoc();
95         //numbering LeftTree: Mapping nodes in LeftTree to numbers to be used as array indexes
96
fNRight = new Vector JavaDoc();
97         //numbering RightTree: Mapping nodes in RightTree to numbers to be used as array indexes
98
numberNodes(LeftTree, fNLeft);
99         numberNodes(RightTree, fNRight);
100         fDT = new int[fNLeft.size()][fNRight.size()];
101         fDT_Matchings = new ArrayList JavaDoc[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         // /* mark matchings on LeftTree and RightTree */
111
fMatches = new Vector JavaDoc();
112         if (!LeftTree.getSignature().equals(RightTree.getSignature())) {
113             //matching is empty
114
} 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         //end of Step2
125
/* Renumber Id of Nodes to follow Matches. Or for ancestor, copy over Id to ancestor */
126         if (rightTreeIsAncestor) {
127             for (ListIterator JavaDoc 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 JavaDoc 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 JavaDoc xc_attrs, ArrayList JavaDoc yc_attrs, ArrayList JavaDoc DTMatching) {
145         int distance = 0;
146         x_for : for (
147             Iterator JavaDoc iter_xc = xc_attrs.iterator(); iter_xc.hasNext();) {
148             XMLNode x_attr = (XMLNode) iter_xc.next();
149             String JavaDoc x_attr_name = x_attr.getName();
150             for (Iterator JavaDoc 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 JavaDoc 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         /* handle entries as ordered*/
174         return orderedMath(x, y);
175     }
176 }
177
Popular Tags