KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > jmi > xmi > ElementsCache


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.jmi.xmi;
21
22 import java.util.*;
23
24 import org.netbeans.lib.jmi.util.*;
25
26 import javax.jmi.reflect.*;
27 import javax.jmi.model.*;
28
29 public class ElementsCache extends Object JavaDoc {
30
31     // variables ................................................................
32

33     // cache storing structures' fields
34
private HashMap structureFields_cache = new HashMap ();
35
36     // caches storing instance-scoped and class-scoped attributes and references
37
private HashMap instanceAttributes_cache = new HashMap ();
38     private HashMap classAttributes_cache = new HashMap ();
39     private HashMap references_cache = new HashMap ();
40     private HashMap subtypes_cache = null;
41     
42     private RefPackage extent;
43     
44     // storage of all outermost packages
45
private HashMap outermostPackages = null;
46     // global cache used by @link #findOutermostPackages method
47
private HashMap trackedPackages = null;
48     // cache for resolved proxies enabling Enum and Struct creation and Association proxies
49
private HashMap proxies_cache = new HashMap ();
50     
51     // init .....................................................................
52

53     public ElementsCache (RefPackage extent) {
54         this.extent = extent;
55     }
56     
57     // methods ..................................................................
58

59     /**
60      * Detects all outermost packages related to the input extents and stores them.
61      * Stores all related namespaces as well.
62      */

63     private void findOutermostPackages (RefPackage pkg) {
64         if (trackedPackages.get (pkg) != null)
65             return;
66         String JavaDoc name;
67         MofPackage metaObj = (MofPackage) pkg.refMetaObject ();
68         if (metaObj.getContainer() == null) {
69             Iterator iter = metaObj.getQualifiedName ().iterator ();
70             String JavaDoc fqName = (String JavaDoc) iter.next ();
71             while (iter.hasNext ())
72                 fqName = fqName.concat (".").concat ((String JavaDoc) iter.next ());
73             outermostPackages.put (fqName, pkg);
74         }
75         trackedPackages.put (pkg, pkg);
76         Iterator iter = pkg.refAllPackages ().iterator ();
77         while (iter.hasNext ()) {
78             findOutermostPackages ((RefPackage) iter.next ());
79         }
80     }
81     
82     /**
83      * Finds all attributes and references belonging to a mof class and caches them.
84      */

85     private void cacheContainedElements (MofClass mofClass) {
86         List temp = new LinkedList ();
87         List superClasses = mofClass.allSupertypes ();
88         Namespace namespace = null;
89         Iterator it = superClasses.iterator ();
90         while (it.hasNext ()) {
91             namespace = (Namespace) it.next ();
92             temp.addAll (namespace.getContents ());
93         }
94         temp.addAll (mofClass.getContents ());
95         List instanceAttributes = new LinkedList ();
96         List classAttributes = new LinkedList ();
97         List references = new LinkedList ();
98         
99         it = temp.iterator ();
100         while (it.hasNext ()) {
101             RefObject refObject = (RefObject) it.next ();
102             if (refObject instanceof Feature) {
103                 boolean instanceLevel = ((Feature) refObject).getScope ().equals (ScopeKindEnum.INSTANCE_LEVEL);
104                 if ((refObject instanceof Attribute) && (!((Attribute) refObject).isDerived ())) {
105                     if (instanceLevel) {
106                         instanceAttributes.add (refObject);
107                     } else {
108                         classAttributes.add (refObject);
109                     }
110                 } else if (refObject instanceof Reference) {
111                     Association assoc = (Association) ((Reference) refObject).
112                         getReferencedEnd ().getContainer ();
113                     if (!assoc.isDerived ())
114                         references.add (refObject);
115                 } // else
116
} // if (refObject instanceof Feature)
117
} // while
118
instanceAttributes_cache.put (mofClass, instanceAttributes);
119         classAttributes_cache.put (mofClass, classAttributes);
120         references_cache.put (mofClass, references);
121     }
122
123     /**
124      * For a given mof class, returns list of all instance-scoped attributes
125      * (references are not included).
126      *
127      * @param mofClass
128      * @return list of all non-derived instance-scoped attributes (including inherited ones)
129      */

130     public List instanceAttributes (MofClass mofClass) {
131         List list = (List) instanceAttributes_cache.get (mofClass);
132         if (list == null) {
133             cacheContainedElements (mofClass);
134             list = (List) instanceAttributes_cache.get (mofClass);
135         }
136         return list;
137     }
138     
139     /**
140      * Returns attributes defined in the class (inhretited attributes are not included).
141      */

142     public List localInstanceAttributes (MofClass mofClass) {
143         Iterator it = mofClass.getContents ().iterator ();
144         List result = new LinkedList ();
145         while (it.hasNext ()) {
146             Object JavaDoc refObject = it.next ();
147             if (refObject instanceof Feature) {
148                 boolean instanceLevel = ((Feature) refObject).getScope ().equals (ScopeKindEnum.INSTANCE_LEVEL);
149                 if (instanceLevel && (refObject instanceof Attribute) && (!((Attribute) refObject).isDerived ())) {
150                     result.add (refObject);
151                 } // if
152
} // if
153
} // while
154
return result;
155     }
156     
157     /**
158      * For a given mof class, returns list of all class-scoped attributes.
159      *
160      * @param mofClass
161      * @return list of all non-derived class-scoped attributes (including inherited ones)
162      */

163     public List classAttributes (MofClass mofClass) {
164         List list = (List) classAttributes_cache.get (mofClass);
165         if (list == null) {
166             cacheContainedElements (mofClass);
167             list = (List) classAttributes_cache.get (mofClass);
168         }
169         return list;
170     }
171     
172     /**
173      * For a given mof class, returns list of all references.
174      *
175      * @param mofClass
176      * @return list of all non-derived references (including inherited ones)
177      */

178     public List references (MofClass mofClass) {
179         List list = (List) references_cache.get (mofClass);
180         if (list == null) {
181             cacheContainedElements (mofClass);
182             list = (List) references_cache.get (mofClass);
183         }
184         return list;
185     }
186     
187     /**
188      * Returns references defined in the class (inhretited references are not included).
189      */

190     public List localReferences (MofClass mofClass) {
191         Iterator it = mofClass.getContents ().iterator ();
192         List result = new LinkedList ();
193         while (it.hasNext ()) {
194             Object JavaDoc refObject = it.next ();
195             if (refObject instanceof Feature) {
196                 boolean instanceLevel = ((Feature) refObject).getScope ().equals (ScopeKindEnum.INSTANCE_LEVEL);
197                 if (instanceLevel && (refObject instanceof Reference)) {
198                     Association assoc = (Association) ((Reference) refObject).
199                         getReferencedEnd ().getContainer ();
200                     if (!assoc.isDerived ())
201                         result.add (refObject);
202                 } // if
203
} // if
204
} // while
205
return result;
206     }
207     
208     /**
209      * Returns list of all fields belonging to the given StructureType.
210      */

211     public List structureFields (StructureType type) {
212         List fields = (List) structureFields_cache.get (type);
213         if (fields != null)
214             return fields;
215         // find fields and cache them
216
fields = new LinkedList ();
217         Iterator content = type.getContents ().iterator ();
218         while (content.hasNext ()) {
219             Object JavaDoc element = content.next ();
220             if (element instanceof StructureField)
221                 fields.add (element);
222         } // while
223
structureFields_cache.put (type, fields);
224         return fields;
225     }
226     
227     /**
228      * Finds proxy object related to the container of a given meta model element.
229      *
230      * @param element meta model element
231      * @return related proxy object or <code>null</code> if proxy cannot be found
232      */

233     public RefBaseObject findProxy (ModelElement element) {
234         if (outermostPackages == null) {
235             trackedPackages = new HashMap ();
236             outermostPackages = new HashMap ();
237             findOutermostPackages (extent);
238         }
239         RefBaseObject proxy = (RefBaseObject) proxies_cache.get (element);
240         if (proxy != null)
241             return proxy;
242         LinkedList path = new LinkedList ();
243         ModelElement container = element.getContainer ();
244         while (container != null) {
245             path.add (container);
246             container = container.getContainer ();
247         }
248         MofPackage mofPackage = (MofPackage) path.removeLast ();
249         RefPackage refPackage = (RefPackage) outermostPackages.get (mofPackage.getName ());
250         if (refPackage == null) {
251             Iterator iter = outermostPackages.entrySet ().iterator ();
252             while (iter.hasNext ()) {
253                 RefPackage ref = (RefPackage) ((Map.Entry) iter.next ()).getValue ();
254                 MofPackage meta = (MofPackage) ref.refMetaObject ();
255                 if (meta.allSupertypes().contains(mofPackage)) {
256                     refPackage = ref;
257                     break;
258                 }
259             } // while
260
} // if
261

262         if (refPackage == null)
263             return null;
264         if (path.size () == 0)
265             proxy = refPackage;
266         while (path.size () > 0) {
267             ModelElement elem = (ModelElement) path.removeLast ();
268             if (elem instanceof MofPackage) {
269                 refPackage = refPackage.refPackage (elem);
270                 if (path.size () == 0)
271                     proxy = refPackage;
272             } else {
273                 if ((elem instanceof MofClass) && (path.size () == 0)) {
274                     RefClass refClass = refPackage.refClass (elem);
275                     proxy = refClass;
276                 } else
277                     break;
278             } // else
279
} // while
280
if (proxy != null)
281             proxies_cache.put (element, proxy);
282         return proxy;
283     }
284     
285     /**
286      * @return All subclassses of the given class (including non-direct subclasses and the class itself).
287      */

288     public Set getAllSubtypes (MofClass clazz) {
289         if (subtypes_cache == null) {
290             subtypes_cache = new HashMap ();
291             cacheSubtypes (extent);
292         }
293         return (Set) subtypes_cache.get (clazz);
294     }
295     
296     private void cacheSubtypes (RefPackage pkg) {
297         Iterator iter = pkg.refAllClasses ().iterator ();
298         while (iter.hasNext ()) {
299             MofClass mofClass = (MofClass) ((RefClass) iter.next ()).refMetaObject ();
300             Iterator supertypes = mofClass.allSupertypes ().iterator ();
301             while (supertypes.hasNext ()) {
302                 MofClass superClass = (MofClass) supertypes.next ();
303                 Set list = (Set) subtypes_cache.get (superClass);
304                 if (list == null) {
305                     list = new HashSet ();
306                     subtypes_cache.put (superClass, list);
307                 } // if
308
list.add (mofClass);
309             } // while
310
// add this mofClass as its own subtype
311
Set list = (Set) subtypes_cache.get (mofClass);
312             if (list == null) {
313                 list = new HashSet ();
314                 subtypes_cache.put (mofClass, list);
315             } // if
316
list.add (mofClass);
317         } // while
318
iter = pkg.refAllPackages ().iterator ();
319         while (iter.hasNext ())
320             cacheSubtypes ((RefPackage) iter.next ());
321     }
322     
323 }
324
325
Popular Tags