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.osgi.framework.Bundle; 19 20 import org.eclipse.core.resources.IMarker; 21 import org.eclipse.core.resources.IResource; 22 import org.eclipse.core.resources.IWorkspace; 23 import org.eclipse.core.resources.IWorkspaceRunnable; 24 import org.eclipse.core.resources.ResourcesPlugin; 25 import org.eclipse.core.runtime.CoreException; 26 import org.eclipse.core.runtime.IConfigurationElement; 27 import org.eclipse.core.runtime.IExtension; 28 import org.eclipse.core.runtime.IExtensionPoint; 29 import org.eclipse.core.runtime.ILog; 30 import org.eclipse.core.runtime.IProgressMonitor; 31 import org.eclipse.core.runtime.Platform; 32 33 import org.eclipse.ui.PlatformUI; 34 35 36 45 public final class MarkerUtilities { 46 47 51 private static class MarkerTypeHierarchy { 52 53 private Map fTypeMap; 54 private Map fSuperTypesCache= new HashMap (); 55 56 public String [] getSuperTypes(String typeName) { 57 String [] cachedTypes= (String []) fSuperTypesCache.get(typeName); 58 if (cachedTypes == null) { 59 cachedTypes= computeSuperTypes(typeName); 60 fSuperTypesCache.put(typeName, cachedTypes); 61 } 62 return cachedTypes; 63 } 64 65 private String [] computeSuperTypes(String typeName) { 66 ArrayList types= new ArrayList (); 67 appendAll(types, getDirectSuperTypes(typeName)); 68 int index= 0; 69 while (index < types.size()) { 70 String type= (String ) types.get(index++); 71 appendAll(types, getDirectSuperTypes(type)); 72 } 73 74 String [] superTypes= new String [types.size()]; 75 types.toArray(superTypes); 76 return superTypes; 77 } 78 79 private String [] getDirectSuperTypes(String typeName) { 80 return (String []) getTypeMap().get(typeName); 81 } 82 83 private void appendAll(List list, Object [] objects) { 84 if (objects == null) 85 return; 86 for (int i= 0; i < objects.length; i++) { 87 Object o= objects[i]; 88 if (!list.contains(o)) 89 list.add(o); 90 } 91 } 92 93 private Map getTypeMap() { 94 if (fTypeMap == null) 95 fTypeMap= readTypes(); 96 return fTypeMap; 97 } 98 99 private Map readTypes() { 100 HashMap allTypes= new HashMap (); 101 IExtensionPoint point= Platform.getExtensionRegistry().getExtensionPoint(ResourcesPlugin.PI_RESOURCES, ResourcesPlugin.PT_MARKERS); 102 if (point != null) { 103 IExtension[] extensions = point.getExtensions(); 104 for (int i= 0; i < extensions.length; i++) { 105 IExtension extension= extensions[i]; 106 ArrayList types= new ArrayList (); 107 IConfigurationElement[] configElements= extension.getConfigurationElements(); 108 for (int j= 0; j < configElements.length; ++j) { 109 IConfigurationElement element= configElements[j]; 110 if (element.getName().equalsIgnoreCase("super")) { String type = element.getAttribute("type"); if (type != null) { 113 types.add(type); 114 } 115 } 116 } 117 String [] superTypes= new String [types.size()]; 118 types.toArray(superTypes); 119 allTypes.put(extension.getUniqueIdentifier(), superTypes); 120 } 121 } 122 return allTypes; 123 } 124 } 125 126 private static MarkerTypeHierarchy fgMarkerTypeHierarchy; 127 128 129 130 133 private MarkerUtilities() { 134 } 135 136 144 public static int getCharEnd(IMarker marker) { 145 return getIntAttribute(marker, IMarker.CHAR_END, -1); 146 } 147 148 156 public static int getCharStart(IMarker marker) { 157 return getIntAttribute(marker, IMarker.CHAR_START, -1); 158 } 159 160 170 private static int getIntAttribute(IMarker marker, String attributeName, int defaultValue) { 171 if (marker.exists()) 172 return marker.getAttribute(attributeName, defaultValue); 173 return defaultValue; 174 } 175 176 184 public static int getLineNumber(IMarker marker) { 185 return getIntAttribute(marker, IMarker.LINE_NUMBER, -1); 186 } 187 188 197 public static int getPriority(IMarker marker) { 198 return getIntAttribute(marker, IMarker.PRIORITY, IMarker.PRIORITY_NORMAL); 199 } 200 201 210 public static int getSeverity(IMarker marker) { 211 return getIntAttribute(marker, IMarker.SEVERITY, IMarker.SEVERITY_INFO); 212 } 213 214 219 private static void handleCoreException(CoreException e) { 220 Bundle bundle = Platform.getBundle(PlatformUI.PLUGIN_ID); 221 ILog log= Platform.getLog(bundle); 222 log.log(e.getStatus()); 223 } 224 225 232 public static boolean isMarkerType(IMarker marker, String type) { 233 if (marker != null) { 234 try { 235 return marker.exists() && marker.isSubtypeOf(type); 236 } catch (CoreException x) { 237 handleCoreException(x); 238 } 239 } 240 return false; 241 } 242 243 251 public static String getMarkerType(IMarker marker) { 252 try { 253 return marker.getType(); 254 } catch (CoreException x) { 255 handleCoreException(x); 256 } 257 return null; 258 } 259 260 267 public static String getMessage(IMarker marker) { 268 return marker.getAttribute(IMarker.MESSAGE, null); 269 } 270 271 279 public static void setCharEnd(IMarker marker, int charEnd) { 280 setIntAttribute(marker, IMarker.CHAR_END, charEnd); 281 } 282 283 292 public static void setCharEnd(Map map, int charEnd) { 293 map.put(IMarker.CHAR_END, new Integer (charEnd)); 294 } 295 296 304 public static void setCharStart(IMarker marker, int charStart) { 305 setIntAttribute(marker, IMarker.CHAR_START, charStart); 306 } 307 308 317 public static void setCharStart(Map map, int charStart) { 318 map.put(IMarker.CHAR_START, new Integer (charStart)); 319 } 320 321 328 private static void setIntAttribute(IMarker marker, String attributeName, int value) { 329 try { 330 if (marker.exists()) 331 marker.setAttribute(attributeName, value); 332 } catch (CoreException e) { 333 handleCoreException(e); 334 } 335 } 336 337 345 public static void setLineNumber(IMarker marker, int lineNum) { 346 setIntAttribute(marker, IMarker.LINE_NUMBER, lineNum); 347 } 348 349 358 public static void setLineNumber(Map map, int lineNum) { 359 map.put(IMarker.LINE_NUMBER, new Integer (lineNum)); 360 } 361 362 371 public static void setMessage(Map map, String message) { 372 map.put(IMarker.MESSAGE, message); 373 } 374 375 387 public static void createMarker(final IResource resource, final Map attributes, final String markerType) throws CoreException { 388 389 IWorkspaceRunnable r= new IWorkspaceRunnable() { 390 public void run(IProgressMonitor monitor) throws CoreException { 391 IMarker marker= resource.createMarker(markerType); 392 marker.setAttributes(attributes); 393 } 394 }; 395 396 resource.getWorkspace().run(r, null,IWorkspace.AVOID_UPDATE, null); 397 } 398 399 407 public static String [] getSuperTypes(String markerType) { 408 if (fgMarkerTypeHierarchy == null) 409 fgMarkerTypeHierarchy= new MarkerTypeHierarchy(); 410 return fgMarkerTypeHierarchy.getSuperTypes(markerType); 411 } 412 } 413 | Popular Tags |