Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 29f5b8ece70d1ef4da2cdcb33e5ee8fc40de9102 (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
/*******************************************************************************
 * Copyright (c) 2000, 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.texteditor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;

import org.eclipse.core.resources.IMarker;

import org.eclipse.jface.resource.StringConverter;

import org.eclipse.ui.editors.text.EditorsUI;


/**
 * Provides the strategy for determining annotation types for given markers.
 *
 * @since 3.0
 */
public final class AnnotationTypeLookup {

	/**
	 * Record representing an annotation type mapping.
	 */
	private static class AnnotationTypeMapping {

		final static int UNDEFINED= -1;

		String fAnnotationType;
		String fMarkerType;
		int fMarkerSeverity= UNDEFINED;

		boolean isMarkerSeverityDefined() {
			return fMarkerSeverity != UNDEFINED;
		}
	}

	/**
	 * The lookup table for marker to annotation type mappings. The value type is a {@link String}
	 * or a {@code Map<Integer, String>}.
	 */
	private Map<String, Object> fMapping;

	/**
	 * Creates a new annotation lookup object.
	 */
	public AnnotationTypeLookup() {
	}

	/**
	 * Computes the annotation type that corresponds to the state of
	 * the given marker.
	 *
	 * @param marker the marker
	 * @return the annotation type or <code>null</code>
	 */
	public String getAnnotationType(IMarker marker) {
		String markerType= MarkerUtilities.getMarkerType(marker);
		if (markerType != null) {
			int severity= MarkerUtilities.getSeverity(marker);
			return getAnnotationType(markerType, severity);
		}
		return null;
	}

	/**
	 * Computes the annotation type that corresponds to the given marker type and
	 * the given marker severity.
	 *
	 * @param markerType the marker type
	 * @param markerSeverity the marker severity
	 * @return the annotation type or <code>null</code>
	 */
	public String getAnnotationType(String markerType, int markerSeverity) {
		String annotationType= lookupAnnotationType(markerType, markerSeverity);
		if (annotationType != null)
			return annotationType;
		String[] superTypes= MarkerUtilities.getSuperTypes(markerType);
		for (String superType : superTypes) {
			annotationType = lookupAnnotationType(superType, markerSeverity);
			if (annotationType != null)
				return annotationType;
		}
		return null;
	}

	/**
	 * Returns the annotation type for the given marker type and the given
	 * marker severity.
	 *
	 * @param markerType the marker type
	 * @param severity the marker severity
	 * @return the annotation type
	 */
	private String lookupAnnotationType(String markerType, int severity) {
		if (fMapping == null)
			initializeMapping();

		Object value= fMapping.get(markerType);

		if (value instanceof String)
			return (String) value;

		if (value instanceof Map) {
			@SuppressWarnings("unchecked")
			Map<Integer, String> severityMap= (Map<Integer, String>) value;
			return severityMap.get(Integer.valueOf(severity));
		}

		return null;
	}

	/**
	 * Initializes the mapping between markers and their property values and
	 * annotation types.
	 */
	private void initializeMapping() {
		fMapping= new HashMap<>();
		List<AnnotationTypeMapping> mappings= getAnnotationTypeMappings();
		for (int i= 0, l= mappings.size(); i < l; i++) {
			AnnotationTypeMapping atm= mappings.get(i);
			if (atm.isMarkerSeverityDefined()) {
				Object severityMap= fMapping.get(atm.fMarkerType);
				if (!(severityMap instanceof Map)) {
					severityMap= new HashMap<>();
					fMapping.put(atm.fMarkerType, severityMap);
				}
				@SuppressWarnings("unchecked")
				Map<Integer, String> map= (Map<Integer, String>) severityMap;
				map.put(Integer.valueOf(atm.fMarkerSeverity), atm.fAnnotationType);
			} else {
				fMapping.put(atm.fMarkerType, atm.fAnnotationType);
			}
		}
	}

	/**
	 * Returns the list of annotation type mappings generated from the
	 * extensions provided for the annotation type extension point.
	 *
	 * @return a list of annotation type mappings
	 */
	private List<AnnotationTypeMapping> getAnnotationTypeMappings() {
		List<AnnotationTypeMapping> annotationTypeMappings= new ArrayList<>();
		// read compatibility mode
		readExtensionPoint(annotationTypeMappings, "markerAnnotationSpecification", "annotationType"); //$NON-NLS-1$ //$NON-NLS-2$
		// read new extension point
		readExtensionPoint(annotationTypeMappings, "annotationTypes", "name"); //$NON-NLS-1$ //$NON-NLS-2$
		return annotationTypeMappings;
	}

	/**
	 * Reads the extensions provided for the given extension point name. Uses
	 * the given type attribute name to create annotation type mappings that
	 * are appended to the given list.
	 *
	 * @param annotationTypeMappings the list to be populated
	 * @param extensionPointName the name of the extension point to read
	 * @param typeAttributeName the name of attribute specifying the annotation
	 *            type
	 */
	private void readExtensionPoint(List<AnnotationTypeMapping> annotationTypeMappings, String extensionPointName, String typeAttributeName) {
		IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(EditorsUI.PLUGIN_ID, extensionPointName);
		if (extensionPoint != null) {
			IConfigurationElement[] elements= extensionPoint.getConfigurationElements();
			for (IConfigurationElement element : elements) {
				AnnotationTypeMapping mapping = createMapping(element, typeAttributeName);
				if (mapping != null)
					annotationTypeMappings.add(mapping);
			}
		}
	}

	/**
	 * Creates an annotation type mapping from the given configuration element.
	 *
	 * @param element the configuration element
	 * @param typeAttributeName the name of the attribute specifying the
	 *            annotation type
	 * @return the annotation type mapping or <code>null</code>
	 */
	private AnnotationTypeMapping createMapping(IConfigurationElement element, String typeAttributeName) {

		AnnotationTypeMapping mapping= new AnnotationTypeMapping();

		String s= element.getAttribute(typeAttributeName);
		if (s == null || s.trim().isEmpty()) return null;
		mapping.fAnnotationType= s;

		s= element.getAttribute("markerType");  //$NON-NLS-1$
		if (s == null || s.trim().isEmpty()) return null;
		mapping.fMarkerType= s;

		s= element.getAttribute("markerSeverity");  //$NON-NLS-1$
		if (s != null && !s.trim().isEmpty())
			mapping.fMarkerSeverity= StringConverter.asInt(s, AnnotationTypeMapping.UNDEFINED);

		return mapping;
	}
}

Back to the top