KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > MarkerUtilities


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.texteditor;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
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 /**
37  * Utility class for accessing marker attributes. The static methods provided
38  * on this class provide internal exception handling (unexpected
39  * <code>CoreException</code>s are logged to workbench).
40  * <p>
41  * This class provides static methods only; it is not intended to be
42  * instantiated or subclassed by clients.
43  * </p>
44  */

45 public final class MarkerUtilities {
46
47     /**
48      * Internal marker super type hierarchy cache.
49      * TODO this cache is currently unbound, i.e. only limited by the number of marker types
50      */

51     private static class MarkerTypeHierarchy {
52
53         private Map JavaDoc fTypeMap;
54         private Map JavaDoc fSuperTypesCache= new HashMap JavaDoc();
55
56         public String JavaDoc[] getSuperTypes(String JavaDoc typeName) {
57             String JavaDoc[] cachedTypes= (String JavaDoc[]) 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 JavaDoc[] computeSuperTypes(String JavaDoc typeName) {
66             ArrayList JavaDoc types= new ArrayList JavaDoc();
67             appendAll(types, getDirectSuperTypes(typeName));
68             int index= 0;
69             while (index < types.size()) {
70                 String JavaDoc type= (String JavaDoc) types.get(index++);
71                 appendAll(types, getDirectSuperTypes(type));
72             }
73
74             String JavaDoc[] superTypes= new String JavaDoc[types.size()];
75             types.toArray(superTypes);
76             return superTypes;
77         }
78
79         private String JavaDoc[] getDirectSuperTypes(String JavaDoc typeName) {
80             return (String JavaDoc[]) getTypeMap().get(typeName);
81         }
82
83         private void appendAll(List JavaDoc list, Object JavaDoc[] objects) {
84             if (objects == null)
85                 return;
86             for (int i= 0; i < objects.length; i++) {
87                 Object JavaDoc o= objects[i];
88                 if (!list.contains(o))
89                     list.add(o);
90             }
91         }
92
93         private Map JavaDoc getTypeMap() {
94             if (fTypeMap == null)
95                 fTypeMap= readTypes();
96             return fTypeMap;
97         }
98
99         private Map JavaDoc readTypes() {
100             HashMap JavaDoc allTypes= new HashMap JavaDoc();
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 JavaDoc types= new ArrayList JavaDoc();
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")) { //$NON-NLS-1$
111
String JavaDoc type = element.getAttribute("type"); //$NON-NLS-1$
112
if (type != null) {
113                                 types.add(type);
114                             }
115                         }
116                     }
117                     String JavaDoc[] superTypes= new String JavaDoc[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     /**
131      * Don't allow instantiation.
132      */

133     private MarkerUtilities() {
134     }
135
136     /**
137      * Returns the ending character offset of the given marker.
138      *
139      * @param marker the marker
140      * @return the ending character offset, or <code>-1</code> if not set
141      * @see IMarker#CHAR_END
142      * @see IMarker#getAttribute(java.lang.String, int)
143      */

144     public static int getCharEnd(IMarker marker) {
145         return getIntAttribute(marker, IMarker.CHAR_END, -1);
146     }
147
148     /**
149      * Returns the starting character offset of the given marker.
150      *
151      * @param marker the marker
152      * @return the starting character offset, or <code>-1</code> if not set
153      * @see IMarker#CHAR_START
154      * @see IMarker#getAttribute(java.lang.String,int)
155      */

156     public static int getCharStart(IMarker marker) {
157         return getIntAttribute(marker, IMarker.CHAR_START, -1);
158     }
159
160     /**
161      * Returns the specified attribute of the given marker as an integer.
162      * Returns the given default if the attribute value is not an integer.
163      *
164      * @param marker the marker
165      * @param attributeName the name of the attribute
166      * @param defaultValue the default value
167      * @return the attribute's value or the default value
168      * if the attribute does not exist or isn't an int
169      */

170     private static int getIntAttribute(IMarker marker, String JavaDoc attributeName, int defaultValue) {
171         if (marker.exists())
172             return marker.getAttribute(attributeName, defaultValue);
173         return defaultValue;
174     }
175
176     /**
177      * Returns the line number of the given marker.
178      *
179      * @param marker the marker
180      * @return the line number, or <code>-1</code> if not set
181      * @see IMarker#LINE_NUMBER
182      * @see IMarker#getAttribute(java.lang.String,int)
183      */

184     public static int getLineNumber(IMarker marker) {
185         return getIntAttribute(marker, IMarker.LINE_NUMBER, -1);
186     }
187
188     /**
189      * Returns the priority of the given marker.
190      *
191      * @param marker the marker
192      * @return the priority, or <code>IMarker.PRIORITY_NORMAL</code> if not set
193      * @see IMarker#PRIORITY
194      * @see IMarker#PRIORITY_NORMAL
195      * @see IMarker#getAttribute(java.lang.String,int)
196      */

197     public static int getPriority(IMarker marker) {
198         return getIntAttribute(marker, IMarker.PRIORITY, IMarker.PRIORITY_NORMAL);
199     }
200
201     /**
202      * Returns the severity of the given marker.
203      *
204      * @param marker the marker
205      * @return the priority, or <code>IMarker.SEVERITY_INFO</code> if not set
206      * @see IMarker#SEVERITY
207      * @see IMarker#SEVERITY_INFO
208      * @see IMarker#getAttribute(java.lang.String,int)
209      */

210     public static int getSeverity(IMarker marker) {
211         return getIntAttribute(marker, IMarker.SEVERITY, IMarker.SEVERITY_INFO);
212     }
213
214     /**
215      * Handles a core exception which occurs when accessing marker attributes.
216      *
217      * @param e the core exception
218      */

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     /**
226      * Returns whether the given marker is of the given type (either directly or indirectly).
227      *
228      * @param marker the marker to be checked
229      * @param type the reference type
230      * @return <code>true</code>if maker is an instance of the reference type
231      */

232     public static boolean isMarkerType(IMarker marker, String JavaDoc 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     /**
244      * Returns the marker type of the given marker or <code>null</code> if
245      * the type could not be determined.
246      *
247      * @param marker the marker
248      * @return the marker type
249      * @since 3.0
250      */

251     public static String JavaDoc getMarkerType(IMarker marker) {
252         try {
253             return marker.getType();
254         } catch (CoreException x) {
255             handleCoreException(x);
256         }
257         return null;
258     }
259
260     /**
261      * Returns the message associated with the given marker.
262      *
263      * @param marker the marker
264      * @return the message associated with the marker or <code>null</code>
265      * @since 3.0
266      */

267     public static String JavaDoc getMessage(IMarker marker) {
268         return marker.getAttribute(IMarker.MESSAGE, null);
269     }
270
271     /**
272      * Sets the ending character offset of the given marker.
273      *
274      * @param marker the marker
275      * @param charEnd the ending character offset
276      * @see IMarker#CHAR_END
277      * @see IMarker#setAttribute(java.lang.String,int)
278      */

279     public static void setCharEnd(IMarker marker, int charEnd) {
280         setIntAttribute(marker, IMarker.CHAR_END, charEnd);
281     }
282
283     /**
284      * Sets the ending character offset in the given map using the standard
285      * marker attribute name as the key.
286      *
287      * @param map the map (key type: <code>String</code>, value type:
288      * <code>Object</code>)
289      * @param charEnd the ending character offset
290      * @see IMarker#CHAR_END
291      */

292     public static void setCharEnd(Map JavaDoc map, int charEnd) {
293         map.put(IMarker.CHAR_END, new Integer JavaDoc(charEnd));
294     }
295
296     /**
297      * Sets the starting character offset of the given marker.
298      *
299      * @param marker the marker
300      * @param charStart the starting character offset
301      * @see IMarker#CHAR_START
302      * @see IMarker#setAttribute(java.lang.String,int)
303      */

304     public static void setCharStart(IMarker marker, int charStart) {
305         setIntAttribute(marker, IMarker.CHAR_START, charStart);
306     }
307
308     /**
309      * Sets the starting character offset in the given map using the standard
310      * marker attribute name as the key.
311      *
312      * @param map the map (key type: <code>String</code>, value type:
313      * <code>Object</code>)
314      * @param charStart the starting character offset
315      * @see IMarker#CHAR_START
316      */

317     public static void setCharStart(Map JavaDoc map, int charStart) {
318         map.put(IMarker.CHAR_START, new Integer JavaDoc(charStart));
319     }
320
321     /**
322      * Sets the specified attribute of the given marker as an integer.
323      *
324      * @param marker the marker
325      * @param attributeName the attribute name
326      * @param value the int value
327      */

328     private static void setIntAttribute(IMarker marker, String JavaDoc 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     /**
338      * Sets the line number of the given marker.
339      *
340      * @param marker the marker
341      * @param lineNum the line number
342      * @see IMarker#LINE_NUMBER
343      * @see IMarker#setAttribute(java.lang.String,int)
344      */

345     public static void setLineNumber(IMarker marker, int lineNum) {
346         setIntAttribute(marker, IMarker.LINE_NUMBER, lineNum);
347     }
348
349     /**
350      * Sets the line number in the given map using the standard marker attribute
351      * name as the key.
352      *
353      * @param map the map (key type: <code>String</code>, value type:
354      * <code>Object</code>)
355      * @param lineNum the line number
356      * @see IMarker#LINE_NUMBER
357      */

358     public static void setLineNumber(Map JavaDoc map, int lineNum) {
359         map.put(IMarker.LINE_NUMBER, new Integer JavaDoc(lineNum));
360     }
361
362     /**
363      * Sets the message in the given map using the standard marker attribute name
364      * as the key.
365      *
366      * @param map the map (key type: <code>String</code>, value type:
367      * <code>Object</code>)
368      * @param message the message
369      * @see IMarker#MESSAGE
370      */

371     public static void setMessage(Map JavaDoc map, String JavaDoc message) {
372         map.put(IMarker.MESSAGE, message);
373     }
374
375     /**
376      * Creates a marker on the given resource with the given type and attributes.
377      * <p>
378      * This method modifies the workspace (progress is not reported to the user).</p>
379      *
380      * @param resource the resource
381      * @param attributes the attribute map (key type: <code>String</code>,
382      * value type: <code>Object</code>)
383      * @param markerType the type of marker
384      * @throws CoreException if this method fails
385      * @see IResource#createMarker(java.lang.String)
386      */

387     public static void createMarker(final IResource resource, final Map JavaDoc attributes, final String JavaDoc 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     /**
400      * Returns the list of super types for the given marker.
401      * The list is a depth first list and maintains the sequence in which
402      * the super types are listed in the marker specification.
403      *
404      * @param markerType the marker's type
405      * @return a depth-first list of all super types of the given marker type
406      */

407     public static String JavaDoc[] getSuperTypes(String JavaDoc markerType) {
408         if (fgMarkerTypeHierarchy == null)
409             fgMarkerTypeHierarchy= new MarkerTypeHierarchy();
410         return fgMarkerTypeHierarchy.getSuperTypes(markerType);
411     }
412 }
413
Popular Tags