KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > xmi > ObjectFactoryManager


1 package org.objectweb.modfact.jmi.xmi;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Hashtable JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import javax.jmi.reflect.*;
10
11 import org.objectweb.modfact.jmi.reflect.ReflectHelper;
12
13
14 /**
15  * Create RefObject, RefStruct, RefEnum from
16  * dot qualified name
17  * or unqualified name
18  * or qualified name (List)
19  */

20 public class ObjectFactoryManager {
21     
22     Map JavaDoc classProxies = new Hashtable JavaDoc();
23     Map JavaDoc structFactories = new Hashtable JavaDoc();
24     Map JavaDoc enumManagers = new Hashtable JavaDoc();
25     Map JavaDoc assoProxies = new Hashtable JavaDoc();
26
27     public ObjectFactoryManager(RefPackage outermostExtent) {
28         cacheAllFactories(outermostExtent);
29     }
30
31     private void cacheAllFactories(RefPackage p) {
32         Iterator JavaDoc it = ((Collection JavaDoc) p.refMetaObject().refGetValue("contents")).iterator();
33         while(it.hasNext()) {
34           RefObject content = (RefObject) it.next();
35           String JavaDoc name = (String JavaDoc) content.refGetValue("name");
36           String JavaDoc qname = ReflectHelper.dotQName(ReflectHelper.getQualifiedName(content));
37           String JavaDoc type = (String JavaDoc) content.refMetaObject().refGetValue("name");
38           if("Class".equals(type)) {
39             classProxies.put(name, p.refClass(name));
40             classProxies.put(qname, p.refClass(name));
41                         
42             cacheAllFactories(p.refClass(name));
43           
44           } else if("StructureType".equals(type)) {
45             structFactories.put(name, p);
46             structFactories.put(qname, p);
47             
48           } else if("EnumerationType".equals(type)) {
49             enumManagers.put(name, p);
50             enumManagers.put(qname, p);
51             
52           } else if("Association".equals(type)) {
53             assoProxies.put(name, p.refAssociation(name));
54             assoProxies.put(qname, p.refAssociation(name));
55                         
56           } else if("Package".equals(type)) {
57             cacheAllFactories(p.refPackage(name));
58                     
59           } else if("Import".equals(type)) {
60             try {
61               RefObject ns = (RefObject) content.refGetValue("importedNamespace");
62               String JavaDoc nsName = (String JavaDoc) ns.refGetValue("name");
63               String JavaDoc nsQName = ReflectHelper.dotQName(ReflectHelper.getQualifiedName(ns));
64               String JavaDoc nsType = (String JavaDoc) ns.refMetaObject().refGetValue("name");
65               if("Package".equals(nsType)) {
66                   cacheAllFactories(p.refPackage(nsName));
67                                 
68               } else if("Class".equals(nsType)){
69                   RefClass c = p.refClass(nsName);
70                   classProxies.put(nsName, c);
71                   classProxies.put(nsQName, c);
72                        
73                   cacheAllFactories(c);
74               }
75               
76             } catch(Exception JavaDoc e) {
77               // e.printStackTrace();
78
}
79           }
80         }
81       }
82     
83       private void cacheAllFactories(RefClass c) {
84           Iterator JavaDoc it = ((Collection JavaDoc) c.refMetaObject().refGetValue("contents")).iterator();
85           while(it.hasNext()) {
86             RefObject content = (RefObject) it.next();
87             String JavaDoc name = (String JavaDoc) content.refGetValue("name");
88             String JavaDoc qname = ReflectHelper.dotQName(ReflectHelper.getQualifiedName(content));
89             String JavaDoc type = (String JavaDoc) content.refMetaObject().refGetValue("name");
90             if("StructureType".equals(type)) {
91               structFactories.put(name, c);
92               structFactories.put(qname, c);
93                           
94             } else if("EnumerationType".equals(type)) {
95               enumManagers.put(name, c);
96               enumManagers.put(qname, c);
97                         
98             }
99           }
100       }
101     
102     public RefClass getRefClass(String JavaDoc name) {
103         name = name.substring(name.indexOf(":")+1);
104         return (RefClass) classProxies.get(name);
105     }
106       
107     public boolean canCreateObject(String JavaDoc name) {
108         name = name.substring(name.indexOf(":")+1);
109         return classProxies.containsKey(name);
110     }
111
112     public RefObject createObject(String JavaDoc name) {
113         name = name.substring(name.indexOf(":")+1);
114         RefClass c = (RefClass)classProxies.get(name);
115         if(c==null) {
116             System.err.println("warning: class '" +name +"' undefined");
117             return null;
118         }
119         RefObject o = c.refCreateInstance(null);
120         return o;
121     }
122         
123     
124     public RefEnum getEnum(String JavaDoc name, String JavaDoc value) {
125         name = name.substring(name.indexOf(":")+1);
126         Object JavaDoc m = enumManagers.get(name);
127         if(m==null) {
128           System.err.println("warning: enumtype '" +name +"' undefined");
129           return null;
130         }
131         String JavaDoc unqName = ReflectHelper.unqualifiedName(name);
132         if(m instanceof RefPackage) {
133           return ((RefPackage)m).refGetEnum(unqName, value);
134         }
135         try {
136             return ((RefClass)m).refGetEnum(unqName, value);
137         } catch(Exception JavaDoc e) {
138             System.err.println("warning: enum " +name +"." +value +" undefined");
139         }
140         return null;
141     }
142     
143     
144
145     
146
147         
148     public RefStruct createStruct(String JavaDoc name, List JavaDoc args) {
149         name = name.substring(name.indexOf(":")+1);
150         Object JavaDoc f = structFactories.get(name);
151         if(f==null) {
152             System.err.println("warning: struct '" +name +"' undefined");
153             return null;
154         }
155         String JavaDoc unqName = ReflectHelper.unqualifiedName(name);
156         if(f instanceof RefPackage) {
157           return ((RefPackage) f).refCreateStruct(unqName, args);
158         }
159         return ((RefClass) f).refCreateStruct(unqName, args);
160     }
161     
162     
163     
164
165 }
166
167
168
169
Popular Tags