1 package org.objectweb.modfact.jmi.xmi; 2 3 import java.util.Collection ; 4 import java.util.Hashtable ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 9 import javax.jmi.reflect.*; 10 11 import org.objectweb.modfact.jmi.reflect.ReflectHelper; 12 13 14 20 public class ObjectFactoryManager { 21 22 Map classProxies = new Hashtable (); 23 Map structFactories = new Hashtable (); 24 Map enumManagers = new Hashtable (); 25 Map assoProxies = new Hashtable (); 26 27 public ObjectFactoryManager(RefPackage outermostExtent) { 28 cacheAllFactories(outermostExtent); 29 } 30 31 private void cacheAllFactories(RefPackage p) { 32 Iterator it = ((Collection ) p.refMetaObject().refGetValue("contents")).iterator(); 33 while(it.hasNext()) { 34 RefObject content = (RefObject) it.next(); 35 String name = (String ) content.refGetValue("name"); 36 String qname = ReflectHelper.dotQName(ReflectHelper.getQualifiedName(content)); 37 String type = (String ) 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 nsName = (String ) ns.refGetValue("name"); 63 String nsQName = ReflectHelper.dotQName(ReflectHelper.getQualifiedName(ns)); 64 String nsType = (String ) 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 e) { 77 } 79 } 80 } 81 } 82 83 private void cacheAllFactories(RefClass c) { 84 Iterator it = ((Collection ) c.refMetaObject().refGetValue("contents")).iterator(); 85 while(it.hasNext()) { 86 RefObject content = (RefObject) it.next(); 87 String name = (String ) content.refGetValue("name"); 88 String qname = ReflectHelper.dotQName(ReflectHelper.getQualifiedName(content)); 89 String type = (String ) 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 name) { 103 name = name.substring(name.indexOf(":")+1); 104 return (RefClass) classProxies.get(name); 105 } 106 107 public boolean canCreateObject(String name) { 108 name = name.substring(name.indexOf(":")+1); 109 return classProxies.containsKey(name); 110 } 111 112 public RefObject createObject(String 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 name, String value) { 125 name = name.substring(name.indexOf(":")+1); 126 Object m = enumManagers.get(name); 127 if(m==null) { 128 System.err.println("warning: enumtype '" +name +"' undefined"); 129 return null; 130 } 131 String 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 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 name, List args) { 149 name = name.substring(name.indexOf(":")+1); 150 Object f = structFactories.get(name); 151 if(f==null) { 152 System.err.println("warning: struct '" +name +"' undefined"); 153 return null; 154 } 155 String 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 |