1 package org.objectweb.modfact.jmi.reflect; 2 3 import java.util.List ; 4 import java.io.IOException ; 5 import java.util.Collection ; 6 import java.util.HashSet ; 7 import java.util.Hashtable ; 8 import java.util.Iterator ; 9 import java.util.Map ; 10 import java.util.Set ; 11 12 import javax.jmi.reflect.RefAssociationLink; 13 import javax.jmi.reflect.RefBaseObject; 14 import javax.jmi.reflect.RefObject; 15 import javax.jmi.reflect.RefPackage; 16 import javax.jmi.xmi.MalformedXMIException; 17 import javax.jmi.xmi.XmiReader; 18 import javax.jmi.xmi.XmiWriter; 19 20 import org.objectweb.modfact.jmi.xmi.XmiReaderImpl; 21 import org.objectweb.modfact.jmi.xmi.XmiWriterImpl; 22 23 public class RefPackageFactory { 24 25 public XmiReader xmiReader = new XmiReaderImpl(); 26 public XmiWriter xmiWriter = new XmiWriterImpl(); 27 28 public RefPackage createM2() { 29 return RefPackageImpl.getM2Repository(); 30 } 31 32 public RefPackage createM2(String m2url) 33 throws MalformedXMIException, IOException { 34 35 RefPackage p = RefPackageImpl.getM2Repository(); 36 xmiReader.read(m2url, p); 37 return p; 38 } 39 40 public RefPackage createM1(String m2url) 41 throws MalformedXMIException, IOException { 42 RefPackage m2Extent = createM2(m2url); 43 Collection outermostMetaPackages = ReflectHelper.getOuterMostMetaPackages(m2Extent); 44 return createM1(outermostMetaPackages); 45 } 46 47 public RefPackage createM1(Collection outermostMetaPackages) { 48 49 Map extentMap = createExtentMap(outermostMetaPackages); 50 Collection topIndependantExtents = setImports(outermostMetaPackages, extentMap); 51 52 if (topIndependantExtents.size() == 1) { 53 return (RefPackage) topIndependantExtents.iterator().next(); 54 } 55 56 System.err.println("[warning] found " +topIndependantExtents.size() +" extents: "+ topIndependantExtents ); 57 58 Iterator it = topIndependantExtents.iterator(); 60 while(it.hasNext()) { 61 RefPackage extent = (RefPackage) it.next(); 62 if(!extent.refAllClasses().isEmpty()) { 63 return extent; 64 } 65 } 66 return (RefPackage) topIndependantExtents.iterator().next(); 67 } 68 69 70 public Map createM1s(String m2url) 71 throws MalformedXMIException, IOException { 72 RefPackage m2Extent = createM2(m2url); 73 Collection outermostMetaPackages = ReflectHelper.getOuterMostMetaPackages(m2Extent); 74 return createM1s(outermostMetaPackages); 75 } 76 77 public Map createM1s(Collection outermostMetaPackages) 79 throws MalformedXMIException, IOException { 80 81 if(outermostMetaPackages.isEmpty()) { 82 throw new RuntimeException ("NoPackageDefined"); 83 } 84 Map extentMap = createExtentMap(outermostMetaPackages); 85 setImports(outermostMetaPackages, extentMap); 86 87 return extentMap; 88 } 89 90 private Map createExtentMap(Collection outermostMetaPackages) { 91 Map extentMap = new Hashtable (); 94 Iterator it = outermostMetaPackages.iterator(); 95 while (it.hasNext()) { 96 RefObject metap = (RefObject) it.next(); 97 RefPackage extent = RefPackageImpl.getM1Repository(metap); 98 extentMap.put(metap.refGetValue("name"), extent); 99 } 100 return extentMap; 101 } 102 103 104 private Collection setImports(Collection outermostMetaPackages, Map extentMap) { 106 Set topIndependantExtents = new HashSet (extentMap.values()); 107 108 RefPackage m2Extent = ((RefObject) outermostMetaPackages.iterator().next()).refOutermostPackage(); 110 Iterator it = m2Extent.refAssociation("Aliases").refAllLinks().iterator(); 111 while (it.hasNext()) { 112 RefAssociationLink l = (RefAssociationLink) it.next(); 113 RefObject importObject = l.refFirstEnd(); 114 RefObjectImpl importerPackage = 115 (RefObjectImpl) importObject.refGetValue("container"); 116 if (!"Package" 118 .equals(importerPackage.refMetaObject().refGetValue("name"))) { 119 continue; 120 } 121 List importerPackageName = ReflectHelper.getQualifiedName(importerPackage); 122 123 RefPackageImpl importerExtent =( RefPackageImpl) ReflectHelper.resolveQualifiedNameInExtents(extentMap, importerPackageName); 124 if(importerExtent==null) { 125 continue; 126 } 127 128 RefObjectImpl importedMetaObject = (RefObjectImpl) l.refSecondEnd(); 129 String importedObjectType = ReflectHelper.getType(importedMetaObject); 130 131 List importedObjectName = ReflectHelper.getQualifiedName(importedMetaObject); 132 133 RefBaseObject importedNs = ReflectHelper.resolveQualifiedNameInExtents(extentMap, importedObjectName); 134 if(importedNs==null) { 135 continue; 136 } 137 138 String unqname = 139 (String ) importedObjectName.get(importedObjectName.size() - 1); 140 if ("Package".equals(importedObjectType)) { 141 importerExtent.setImportedExtent( 142 unqname, 143 (RefPackageImpl) importedNs); 144 } else if ("Class".equals(importedObjectType)) { 145 importerExtent.setImportedClassProxy( 146 unqname, 147 (RefClassImpl) importedNs); 148 } 149 topIndependantExtents.remove(importedNs); 150 } 151 return topIndependantExtents; 152 153 } 154 155 public RefPackage createM1(String m2url, String m1url) 156 throws MalformedXMIException, IOException { 157 158 RefPackage p = createM1(m2url); 159 xmiReader.read(m1url, p); 160 return p; 161 } 162 163 } 164 | Popular Tags |