1 | /******************************************************************************* |
2 | * Copyright (c) 2008, 2009 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.api.tools.internal.provisional.comparator; |
12 | |
13 | import java.util.Set; |
14 | |
15 | import org.eclipse.core.runtime.CoreException; |
16 | import org.eclipse.core.runtime.IProgressMonitor; |
17 | import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; |
18 | import org.eclipse.pde.api.tools.internal.provisional.model.ApiScopeVisitor; |
19 | import org.eclipse.pde.api.tools.internal.provisional.model.ApiTypeContainerVisitor; |
20 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiBaseline; |
21 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent; |
22 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeContainer; |
23 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeRoot; |
24 | import org.eclipse.pde.api.tools.internal.util.Util; |
25 | |
26 | /** |
27 | * ApiScope visitor implementation to run the comparison on all elements of the scope. |
28 | */ |
29 | public class CompareApiScopeVisitor extends ApiScopeVisitor { |
30 | |
31 | Set deltas; |
32 | IApiBaseline referenceBaseline; |
33 | int visibilityModifiers; |
34 | boolean force; |
35 | boolean containsErrors = false; |
36 | IProgressMonitor monitor; |
37 | |
38 | public CompareApiScopeVisitor( |
39 | final Set deltas, |
40 | final IApiBaseline baseline, |
41 | final boolean force, |
42 | final int visibilityModifiers, |
43 | final IProgressMonitor monitor) { |
44 | this.deltas = deltas; |
45 | this.referenceBaseline = baseline; |
46 | this.visibilityModifiers = visibilityModifiers; |
47 | this.force = force; |
48 | this.monitor = monitor; |
49 | } |
50 | |
51 | public boolean visit(IApiBaseline baseline) throws CoreException { |
52 | try { |
53 | Util.updateMonitor(this.monitor); |
54 | IDelta delta = ApiComparator.compare(this.referenceBaseline, baseline, this.visibilityModifiers, this.force, null); |
55 | if (delta != null) { |
56 | delta.accept(new DeltaVisitor() { |
57 | public void endVisit(IDelta localDelta) { |
58 | if (localDelta.getChildren().length == 0) { |
59 | CompareApiScopeVisitor.this.deltas.add(localDelta); |
60 | } |
61 | } |
62 | }); |
63 | } else { |
64 | this.containsErrors = true; |
65 | } |
66 | return false; |
67 | } finally { |
68 | this.monitor.worked(1); |
69 | } |
70 | } |
71 | |
72 | public boolean visit(IApiTypeContainer container) throws CoreException { |
73 | try { |
74 | Util.updateMonitor(this.monitor); |
75 | container.accept(new ApiTypeContainerVisitor() { |
76 | public void visit(String packageName, IApiTypeRoot typeroot) { |
77 | try { |
78 | Util.updateMonitor(CompareApiScopeVisitor.this.monitor); |
79 | compareApiTypeRoot(typeroot); |
80 | } catch (CoreException e) { |
81 | ApiPlugin.log(e); |
82 | } |
83 | } |
84 | }); |
85 | return false; |
86 | } finally { |
87 | this.monitor.worked(1); |
88 | } |
89 | } |
90 | |
91 | public boolean visit(IApiComponent component) throws CoreException { |
92 | try { |
93 | Util.updateMonitor(this.monitor); |
94 | if (component.getErrors() != null) { |
95 | this.containsErrors = true; |
96 | return false; |
97 | } |
98 | IApiComponent referenceComponent = this.referenceBaseline.getApiComponent(component.getId()); |
99 | if (referenceComponent != null && referenceComponent.getErrors() != null) { |
100 | this.containsErrors = true; |
101 | return false; |
102 | } |
103 | if (component.isSourceComponent() || component.isSystemComponent()) { |
104 | return false; |
105 | } |
106 | Util.updateMonitor(this.monitor); |
107 | IDelta delta = ApiComparator.compare(referenceComponent, component, this.visibilityModifiers, null); |
108 | if (delta != null) { |
109 | delta.accept(new DeltaVisitor() { |
110 | public void endVisit(IDelta localDelta) { |
111 | if (localDelta.getChildren().length == 0) { |
112 | CompareApiScopeVisitor.this.deltas.add(localDelta); |
113 | } |
114 | } |
115 | }); |
116 | } else { |
117 | this.containsErrors = true; |
118 | } |
119 | return false; |
120 | } finally { |
121 | this.monitor.worked(1); |
122 | } |
123 | } |
124 | |
125 | public void visit(IApiTypeRoot root) throws CoreException { |
126 | try { |
127 | Util.updateMonitor(this.monitor); |
128 | compareApiTypeRoot(root); |
129 | } finally { |
130 | this.monitor.worked(1); |
131 | } |
132 | } |
133 | |
134 | void compareApiTypeRoot(IApiTypeRoot root) throws CoreException { |
135 | IApiComponent apiComponent = root.getApiComponent(); |
136 | if (apiComponent == null || apiComponent.isSystemComponent() || apiComponent.isSourceComponent()) { |
137 | return; |
138 | } |
139 | if (apiComponent.getErrors() != null) { |
140 | this.containsErrors = true; |
141 | return; |
142 | } |
143 | IApiComponent referenceComponent = this.referenceBaseline.getApiComponent(apiComponent.getId()); |
144 | if (referenceComponent == null) return; |
145 | if (referenceComponent.getErrors() != null) { |
146 | this.containsErrors = true; |
147 | return; |
148 | } |
149 | IApiBaseline baseline = referenceComponent.getBaseline(); |
150 | IDelta delta = ApiComparator.compare( |
151 | root, |
152 | referenceComponent, |
153 | apiComponent, |
154 | this.referenceBaseline, |
155 | baseline, |
156 | this.visibilityModifiers, null); |
157 | if (delta != null) { |
158 | delta.accept(new DeltaVisitor() { |
159 | public void endVisit(IDelta localDelta) { |
160 | if (localDelta.getChildren().length == 0) { |
161 | CompareApiScopeVisitor.this.deltas.add(localDelta); |
162 | } |
163 | } |
164 | }); |
165 | } else { |
166 | this.containsErrors = true; |
167 | } |
168 | } |
169 | |
170 | public boolean containsError() { |
171 | return this.containsErrors; |
172 | } |
173 | } |