Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: edb2531c4b916e9515d147b696f80448c9c02286 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*******************************************************************************
 * Copyright (c) 2012, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.pde.api.tools.internal;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.pde.api.tools.internal.model.BundleComponent;
import org.eclipse.pde.api.tools.internal.problems.ApiProblemFactory;
import org.eclipse.pde.api.tools.internal.problems.ApiProblemFilter;
import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin;
import org.eclipse.pde.api.tools.internal.provisional.IApiFilterStore;
import org.eclipse.pde.api.tools.internal.provisional.problems.IApiProblem;
import org.eclipse.pde.api.tools.internal.provisional.problems.IApiProblemFilter;
import org.eclipse.pde.api.tools.internal.util.Util;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * A generic {@link IApiFilterStore} that does not depend on workspace resources
 * to filter {@link IApiProblem}s.
 * <br>
 * This filter store can have filters added and removed from it, but those changes 
 * are not saved.
 */
public class FilterStore implements IApiFilterStore {

	public static final String GLOBAL = "!global!"; //$NON-NLS-1$
	/**
	 * Represents no filters
	 */
	public static IApiProblemFilter[] NO_FILTERS = new IApiProblemFilter[0];
	/**
	 * The current version of this filter store file format
	 */
	public static final int CURRENT_STORE_VERSION = 2;
	/**
	 * Constant representing the name of the .settings folder
	 */
	static final String SETTINGS_FOLDER = ".settings"; //$NON-NLS-1$
	/**
	 * The mapping of filters for this store.
	 */
	protected HashMap fFilterMap = null;
	/**
	 * The bundle component backing this store
	 */
	private BundleComponent fComponent = null;

	/**
	 * Constructor
	 */
	public FilterStore() {}
	
	/**
	 * Constructor
	 * @param component
	 */
	public FilterStore(BundleComponent component) {
		fComponent = component;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.pde.api.tools.internal.provisional.IApiFilterStore#addFilters(org.eclipse.pde.api.tools.internal.provisional.problems.IApiProblemFilter[])
	 */
	public void addFilters(IApiProblemFilter[] filters) {
		if(filters != null && filters.length > 0) {
			initializeApiFilters();
			// This store does not use resources so all filters are stored in the global set
			Set globalFilters = (Set)fFilterMap.get(GLOBAL);
			if (globalFilters == null){
				globalFilters = new HashSet();
				fFilterMap.put(GLOBAL, globalFilters);
			}
			for (int i = 0; i < filters.length; i++) {
				globalFilters.add(filters[i]);
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.pde.api.tools.internal.provisional.IApiFilterStore#addFiltersFor(org.eclipse.pde.api.tools.internal.provisional.problems.IApiProblem[])
	 */
	public void addFiltersFor(IApiProblem[] problems) {
		if(problems != null && problems.length > 0) {
			initializeApiFilters();
			internalAddFilters(problems, null);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.pde.api.tools.internal.provisional.IApiFilterStore#getFilters(org.eclipse.core.resources.IResource)
	 */
	public IApiProblemFilter[] getFilters(IResource resource) {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.pde.api.tools.internal.provisional.IApiFilterStore#getResources()
	 */
	public IResource[] getResources() {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.pde.api.tools.internal.provisional.IApiFilterStore#removeFilters(org.eclipse.pde.api.tools.internal.provisional.problems.IApiProblemFilter[])
	 */
	public boolean removeFilters(IApiProblemFilter[] filters) {
		if(filters != null && filters.length > 0) {
			initializeApiFilters();
			boolean removed = true;
			// This filter store does not support resources so all filters are stored under GLOBAL
			Set globalFilters = (Set)fFilterMap.get(GLOBAL);
			if (globalFilters != null && globalFilters.size() > 0){
				for(int i = 0; i < filters.length; i++) {
					removed &= globalFilters.remove(filters[i]);
				}
				return removed;
			}
		}
		return false;
	}

	/**
	 * Loads the filters from the .api_filters file
	 */
	protected synchronized void initializeApiFilters() {
		if(fFilterMap == null) {
			fFilterMap = new HashMap(5);
			ZipFile jarFile = null;
			InputStream filterstream = null;
			File loc = new File(fComponent.getLocation());
			String extension = new Path(loc.getName()).getFileExtension();
			try {
				if (extension != null && extension.equals("jar") && loc.isFile()) { //$NON-NLS-1$
					jarFile = new ZipFile(loc, ZipFile.OPEN_READ);
					ZipEntry filterfile = jarFile.getEntry(IApiCoreConstants.API_FILTERS_XML_NAME);
					if (filterfile != null) {
						if(ApiPlugin.DEBUG_FILTER_STORE) {
							System.out.println("found api filter file: [" + fComponent.getName() + "] inside jar file " + loc); //$NON-NLS-1$ //$NON-NLS-2$
						}
						filterstream = jarFile.getInputStream(filterfile);
					}
				} else {
					File file = new File(loc, SETTINGS_FOLDER+File.separator+IApiCoreConstants.API_FILTERS_XML_NAME);
					if (file.exists()) {
						if(ApiPlugin.DEBUG_FILTER_STORE) {
							System.out.println("found api filter file: [" + fComponent.getName() + "] at " + file); //$NON-NLS-1$ //$NON-NLS-2$
						}
						filterstream = new FileInputStream(file);
					}
				}
				if (filterstream == null) {
					return;
				}
				readFilterFile(filterstream);
				
			} catch (IOException e) {
				ApiPlugin.log(e);
			} finally {
				fComponent.closingZipFileAndStream(filterstream, jarFile);
			}
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.pde.api.tools.internal.provisional.IApiFilterStore#isFiltered(org.eclipse.pde.api.tools.internal.provisional.problems.IApiProblem)
	 */
	public boolean isFiltered(IApiProblem problem) {
		initializeApiFilters();
		if (fFilterMap == null || fFilterMap.isEmpty()) {
			return false;
		}
		Set globalFilters = (Set) fFilterMap.get(GLOBAL);
		if (globalFilters == null) {
			return false;
		}
		for (Iterator iterator = globalFilters.iterator(); iterator.hasNext();) {
			IApiProblemFilter filter = (IApiProblemFilter) iterator.next();
			if (problemsMatch(filter.getUnderlyingProblem(), problem)) {
				if(ApiPlugin.DEBUG_FILTER_STORE) {
					System.out.println("filter used: [" + filter.toString() + "]");  //$NON-NLS-1$//$NON-NLS-2$
				}
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Returns <code>true</code> if the attributes of the problems match, <code>false</code> otherwise
	 * 
	 * @param filterProblem the problem from the filter store
	 * @param problem the problem from the builder
	 * @return <code>true</code> if the problems match, <code>false</code> otherwise
	 */
	protected boolean problemsMatch(IApiProblem filterProblem, IApiProblem problem) {
		if (problem.getId() == filterProblem.getId()) {
			// Two problems are different if their paths are different, but if one is missing a path they may still be equal
			String problemPath = problem.getResourcePath();
			String filterProblemPath = filterProblem.getResourcePath();
			if (problemPath != null && filterProblemPath != null && !(new Path(problemPath).equals(new Path(filterProblemPath)))) {
				return false;
			}
			String problemTypeName = problem.getTypeName();
			String filterProblemTypeName = filterProblem.getTypeName();
			if (problemTypeName == null) {
				if (filterProblemTypeName != null) {
					return false;
				}
			} else if (filterProblemTypeName == null) {
				return false;
			} else if (!problemTypeName.equals(filterProblemTypeName)) {
				return false;
			}
			return argumentsEquals(problem.getMessageArguments(), filterProblem.getMessageArguments());
		}
		return false;
	}
	
	/**
	 * Returns if the arrays of message arguments are equal. 
	 * <br>
	 * The arrays are considered equal iff:
	 * <ul>
	 * <li>both are <code>null</code></li>
	 * <li>both are the same length</li>
	 * <li>both have equal elements at equal positions in the array</li>
	 * </ul>
	 * @param problemMessageArguments
	 * @param filterProblemMessageArguments
	 * @return <code>true</code> if the arrays are equal, <code>false</code> otherwise
	 */
	private boolean argumentsEquals(String[] problemMessageArguments, String[] filterProblemMessageArguments) {
		// filter problems message arguments are always simple name
		// problem message arguments are fully qualified name outside the IDE
		int length = problemMessageArguments.length;
		if (length == filterProblemMessageArguments.length) {
			for (int i = 0; i < length; i++) {
				String problemMessageArgument = problemMessageArguments[i];
				String filterProblemMessageArgument = filterProblemMessageArguments[i];
				if (problemMessageArgument.equals(filterProblemMessageArgument)) {
					continue;
				}
				int index = problemMessageArgument.lastIndexOf('.');
				int filterProblemIndex = filterProblemMessageArgument.lastIndexOf('.');
				if (index == -1) {
					if (filterProblemIndex == -1) {
						return false; // simple names should match
					}
					if (filterProblemMessageArgument.substring(filterProblemIndex + 1).equals(problemMessageArgument)) {
						continue;
					} else {
						return false;
					}
				} else if (filterProblemIndex != -1) {
					return false; // fully qualified name should match
				} else {
					if (problemMessageArgument.substring(index + 1).equals(filterProblemMessageArgument)) {
						continue;
					} else {
						return false;
					}
				}
			}
			return true;
		}
		return false;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.pde.api.tools.internal.provisional.IApiFilterStore#dispose()
	 */
	public void dispose() {
		if(fFilterMap != null) {
			fFilterMap.clear();
			fFilterMap = null;
		}
	}
	
	/**
	 * Reads the API problem filter file and calls back to {@link #addFilters(IApiProblemFilter[])} 
	 * to store the filters.
	 * <br>
	 * This method will not close the given input stream when done reading it.
	 * 
	 * @param contents the {@link InputStream} for the contents of the filter file, <code>null</code> is not allowed.
	 * @throws IOException if the stream cannot be read or fails
	 */
	protected void readFilterFile(InputStream contents) throws IOException {
		if(contents == null) {
			throw new IOException(CoreMessages.FilterStore_0);
		}
		String xml = new String(Util.getInputStreamAsCharArray(contents, -1, IApiCoreConstants.UTF_8));
		Element root = null;
		try {
			root = Util.parseDocument(xml);
		}
		catch(CoreException ce) {
			ApiPlugin.log(ce);
		}
		if (root == null) {
			return;
		}
		if (!root.getNodeName().equals(IApiXmlConstants.ELEMENT_COMPONENT)) {
			return;
		}
		String component = root.getAttribute(IApiXmlConstants.ATTR_ID);
		if(component.length() == 0) {
			return;
		}
		String versionValue = root.getAttribute(IApiXmlConstants.ATTR_VERSION);
		int currentVersion = Integer.parseInt(IApiXmlConstants.API_FILTER_STORE_CURRENT_VERSION);
		int version = 0;
		if(versionValue.length() != 0) {
			try {
				version = Integer.parseInt(versionValue);
			} catch (NumberFormatException e) {
				// ignore
			}
		}
		if (version != currentVersion) {
			return;
		}
		NodeList resources = root.getElementsByTagName(IApiXmlConstants.ELEMENT_RESOURCE);
		ArrayList newfilters = new ArrayList();
		ArrayList comments = new ArrayList();
		for(int i = 0; i < resources.getLength(); i++) {
			Element element = (Element) resources.item(i);
			String typeName = element.getAttribute(IApiXmlConstants.ATTR_TYPE);
			if (typeName.length() == 0) {
				// if there is no type attribute, an empty string is returned
				typeName = null;
			}
			String path = element.getAttribute(IApiXmlConstants.ATTR_PATH);
			if (path.trim().length() == 0){
				path = null;  // it is valid to have a filter without a path
			}
			NodeList filters = element.getElementsByTagName(IApiXmlConstants.ELEMENT_FILTER);
			for(int j = 0; j < filters.getLength(); j++) {
				element = (Element) filters.item(j);
				int id = loadIntegerAttribute(element, IApiXmlConstants.ATTR_ID);
				if(id <= 0) {
					continue;
				}
				String[] messageargs = null;
				NodeList elements = element.getElementsByTagName(IApiXmlConstants.ELEMENT_PROBLEM_MESSAGE_ARGUMENTS);
				if (elements.getLength() == 1){
					Element messageArguments = (Element) elements.item(0);
					NodeList arguments = messageArguments.getElementsByTagName(IApiXmlConstants.ELEMENT_PROBLEM_MESSAGE_ARGUMENT);
					int length = arguments.getLength();
					messageargs = new String[length];
					for (int k = 0; k < length; k++) {
						Element messageArgument = (Element) arguments.item(k);
						messageargs[k] = messageArgument.getAttribute(IApiXmlConstants.ATTR_VALUE);
					}
				}
				
				String comment = element.getAttribute(IApiXmlConstants.ATTR_COMMENT);
				comments.add((comment.length() < 1 ? null : comment));
				newfilters.add(ApiProblemFactory.newApiProblem(path, typeName, messageargs, null, null, -1, -1, -1, id));
			}
		}
		if(ApiPlugin.DEBUG_FILTER_STORE) {
			System.out.println(newfilters.size() + " filters found and added for: [" + component + "]"); //$NON-NLS-1$ //$NON-NLS-2$
		}
		internalAddFilters(
				(IApiProblem[]) newfilters.toArray(new IApiProblem[newfilters.size()]),
				(String[]) comments.toArray(new String[comments.size()]));
		newfilters.clear();
	}
	
	/**
	 * Internal use method that allows auto-persisting of the filter file to be turned on or off
	 * @param problems the problems to add the the store
	 * @param persist if the filters should be auto-persisted after they are added
	 */
	protected void internalAddFilters(IApiProblem[] problems, String[] comments) {
		if(problems == null || problems.length == 0) {
			return;
		}
		// This filter store doesn't handle resources so all filters are added to GLOBAL
		Set globalFilters = (Set) fFilterMap.get(GLOBAL);
		if(globalFilters == null) {
			globalFilters = new HashSet();
			fFilterMap.put(GLOBAL, globalFilters);
		}
		
		for(int i = 0; i < problems.length; i++) {
			IApiProblem problem = problems[i];
			String comment = comments != null ? comments[i] : null;
			IApiProblemFilter filter = new ApiProblemFilter(fComponent.getSymbolicName(), problem, comment);
			globalFilters.add(filter);
		}
	}
	
	/**
	 * Loads the specified integer attribute from the given XML element
	 * @param element the XML element
	 * @param name the name of the attribute
	 * @return the specified value in XML or -1
	 */
	protected int loadIntegerAttribute(Element element, String name) {
		String value = element.getAttribute(name);
		if(value.length() == 0) {
			return -1;
		}
		try {
			int number = Integer.parseInt(value);
			return number;
		}
		catch(NumberFormatException nfe) {}
		return -1;
	}

}

Back to the top