Lines 1-5
Link Here
|
1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2008, 2011 IBM Corporation and others. |
2 |
* Copyright (c) 2008, 2012 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
Lines 14-20
Link Here
|
14 |
import java.io.File; |
14 |
import java.io.File; |
15 |
import java.io.FileInputStream; |
15 |
import java.io.FileInputStream; |
16 |
import java.io.IOException; |
16 |
import java.io.IOException; |
17 |
import java.io.InputStream; |
|
|
18 |
import java.io.PrintWriter; |
17 |
import java.io.PrintWriter; |
19 |
import java.io.StringWriter; |
18 |
import java.io.StringWriter; |
20 |
import java.util.ArrayList; |
19 |
import java.util.ArrayList; |
Lines 29-46
Link Here
|
29 |
import java.util.Set; |
28 |
import java.util.Set; |
30 |
|
29 |
|
31 |
import org.apache.tools.ant.BuildException; |
30 |
import org.apache.tools.ant.BuildException; |
32 |
import org.eclipse.core.resources.IResource; |
|
|
33 |
import org.eclipse.core.runtime.CoreException; |
31 |
import org.eclipse.core.runtime.CoreException; |
34 |
import org.eclipse.core.runtime.NullProgressMonitor; |
32 |
import org.eclipse.core.runtime.NullProgressMonitor; |
35 |
import org.eclipse.core.runtime.Path; |
|
|
36 |
import org.eclipse.osgi.util.NLS; |
33 |
import org.eclipse.osgi.util.NLS; |
37 |
import org.eclipse.pde.api.tools.internal.IApiCoreConstants; |
|
|
38 |
import org.eclipse.pde.api.tools.internal.IApiXmlConstants; |
34 |
import org.eclipse.pde.api.tools.internal.IApiXmlConstants; |
39 |
import org.eclipse.pde.api.tools.internal.builder.BaseApiAnalyzer; |
35 |
import org.eclipse.pde.api.tools.internal.builder.BaseApiAnalyzer; |
40 |
import org.eclipse.pde.api.tools.internal.builder.BuildContext; |
36 |
import org.eclipse.pde.api.tools.internal.builder.BuildContext; |
41 |
import org.eclipse.pde.api.tools.internal.model.StubApiComponent; |
37 |
import org.eclipse.pde.api.tools.internal.model.StubApiComponent; |
42 |
import org.eclipse.pde.api.tools.internal.problems.ApiProblemFactory; |
38 |
import org.eclipse.pde.api.tools.internal.problems.ApiProblemFactory; |
43 |
import org.eclipse.pde.api.tools.internal.problems.ApiProblemFilter; |
|
|
44 |
import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; |
39 |
import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; |
45 |
import org.eclipse.pde.api.tools.internal.provisional.IApiFilterStore; |
40 |
import org.eclipse.pde.api.tools.internal.provisional.IApiFilterStore; |
46 |
import org.eclipse.pde.api.tools.internal.provisional.IApiMarkerConstants; |
41 |
import org.eclipse.pde.api.tools.internal.provisional.IApiMarkerConstants; |
Lines 48-352
Link Here
|
48 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiBaseline; |
43 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiBaseline; |
49 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent; |
44 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent; |
50 |
import org.eclipse.pde.api.tools.internal.provisional.problems.IApiProblem; |
45 |
import org.eclipse.pde.api.tools.internal.provisional.problems.IApiProblem; |
51 |
import org.eclipse.pde.api.tools.internal.provisional.problems.IApiProblemFilter; |
|
|
52 |
import org.eclipse.pde.api.tools.internal.util.FilteredElements; |
46 |
import org.eclipse.pde.api.tools.internal.util.FilteredElements; |
53 |
import org.eclipse.pde.api.tools.internal.util.Util; |
47 |
import org.eclipse.pde.api.tools.internal.util.Util; |
54 |
import org.w3c.dom.DOMException; |
48 |
import org.w3c.dom.DOMException; |
55 |
import org.w3c.dom.Document; |
49 |
import org.w3c.dom.Document; |
56 |
import org.w3c.dom.Element; |
50 |
import org.w3c.dom.Element; |
57 |
import org.w3c.dom.NodeList; |
|
|
58 |
|
51 |
|
59 |
/** |
52 |
/** |
60 |
* Ant task to run the API tool verification during Eclipse build. |
53 |
* Ant task to run the API tool verification during Eclipse build. |
61 |
*/ |
54 |
*/ |
62 |
public class APIToolsAnalysisTask extends CommonUtilsTask { |
55 |
public class APIToolsAnalysisTask extends CommonUtilsTask { |
63 |
/** |
56 |
|
64 |
* This filter store is only used to filter problem using existing filters. |
|
|
65 |
* It doesn't add or remove any filters. |
66 |
*/ |
67 |
private static class AntFilterStore implements IApiFilterStore { |
68 |
private static final String GLOBAL = "!global!"; //$NON-NLS-1$ |
69 |
private static int loadIntegerAttribute(Element element, String name) { |
70 |
String value = element.getAttribute(name); |
71 |
if(value.length() == 0) { |
72 |
return -1; |
73 |
} |
74 |
try { |
75 |
int number = Integer.parseInt(value); |
76 |
return number; |
77 |
} |
78 |
catch(NumberFormatException nfe) {} |
79 |
return -1; |
80 |
} |
81 |
private boolean debug; |
82 |
|
83 |
private Map fFilterMap; |
84 |
|
85 |
public AntFilterStore(boolean debug, String filtersRoot, String componentID) { |
86 |
this.initialize(filtersRoot, componentID); |
87 |
} |
88 |
|
89 |
public void addFiltersFor(IApiProblem[] problems) { |
90 |
// do nothing |
91 |
} |
92 |
|
93 |
public void addFilters(IApiProblemFilter[] filters) { |
94 |
// do nothing |
95 |
} |
96 |
|
97 |
private boolean argumentsEquals(String[] problemMessageArguments, |
98 |
String[] filterProblemMessageArguments) { |
99 |
// filter problems message arguments are always simple name |
100 |
// problem message arguments are fully qualified name outside the IDE |
101 |
int length = problemMessageArguments.length; |
102 |
if (length == filterProblemMessageArguments.length) { |
103 |
for (int i = 0; i < length; i++) { |
104 |
String problemMessageArgument = problemMessageArguments[i]; |
105 |
String filterProblemMessageArgument = filterProblemMessageArguments[i]; |
106 |
if (problemMessageArgument.equals(filterProblemMessageArgument)) { |
107 |
continue; |
108 |
} |
109 |
int index = problemMessageArgument.lastIndexOf('.'); |
110 |
int filterProblemIndex = filterProblemMessageArgument.lastIndexOf('.'); |
111 |
if (index == -1) { |
112 |
if (filterProblemIndex == -1) { |
113 |
return false; // simple names should match |
114 |
} |
115 |
if (filterProblemMessageArgument.substring(filterProblemIndex + 1).equals(problemMessageArgument)) { |
116 |
continue; |
117 |
} else { |
118 |
return false; |
119 |
} |
120 |
} else if (filterProblemIndex != -1) { |
121 |
return false; // fully qualified name should match |
122 |
} else { |
123 |
if (problemMessageArgument.substring(index + 1).equals(filterProblemMessageArgument)) { |
124 |
continue; |
125 |
} else { |
126 |
return false; |
127 |
} |
128 |
} |
129 |
} |
130 |
return true; |
131 |
} |
132 |
return false; |
133 |
} |
134 |
|
135 |
public void dispose() { |
136 |
// do nothing |
137 |
} |
138 |
|
139 |
public IApiProblemFilter[] getFilters(IResource resource) { |
140 |
return null; |
141 |
} |
142 |
|
143 |
public IResource[] getResources() { |
144 |
return null; |
145 |
} |
146 |
|
147 |
/** |
148 |
* Initialize the filter store using the given component id |
149 |
*/ |
150 |
private void initialize(String filtersRoot, String componentID) { |
151 |
if(fFilterMap != null) { |
152 |
return; |
153 |
} |
154 |
if(this.debug) { |
155 |
System.out.println("null filter map, creating a new one"); //$NON-NLS-1$ |
156 |
} |
157 |
fFilterMap = new HashMap(5); |
158 |
String xml = null; |
159 |
InputStream contents = null; |
160 |
try { |
161 |
File filterFileParent = new File(filtersRoot, componentID); |
162 |
if (!filterFileParent.exists()) { |
163 |
return; |
164 |
} |
165 |
contents = new BufferedInputStream(new FileInputStream(new File(filterFileParent, IApiCoreConstants.API_FILTERS_XML_NAME))); |
166 |
xml = new String(Util.getInputStreamAsCharArray(contents, -1, IApiCoreConstants.UTF_8)); |
167 |
} |
168 |
catch(IOException ioe) {} |
169 |
finally { |
170 |
if (contents != null) { |
171 |
try { |
172 |
contents.close(); |
173 |
} catch(IOException e) { |
174 |
// ignore |
175 |
} |
176 |
} |
177 |
} |
178 |
if(xml == null) { |
179 |
return; |
180 |
} |
181 |
Element root = null; |
182 |
try { |
183 |
root = Util.parseDocument(xml); |
184 |
} |
185 |
catch(CoreException ce) { |
186 |
ApiPlugin.log(ce); |
187 |
} |
188 |
if (root == null) { |
189 |
return; |
190 |
} |
191 |
if (!root.getNodeName().equals(IApiXmlConstants.ELEMENT_COMPONENT)) { |
192 |
return; |
193 |
} |
194 |
String component = root.getAttribute(IApiXmlConstants.ATTR_ID); |
195 |
if(component.length() == 0) { |
196 |
return; |
197 |
} |
198 |
String versionValue = root.getAttribute(IApiXmlConstants.ATTR_VERSION); |
199 |
int version = 0; |
200 |
if(versionValue.length() != 0) { |
201 |
try { |
202 |
version = Integer.parseInt(versionValue); |
203 |
} catch (NumberFormatException e) { |
204 |
// ignore |
205 |
} |
206 |
} |
207 |
if (version < 2) { |
208 |
// we discard all filters since there is no way to retrieve the type name |
209 |
return; |
210 |
} |
211 |
NodeList resources = root.getElementsByTagName(IApiXmlConstants.ELEMENT_RESOURCE); |
212 |
ArrayList newfilters = new ArrayList(); |
213 |
ArrayList comments = new ArrayList(); |
214 |
for(int i = 0; i < resources.getLength(); i++) { |
215 |
Element element = (Element) resources.item(i); |
216 |
String typeName = element.getAttribute(IApiXmlConstants.ATTR_TYPE); |
217 |
if (typeName.length() == 0) { |
218 |
// if there is no type attribute, an empty string is returned |
219 |
typeName = null; |
220 |
} |
221 |
String path = element.getAttribute(IApiXmlConstants.ATTR_PATH); |
222 |
NodeList filters = element.getElementsByTagName(IApiXmlConstants.ELEMENT_FILTER); |
223 |
for(int j = 0; j < filters.getLength(); j++) { |
224 |
element = (Element) filters.item(j); |
225 |
int id = loadIntegerAttribute(element, IApiXmlConstants.ATTR_ID); |
226 |
if(id <= 0) { |
227 |
continue; |
228 |
} |
229 |
String[] messageargs = null; |
230 |
NodeList elements = element.getElementsByTagName(IApiXmlConstants.ELEMENT_PROBLEM_MESSAGE_ARGUMENTS); |
231 |
if (elements.getLength() != 1) continue; |
232 |
Element messageArguments = (Element) elements.item(0); |
233 |
NodeList arguments = messageArguments.getElementsByTagName(IApiXmlConstants.ELEMENT_PROBLEM_MESSAGE_ARGUMENT); |
234 |
int length = arguments.getLength(); |
235 |
messageargs = new String[length]; |
236 |
String comment = element.getAttribute(IApiXmlConstants.ATTR_COMMENT); |
237 |
comments.add((comment.length() < 1 ? null : comment)); |
238 |
for (int k = 0; k < length; k++) { |
239 |
Element messageArgument = (Element) arguments.item(k); |
240 |
messageargs[k] = messageArgument.getAttribute(IApiXmlConstants.ATTR_VALUE); |
241 |
} |
242 |
newfilters.add(ApiProblemFactory.newApiProblem(path, typeName, messageargs, null, null, -1, -1, -1, id)); |
243 |
} |
244 |
} |
245 |
internalAddFilters(componentID, (IApiProblem[]) newfilters.toArray(new IApiProblem[newfilters.size()]), |
246 |
(String[]) comments.toArray(new String[comments.size()])); |
247 |
newfilters.clear(); |
248 |
} |
249 |
|
250 |
/** |
251 |
* Internal use method that allows auto-persisting of the filter file to be turned on or off |
252 |
* @param problems the problems to add the the store |
253 |
* @param persist if the filters should be auto-persisted after they are added |
254 |
*/ |
255 |
private void internalAddFilters(String componentID, IApiProblem[] problems, String[] comments) { |
256 |
if(problems == null) { |
257 |
if(this.debug) { |
258 |
System.out.println("null problems array not addding filters"); //$NON-NLS-1$ |
259 |
} |
260 |
return; |
261 |
} |
262 |
for(int i = 0; i < problems.length; i++) { |
263 |
IApiProblem problem = problems[i]; |
264 |
IApiProblemFilter filter = new ApiProblemFilter(componentID, problem, comments[i]); |
265 |
String typeName = problem.getTypeName(); |
266 |
if (typeName == null) { |
267 |
typeName = GLOBAL; |
268 |
} |
269 |
Set filters = (Set) fFilterMap.get(typeName); |
270 |
if(filters == null) { |
271 |
filters = new HashSet(); |
272 |
fFilterMap.put(typeName, filters); |
273 |
} |
274 |
filters.add(filter); |
275 |
} |
276 |
} |
277 |
|
278 |
public boolean isFiltered(IApiProblem problem) { |
279 |
if (this.fFilterMap == null || this.fFilterMap.isEmpty()) return false; |
280 |
String typeName = problem.getTypeName(); |
281 |
if (typeName == null || typeName.length() == 0) { |
282 |
typeName = GLOBAL; |
283 |
} |
284 |
Set filters = (Set) this.fFilterMap.get(typeName); |
285 |
if (filters == null) { |
286 |
return false; |
287 |
} |
288 |
for (Iterator iterator = filters.iterator(); iterator.hasNext();) { |
289 |
IApiProblemFilter filter = (IApiProblemFilter) iterator.next(); |
290 |
if (problem.getCategory() == IApiProblem.CATEGORY_USAGE) { |
291 |
// write our own matching implementation |
292 |
return matchUsageProblem(filter.getUnderlyingProblem(), problem); |
293 |
} else if (matchFilters(filter.getUnderlyingProblem(), problem)) { |
294 |
return true; |
295 |
} |
296 |
} |
297 |
return false; |
298 |
} |
299 |
|
300 |
private boolean matchFilters(IApiProblem filterProblem, IApiProblem problem) { |
301 |
if (problem.getId() == filterProblem.getId() && argumentsEquals(problem.getMessageArguments(), filterProblem.getMessageArguments())) { |
302 |
String typeName = problem.getTypeName(); |
303 |
String filteredProblemTypeName = filterProblem.getTypeName(); |
304 |
if (typeName == null) { |
305 |
if (filteredProblemTypeName != null) { |
306 |
return false; |
307 |
} |
308 |
return true; |
309 |
} else if (filteredProblemTypeName == null) { |
310 |
return false; |
311 |
} |
312 |
return typeName.equals(filteredProblemTypeName); |
313 |
} |
314 |
return false; |
315 |
} |
316 |
|
317 |
private boolean matchUsageProblem(IApiProblem filterProblem, IApiProblem problem) { |
318 |
if (problem.getId() == filterProblem.getId()) { |
319 |
// check arguments |
320 |
String problemPath = problem.getResourcePath(); |
321 |
String filterProblemPath = filterProblem.getResourcePath(); |
322 |
if (problemPath == null) { |
323 |
if (filterProblemPath != null) { |
324 |
return false; |
325 |
} |
326 |
} else if (filterProblemPath == null) { |
327 |
return false; |
328 |
} else if (!new Path(problemPath).equals(new Path(filterProblemPath))) { |
329 |
return false; |
330 |
} |
331 |
String problemTypeName = problem.getTypeName(); |
332 |
String filterProblemTypeName = filterProblem.getTypeName(); |
333 |
if (problemTypeName == null) { |
334 |
if (filterProblemTypeName != null) { |
335 |
return false; |
336 |
} |
337 |
} else if (filterProblemTypeName == null) { |
338 |
return false; |
339 |
} else if (!problemTypeName.equals(filterProblemTypeName)) { |
340 |
return false; |
341 |
} |
342 |
return argumentsEquals(problem.getMessageArguments(), filterProblem.getMessageArguments()); |
343 |
} |
344 |
return false; |
345 |
} |
346 |
public boolean removeFilters(IApiProblemFilter[] filters) { |
347 |
return false; |
348 |
} |
349 |
} |
350 |
private static class Summary { |
57 |
private static class Summary { |
351 |
List apiBundleVersionProblems = new ArrayList(); |
58 |
List apiBundleVersionProblems = new ArrayList(); |
352 |
List apiCompatibilityProblems = new ArrayList(); |
59 |
List apiCompatibilityProblems = new ArrayList(); |