1 11 package org.eclipse.ui.texteditor; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.Map ; 17 18 import org.eclipse.core.resources.IMarker; 19 import org.eclipse.core.runtime.IConfigurationElement; 20 import org.eclipse.core.runtime.IExtensionPoint; 21 import org.eclipse.core.runtime.Platform; 22 23 import org.eclipse.jface.resource.StringConverter; 24 25 import org.eclipse.ui.editors.text.EditorsUI; 26 27 28 33 public final class AnnotationTypeLookup { 34 35 38 private static class AnnotationTypeMapping { 39 40 final static int UNDEFINED= -1; 41 42 String fAnnotationType; 43 String fMarkerType; 44 int fMarkerSeverity= UNDEFINED; 45 46 boolean isMarkerSeverityDefined() { 47 return fMarkerSeverity != UNDEFINED; 48 } 49 } 50 51 52 private Map fMapping; 53 54 57 public AnnotationTypeLookup() { 58 } 59 60 67 public String getAnnotationType(IMarker marker) { 68 String markerType= MarkerUtilities.getMarkerType(marker); 69 if (markerType != null) { 70 int severity= MarkerUtilities.getSeverity(marker); 71 return getAnnotationType(markerType, severity); 72 } 73 return null; 74 } 75 76 84 public String getAnnotationType(String markerType, int markerSeverity) { 85 String annotationType= lookupAnnotationType(markerType, markerSeverity); 86 if (annotationType != null) 87 return annotationType; 88 String [] superTypes= MarkerUtilities.getSuperTypes(markerType); 89 for (int i= 0; i < superTypes.length; i++) { 90 annotationType= lookupAnnotationType(superTypes[i], markerSeverity); 91 if (annotationType != null) 92 return annotationType; 93 } 94 return null; 95 } 96 97 105 private String lookupAnnotationType(String markerType, int severity) { 106 if (fMapping == null) 107 initializeMapping(); 108 109 Object value= fMapping.get(markerType); 110 111 if (value instanceof String ) 112 return (String ) value; 113 114 if (value instanceof Map ) { 115 Map severityMap= (Map ) value; 116 return (String ) severityMap.get(new Integer (severity)); 117 } 118 119 return null; 120 } 121 122 126 private void initializeMapping() { 127 fMapping= new HashMap (); 128 List mappings= getAnnotationTypeMappings(); 129 for (int i= 0, l= mappings.size(); i < l; i++) { 130 AnnotationTypeMapping atm= (AnnotationTypeMapping) mappings.get(i); 131 if (atm.isMarkerSeverityDefined()) { 132 Object severityMap= fMapping.get(atm.fMarkerType); 133 if (!(severityMap instanceof Map )) { 134 severityMap= new HashMap (); 135 fMapping.put(atm.fMarkerType, severityMap); 136 } 137 Map map= (Map ) severityMap; 138 map.put(new Integer (atm.fMarkerSeverity), atm.fAnnotationType); 139 } else { 140 fMapping.put(atm.fMarkerType, atm.fAnnotationType); 141 } 142 } 143 } 144 145 151 private List getAnnotationTypeMappings() { 152 List annotationTypeMappings= new ArrayList (); 153 readExtensionPoint(annotationTypeMappings, "markerAnnotationSpecification", "annotationType"); readExtensionPoint(annotationTypeMappings, "annotationTypes", "name"); return annotationTypeMappings; 158 } 159 160 170 private void readExtensionPoint(List annotationTypeMappings, String extensionPointName, String typeAttributeName) { 171 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(EditorsUI.PLUGIN_ID, extensionPointName); 172 if (extensionPoint != null) { 173 IConfigurationElement[] elements= extensionPoint.getConfigurationElements(); 174 for (int i= 0; i < elements.length; i++) { 175 AnnotationTypeMapping mapping= createMapping(elements[i], typeAttributeName); 176 if (mapping != null) 177 annotationTypeMappings.add(mapping); 178 } 179 } 180 } 181 182 190 private AnnotationTypeMapping createMapping(IConfigurationElement element, String typeAttributeName) { 191 192 AnnotationTypeMapping mapping= new AnnotationTypeMapping(); 193 194 String s= element.getAttribute(typeAttributeName); 195 if (s == null || s.trim().length() == 0) return null; 196 mapping.fAnnotationType= s; 197 198 s= element.getAttribute("markerType"); if (s == null || s.trim().length() == 0) return null; 200 mapping.fMarkerType= s; 201 202 s= element.getAttribute("markerSeverity"); if (s != null && s.trim().length() > 0) 204 mapping.fMarkerSeverity= StringConverter.asInt(s, AnnotationTypeMapping.UNDEFINED); 205 206 return mapping; 207 } 208 } 209 | Popular Tags |