summaryrefslogtreecommitdiffstats
path: root/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/CodanProblemMarkerResolutionGenerator.java
blob: 690f87ba60aacab20234db434f5a1f0e6b7bf653 (plain)
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
/*******************************************************************************
 * Copyright (c) 2009, 2010 Andrew Gvozdev
 * 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:
 *    Andrew Gvozdev  - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.codan.internal.ui;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Pattern;

import org.eclipse.cdt.codan.ui.AbstarctCodanCMarkerResolution;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.ui.IMarkerResolution;
import org.eclipse.ui.IMarkerResolutionGenerator;

public class CodanProblemMarkerResolutionGenerator implements
		IMarkerResolutionGenerator {
	private static final String EXTENSION_POINT_NAME = "codanMarkerResolution"; //$NON-NLS-1$
	private static Map<String, Collection<ConditionalResolution>> resolutions = new HashMap<String, Collection<ConditionalResolution>>();
	private static boolean resolutionsLoaded = false;

	static class ConditionalResolution {
		IMarkerResolution res;
		String messagePattern;

		public ConditionalResolution(IMarkerResolution res2,
				String messagePattern2) {
			res = res2;
			messagePattern = messagePattern2;
		}
	}

	public IMarkerResolution[] getResolutions(IMarker marker) {
		if (resolutionsLoaded == false) {
			readExtensions();
		}
		String id = marker.getAttribute(IMarker.PROBLEM, null);
		if (id == null)
			return new IMarkerResolution[0];
		String message = marker.getAttribute(IMarker.MESSAGE, ""); //$NON-NLS-1$
		Collection<ConditionalResolution> collection = resolutions.get(id);
		if (collection != null) {
			ArrayList<IMarkerResolution> list = new ArrayList<IMarkerResolution>();
			for (Iterator<ConditionalResolution> iterator = collection
					.iterator(); iterator.hasNext();) {
				ConditionalResolution res = iterator.next();
				if (res.messagePattern != null) {
					if (!message.matches(res.messagePattern))
						continue;
				}
				if (res.res instanceof AbstarctCodanCMarkerResolution) {
					if (!((AbstarctCodanCMarkerResolution)res.res).isApplicable(marker))
						continue;
				}
				list.add(res.res);
			}
			if (list.size() > 0)
				return list.toArray(new IMarkerResolution[list.size()]);
		}
		return new IMarkerResolution[0];
	}

	private static synchronized void readExtensions() {
		IExtensionPoint ep = Platform.getExtensionRegistry().getExtensionPoint(
				CodanUIActivator.PLUGIN_ID, EXTENSION_POINT_NAME);
		if (ep == null)
			return;
		try {
			IConfigurationElement[] elements = ep.getConfigurationElements();
			// process categories
			for (int i = 0; i < elements.length; i++) {
				IConfigurationElement configurationElement = elements[i];
				processResolution(configurationElement);
			}
		} finally {
			resolutionsLoaded = true;
		}
	}

	/**
	 * @param configurationElement
	 */
	private static void processResolution(
			IConfigurationElement configurationElement) {
		if (configurationElement.getName().equals("resolution")) { //$NON-NLS-1$
			String id = configurationElement.getAttribute("problemId"); //$NON-NLS-1$
			if (id == null) {
				CodanUIActivator.log("Extension for " + EXTENSION_POINT_NAME //$NON-NLS-1$
						+ " problemId is not defined"); //$NON-NLS-1$
				return;
			}
			IMarkerResolution res;
			try {
				res = (IMarkerResolution) configurationElement
						.createExecutableExtension("class");//$NON-NLS-1$
			} catch (CoreException e) {
				CodanUIActivator.log(e);
				return;
			}
			String messagePattern = configurationElement
					.getAttribute("messagePattern"); //$NON-NLS-1$
			if (messagePattern != null) {
				try {
					Pattern.compile(messagePattern);
				} catch (Exception e) {
					// bad pattern log and ignore
					CodanUIActivator.log("Extension for " //$NON-NLS-1$
							+ EXTENSION_POINT_NAME
							+ " messagePattern is invalid: " + e.getMessage()); //$NON-NLS-1$
					return;
				}
			}
			ConditionalResolution co = new ConditionalResolution(res,
					messagePattern);
			addResolution(id, co);
		}
	}

	public static void addResolution(String id, IMarkerResolution res,
			String messagePattern) {
		addResolution(id, new ConditionalResolution(res, messagePattern));
	}

	private static void addResolution(String id, ConditionalResolution res) {
		Collection<ConditionalResolution> collection = resolutions.get(id);
		if (collection == null) {
			collection = new ArrayList<ConditionalResolution>();
			resolutions.put(id, collection);
		}
		collection.add(res);
	}
}