001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.texteditor;
011:
012: import java.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.List;
015: import java.util.Map;
016:
017: import org.eclipse.core.resources.IMarker;
018: import org.eclipse.core.runtime.IConfigurationElement;
019: import org.eclipse.core.runtime.IExtensionPoint;
020: import org.eclipse.core.runtime.Platform;
021:
022: import org.eclipse.jface.resource.StringConverter;
023:
024: import org.eclipse.ui.editors.text.EditorsUI;
025:
026: /**
027: * Provides the strategy for determining annotation types for given markers.
028: *
029: * @since 3.0
030: */
031: public final class AnnotationTypeLookup {
032:
033: /**
034: * Record representing an annotation type mapping.
035: */
036: private static class AnnotationTypeMapping {
037:
038: final static int UNDEFINED = -1;
039:
040: String fAnnotationType;
041: String fMarkerType;
042: int fMarkerSeverity = UNDEFINED;
043:
044: boolean isMarkerSeverityDefined() {
045: return fMarkerSeverity != UNDEFINED;
046: }
047: }
048:
049: /** The lookup table for marker to annotation type mappings. */
050: private Map fMapping;
051:
052: /**
053: * Creates a new annotation lookup object.
054: */
055: public AnnotationTypeLookup() {
056: }
057:
058: /**
059: * Computes the annotation type that corresponds to the state of
060: * the given marker.
061: *
062: * @param marker the marker
063: * @return the annotation type or <code>null</code>
064: */
065: public String getAnnotationType(IMarker marker) {
066: String markerType = MarkerUtilities.getMarkerType(marker);
067: if (markerType != null) {
068: int severity = MarkerUtilities.getSeverity(marker);
069: return getAnnotationType(markerType, severity);
070: }
071: return null;
072: }
073:
074: /**
075: * Computes the annotation type that corresponds to the given marker type and
076: * the given marker severity.
077: *
078: * @param markerType the marker type
079: * @param markerSeverity the marker severity
080: * @return the annotation type or <code>null</code>
081: */
082: public String getAnnotationType(String markerType,
083: int markerSeverity) {
084: String annotationType = lookupAnnotationType(markerType,
085: markerSeverity);
086: if (annotationType != null)
087: return annotationType;
088: String[] super Types = MarkerUtilities.getSuperTypes(markerType);
089: for (int i = 0; i < super Types.length; i++) {
090: annotationType = lookupAnnotationType(super Types[i],
091: markerSeverity);
092: if (annotationType != null)
093: return annotationType;
094: }
095: return null;
096: }
097:
098: /**
099: * Returns the annotation type for the given marker type and the given
100: * marker severity.
101: *
102: * @param markerType the marker type
103: * @param severity the marker severity
104: * @return the annotation type
105: */
106: private String lookupAnnotationType(String markerType, int severity) {
107: if (fMapping == null)
108: initializeMapping();
109:
110: Object value = fMapping.get(markerType);
111:
112: if (value instanceof String)
113: return (String) value;
114:
115: if (value instanceof Map) {
116: Map severityMap = (Map) value;
117: return (String) severityMap.get(new Integer(severity));
118: }
119:
120: return null;
121: }
122:
123: /**
124: * Initializes the mapping between markers and their property values and
125: * annotation types.
126: */
127: private void initializeMapping() {
128: fMapping = new HashMap();
129: List mappings = getAnnotationTypeMappings();
130: for (int i = 0, l = mappings.size(); i < l; i++) {
131: AnnotationTypeMapping atm = (AnnotationTypeMapping) mappings
132: .get(i);
133: if (atm.isMarkerSeverityDefined()) {
134: Object severityMap = fMapping.get(atm.fMarkerType);
135: if (!(severityMap instanceof Map)) {
136: severityMap = new HashMap();
137: fMapping.put(atm.fMarkerType, severityMap);
138: }
139: Map map = (Map) severityMap;
140: map.put(new Integer(atm.fMarkerSeverity),
141: atm.fAnnotationType);
142: } else {
143: fMapping.put(atm.fMarkerType, atm.fAnnotationType);
144: }
145: }
146: }
147:
148: /**
149: * Returns the list of annotation type mappings generated from the
150: * extensions provided for the annotation type extension point.
151: *
152: * @return a list of annotation type mappings
153: */
154: private List getAnnotationTypeMappings() {
155: List annotationTypeMappings = new ArrayList();
156: // read compatibility mode
157: readExtensionPoint(annotationTypeMappings,
158: "markerAnnotationSpecification", "annotationType"); //$NON-NLS-1$ //$NON-NLS-2$
159: // read new extension point
160: readExtensionPoint(annotationTypeMappings,
161: "annotationTypes", "name"); //$NON-NLS-1$ //$NON-NLS-2$
162: return annotationTypeMappings;
163: }
164:
165: /**
166: * Reads the extensions provided for the given extension point name. Uses
167: * the given type attribute name to create annotation type mappings that
168: * are appended to the given list.
169: *
170: * @param annotationTypeMappings the list to be populated
171: * @param extensionPointName the name of the extension point to read
172: * @param typeAttributeName the name of attribute specifying the annotation
173: * type
174: */
175: private void readExtensionPoint(List annotationTypeMappings,
176: String extensionPointName, String typeAttributeName) {
177: IExtensionPoint extensionPoint = Platform
178: .getExtensionRegistry().getExtensionPoint(
179: EditorsUI.PLUGIN_ID, extensionPointName);
180: if (extensionPoint != null) {
181: IConfigurationElement[] elements = extensionPoint
182: .getConfigurationElements();
183: for (int i = 0; i < elements.length; i++) {
184: AnnotationTypeMapping mapping = createMapping(
185: elements[i], typeAttributeName);
186: if (mapping != null)
187: annotationTypeMappings.add(mapping);
188: }
189: }
190: }
191:
192: /**
193: * Creates an annotation type mapping from the given configuration element.
194: *
195: * @param element the configuration element
196: * @param typeAttributeName the name of the attribute specifying the
197: * annotation type
198: * @return the annotation type mapping or <code>null</code>
199: */
200: private AnnotationTypeMapping createMapping(
201: IConfigurationElement element, String typeAttributeName) {
202:
203: AnnotationTypeMapping mapping = new AnnotationTypeMapping();
204:
205: String s = element.getAttribute(typeAttributeName);
206: if (s == null || s.trim().length() == 0)
207: return null;
208: mapping.fAnnotationType = s;
209:
210: s = element.getAttribute("markerType"); //$NON-NLS-1$
211: if (s == null || s.trim().length() == 0)
212: return null;
213: mapping.fMarkerType = s;
214:
215: s = element.getAttribute("markerSeverity"); //$NON-NLS-1$
216: if (s != null && s.trim().length() > 0)
217: mapping.fMarkerSeverity = StringConverter.asInt(s,
218: AnnotationTypeMapping.UNDEFINED);
219:
220: return mapping;
221: }
222: }
|