KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > MarkerTypeDefinitionCache


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.core.internal.resources;
12
13 import java.util.*;
14 import org.eclipse.core.resources.ResourcesPlugin;
15 import org.eclipse.core.runtime.*;
16
17 public class MarkerTypeDefinitionCache {
18     static class MarkerTypeDefinition {
19         boolean isPersistent = false;
20         Set superTypes;
21
22         MarkerTypeDefinition(IExtension ext) {
23             IConfigurationElement[] elements = ext.getConfigurationElements();
24             for (int i = 0; i < elements.length; i++) {
25                 IConfigurationElement element = elements[i];
26                 // supertype
27
final String JavaDoc elementName = element.getName();
28                 if (elementName.equalsIgnoreCase("super")) { //$NON-NLS-1$
29
String JavaDoc aType = element.getAttribute("type"); //$NON-NLS-1$
30
if (aType != null) {
31                         if (superTypes == null)
32                             superTypes = new HashSet(8);
33                         //note that all marker type names will be in the intern table
34
//already because there is invariably a constant to describe
35
//the type name
36
superTypes.add(aType.intern());
37                     }
38                 }
39                 // persistent
40
if (elementName.equalsIgnoreCase("persistent")) { //$NON-NLS-1$
41
String JavaDoc bool = element.getAttribute("value"); //$NON-NLS-1$
42
if (bool != null)
43                         this.isPersistent = Boolean.valueOf(bool).booleanValue();
44                 }
45                 // XXX: legacy code for support of <transient> tag. remove later.
46
if (elementName.equalsIgnoreCase("transient")) { //$NON-NLS-1$
47
String JavaDoc bool = element.getAttribute("value"); //$NON-NLS-1$
48
if (bool != null)
49                         this.isPersistent = !Boolean.valueOf(bool).booleanValue();
50                 }
51             }
52         }
53     }
54
55     /**
56      * The marker type definitions. Maps String (markerId) -> MarkerTypeDefinition
57      */

58     protected HashMap definitions;
59
60     /** Constructs a new type cache.
61      */

62     public MarkerTypeDefinitionCache() {
63         loadDefinitions();
64         HashSet toCompute = new HashSet(definitions.keySet());
65         for (Iterator i = definitions.keySet().iterator(); i.hasNext();) {
66             String JavaDoc markerId = (String JavaDoc) i.next();
67             if (toCompute.contains(markerId))
68                 computeSuperTypes(markerId, toCompute);
69         }
70     }
71
72     /**
73      * Computes the transitive set of super types of the given marker type.
74      * @param markerId The type to compute super types for
75      * @param toCompute The set of types that have not yet had their
76      * supertypes computed.
77      * @return The transitive set of super types for this marker, or null
78      * if this marker is not defined or has no super types.
79      */

80     private Set computeSuperTypes(String JavaDoc markerId, Set toCompute) {
81         MarkerTypeDefinition def = (MarkerTypeDefinition) definitions.get(markerId);
82         if (def == null || def.superTypes == null) {
83             //nothing to do if there are no supertypes
84
toCompute.remove(markerId);
85             return null;
86         }
87         Set transitiveSuperTypes = new HashSet(def.superTypes);
88         for (Iterator it = def.superTypes.iterator(); it.hasNext();) {
89             String JavaDoc superId = (String JavaDoc) it.next();
90             Set toAdd = null;
91             if (toCompute.contains(superId)) {
92                 //this type's super types have not been compute yet. Do recursive call
93
toAdd = computeSuperTypes(superId, toCompute);
94             } else {
95                 // we have already computed this super type's super types (or it doesn't exist)
96
MarkerTypeDefinition parentDef = (MarkerTypeDefinition) definitions.get(superId);
97                 if (parentDef != null)
98                     toAdd = parentDef.superTypes;
99             }
100             if (toAdd != null)
101                 transitiveSuperTypes.addAll(toAdd);
102         }
103         def.superTypes = transitiveSuperTypes;
104         toCompute.remove(markerId);
105         return transitiveSuperTypes;
106     }
107
108     /**
109      * Returns true if the given marker type is defined to be persistent.
110      */

111     public boolean isPersistent(String JavaDoc type) {
112         MarkerTypeDefinition def = (MarkerTypeDefinition) definitions.get(type);
113         return def != null && def.isPersistent;
114     }
115
116     /**
117      * Returns true if the given target class has the specified type as a super type.
118      */

119     public boolean isSubtype(String JavaDoc type, String JavaDoc superType) {
120         //types are considered super types of themselves
121
if (type.equals(superType))
122             return true;
123         MarkerTypeDefinition def = (MarkerTypeDefinition) definitions.get(type);
124         return def != null && def.superTypes != null && def.superTypes.contains(superType);
125     }
126
127     private void loadDefinitions() {
128         IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(ResourcesPlugin.PI_RESOURCES, ResourcesPlugin.PT_MARKERS);
129         IExtension[] types = point.getExtensions();
130         definitions = new HashMap(types.length);
131         for (int i = 0; i < types.length; i++)
132             definitions.put(types[i].getUniqueIdentifier().intern(), new MarkerTypeDefinition(types[i]));
133     }
134 }
135
Popular Tags