1 package org.objectweb.modfact.jmi.reflect; 2 3 import javax.jmi.reflect.*; 4 import javax.jmi.xmi.MalformedXMIException; 5 import javax.jmi.xmi.XmiReader; 6 import org.objectweb.modfact.jmi.xmi.XmiReaderImpl; 7 8 import java.io.IOException ; 9 import java.util.*; 10 11 public class RefPackageImpl extends DataTypeContainer 12 implements RefPackage, java.io.Serializable { 13 14 boolean canModify = true; 17 18 List packageNames = new Vector(); 19 List classNames = new Vector(); 20 List associationNames = new Vector(); 21 22 RefPackageImpl[] packages; 23 RefClassImpl[] classes; 24 RefAssociationImpl[] associations; 25 26 27 Map importedPackageMap = new Hashtable(); 28 Map importedClassMap = new Hashtable(); 29 30 31 public RefPackageImpl() { 34 } 35 36 private static Object lock = ""; 37 38 43 public static RefPackage getM2Repository() { 44 synchronized(lock) { 45 if(m3==null) { 46 m3 = new M3Client().loadModel(); 47 } 48 } 49 RefPackageImpl p = new org.objectweb.modfact.jmi.repository.javax.jmi.model.ModelPackageImpl(); 50 initRepository(p, (RefObjectImpl)m3.refMetaObject()); 51 return p; 52 } 53 54 59 public static RefPackage getM1Repository(RefObject metaObject) { 60 RefPackageImpl p = new RefPackageImpl(); 61 initRepository(p, metaObject); 62 return p; 63 } 64 65 66 public static void initRepository(RefPackageImpl p, RefObject metaObject) { 67 p.metaObject = (RefObjectImpl)metaObject; 68 p.name = (String )metaObject.refGetValue("name"); 69 p.readContentsFromMetaObject(); 70 } 71 72 public static RefObject createMetaObject(String packageName, String metaModelURL) 74 throws MalformedXMIException, IOException { 75 XmiReader reader = new XmiReaderImpl(); 76 RefPackage p = RefPackageImpl.getM2Repository(); 77 reader.read(metaModelURL,p); 78 Iterator it = p.refClass("Package").refAllOfClass().iterator(); 79 while(it.hasNext()) { 80 RefObject o = (RefObject) it.next(); 81 if( packageName.equalsIgnoreCase((String )o.refGetValue("name")) ) { 82 return o; 83 } 84 } 85 return null; 86 } 87 88 95 public static RefObject createMetaObject(String metaModelURL) 96 throws MalformedXMIException, IOException { 97 XmiReader reader = new XmiReaderImpl(); 98 RefPackage p = RefPackageImpl.getM2Repository(); 99 reader.read(metaModelURL,p); 100 RefObject aClass = (RefObject) p.refClass("Class").refAllOfClass().iterator().next(); 101 return (RefObject) aClass.refOutermostComposite(); 102 } 103 104 105 public java.util.Collection refAllAssociations() { 106 List l = arrayToList(associations); 107 return l; 108 } 109 110 public java.util.Collection refAllClasses() { 111 List l = arrayToList(classes); 112 return l; 113 } 114 115 public java.util.Collection refAllPackages() { 116 List l = arrayToList(packages); 117 return l; 118 } 119 120 public javax.jmi.reflect.RefAssociation refAssociation(String str) { 122 int index = associationNames.indexOf(str); 123 if(index<0) { 124 throw new RuntimeException ("No association named '" +str +"' in '" +name +"'"); 125 } 126 return associations[index]; 127 } 128 129 public javax.jmi.reflect.RefAssociation refAssociation(javax.jmi.reflect.RefObject refObject) { 130 String name = (String ) refObject.refGetValue("name"); 131 return refAssociation(name); 132 } 133 134 public javax.jmi.reflect.RefClass refClass(String str) { 135 int index = classNames.indexOf( str ); 136 if(index>=0) { 137 return classes[index]; 138 } 139 if(importedClassMap.containsKey(str)) { 140 return (RefClass) importedClassMap.get(str); 141 } 142 throw new RuntimeException (this +" No class named '" +str +"' in '" +name +"'"); 143 } 144 145 public javax.jmi.reflect.RefClass refClass(javax.jmi.reflect.RefObject refObject) { 146 String name = (String ) refObject.refGetValue("name"); 147 return refClass(name); 148 } 149 150 public javax.jmi.reflect.RefPackage refPackage(String str) { 151 int index = packageNames.indexOf(str); 152 if(index>=0) 153 return packages[index]; 154 155 if(importedPackageMap.containsKey(str)) { 156 return (RefPackage) importedPackageMap.get(str); 157 } 158 159 160 throw new RuntimeException ("No Package named '" +str +"' in '" +name +"'"); 161 } 162 163 public javax.jmi.reflect.RefPackage refPackage(javax.jmi.reflect.RefObject refObject) { 164 String name = (String ) refObject.refGetValue("name"); 165 return refPackage(name); 166 } 167 168 public void refDelete() { 169 throw new RuntimeException ("No implementation: RefPackage.refDelete"); 170 } 171 172 public javax.jmi.reflect.RefObject refMetaObject() { 173 if(metaObject!=null) return metaObject; 174 return findObjectByName( this.refClass("Package"), name); 175 } 176 177 void readContentsFromMetaObject() { 178 179 List packageList_ = new Vector(); 180 List classList_ = new Vector(); 181 List assoList_ = new Vector(); 182 183 Iterator it = ((Collection)metaObject.refGetValue("contents")).iterator(); 184 while(it.hasNext()) { 185 RefObjectImpl content = (RefObjectImpl)it.next(); 186 String contentName = (String ) content.refGetValue("name"); 187 String contentType = (String ) content.refMetaObject().refGetValue("name"); 188 if(contentType.equals("Class")) { 189 classNames.add(contentName); 190 191 RefClassImpl c = newClass(contentName); 192 193 c.metaObject = content; 194 c.container = this; 195 c.name = contentName; 196 c.readContentsFromMetaObject(); 197 198 classList_.add(c); 199 200 } else if(contentType.equals("Package")) { 201 packageNames.add(contentName); 202 203 RefPackageImpl p = newPackage(contentName); 204 p.metaObject = content; 205 p.container = this; 206 p.name = contentName; 207 p.readContentsFromMetaObject(); 209 packageList_.add(p); 210 211 } else if(contentType.equals("Association")) { 212 213 associationNames.add(contentName); 214 215 Iterator it2 = ((Collection)content.refGetValue("contents")).iterator(); 216 String [] endNames = new String [2]; 217 String [] typeNames = new String [2]; 218 boolean[] isOrdered = new boolean[2]; 219 int i = 0; 220 while(it2.hasNext()) { 221 RefObject assoContent = (RefObject) it2.next(); 222 if(assoContent.refMetaObject().refGetValue("name").equals("AssociationEnd")) { 223 endNames[i] = (String )assoContent.refGetValue("name"); 224 RefObject type = (RefObject)assoContent.refGetValue("type"); 225 typeNames[i] = (String ) type.refGetValue("name"); 226 RefStruct mul = (RefStruct)assoContent.refGetValue("multiplicity"); 227 isOrdered[i] = ((Boolean )mul.refGetValue("isOrdered")).booleanValue(); 228 i++; 229 } 230 } 231 RefAssociationImpl a = newAssociation(contentName); 232 a.metaObject = content; 233 a.container = this; 234 a.name = contentName; 235 a.firstEndName = endNames[0]; 236 a.secondEndName = endNames[1]; 237 a.type1 = typeNames[0]; 238 a.type2 = typeNames[1]; 239 a.isEnd1Ordered = isOrdered[0]; 240 a.isEnd2Ordered = isOrdered[1]; 241 242 assoList_.add(a); 243 } else if(contentType.equals("Import")) { 244 246 } 247 } 248 249 classes = (RefClassImpl[]) classList_.toArray(new RefClassImpl[0]); 250 packages = (RefPackageImpl[]) packageList_.toArray(new RefPackageImpl[0]); 251 associations = (RefAssociationImpl[]) assoList_.toArray(new RefAssociationImpl[0]); 252 253 readDataTypeContents(); 254 255 } 256 257 258 void setImportedExtent(String name, RefPackageImpl extent) { 259 importedPackageMap.put(name, extent); 260 } 261 262 void setImportedClassProxy(String name, RefClassImpl proxy) { 263 importedClassMap.put(name, proxy); 264 } 265 public RefPackageImpl newPackage(String name) { 267 return new RefPackageImpl(); 268 269 } 270 271 public RefClassImpl newClass(String name) { 272 return new RefClassImpl(); 273 } 274 275 public RefAssociationImpl newAssociation(String name) { 276 return new RefAssociationImpl(); 277 } 278 279 280 } 281 | Popular Tags |