KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > reflect > RefPackageFactory


1 package org.objectweb.modfact.jmi.reflect;
2
3 import java.util.List JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.HashSet JavaDoc;
7 import java.util.Hashtable JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Set JavaDoc;
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 JavaDoc m2url)
33         throws MalformedXMIException, IOException JavaDoc {
34
35         RefPackage p = RefPackageImpl.getM2Repository();
36         xmiReader.read(m2url, p);
37         return p;
38     }
39     
40     public RefPackage createM1(String JavaDoc m2url)
41         throws MalformedXMIException, IOException JavaDoc {
42         RefPackage m2Extent = createM2(m2url);
43         Collection JavaDoc outermostMetaPackages = ReflectHelper.getOuterMostMetaPackages(m2Extent);
44         return createM1(outermostMetaPackages);
45     }
46
47     public RefPackage createM1(Collection JavaDoc outermostMetaPackages) {
48         
49         Map JavaDoc extentMap = createExtentMap(outermostMetaPackages);
50         Collection JavaDoc 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         // return the package that contains at least one class
59
Iterator JavaDoc 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 JavaDoc createM1s(String JavaDoc m2url)
71         throws MalformedXMIException, IOException JavaDoc {
72         RefPackage m2Extent = createM2(m2url);
73         Collection JavaDoc outermostMetaPackages = ReflectHelper.getOuterMostMetaPackages(m2Extent);
74         return createM1s(outermostMetaPackages);
75     }
76
77     //return extent map
78
public Map JavaDoc createM1s(Collection JavaDoc outermostMetaPackages)
79         throws MalformedXMIException, IOException JavaDoc {
80             
81         if(outermostMetaPackages.isEmpty()) {
82             throw new RuntimeException JavaDoc("NoPackageDefined");
83         }
84         Map JavaDoc extentMap = createExtentMap(outermostMetaPackages);
85         setImports(outermostMetaPackages, extentMap);
86         
87         return extentMap;
88     }
89     
90     private Map JavaDoc createExtentMap(Collection JavaDoc outermostMetaPackages) {
91         // create extent for each outermost packages
92
// map outermost metaPackage to extent
93
Map JavaDoc extentMap = new Hashtable JavaDoc();
94         Iterator JavaDoc 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     // return top independant extents
105
private Collection JavaDoc setImports(Collection JavaDoc outermostMetaPackages, Map JavaDoc extentMap) {
106         Set JavaDoc topIndependantExtents = new HashSet JavaDoc(extentMap.values());
107
108         // links imported extents
109
RefPackage m2Extent = ((RefObject) outermostMetaPackages.iterator().next()).refOutermostPackage();
110         Iterator JavaDoc 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             //verify that the importer is a package
117
if (!"Package"
118                 .equals(importerPackage.refMetaObject().refGetValue("name"))) {
119                 continue;
120             }
121             List JavaDoc 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 JavaDoc importedObjectType = ReflectHelper.getType(importedMetaObject);
130             
131             List JavaDoc importedObjectName = ReflectHelper.getQualifiedName(importedMetaObject);
132
133             RefBaseObject importedNs = ReflectHelper.resolveQualifiedNameInExtents(extentMap, importedObjectName);
134             if(importedNs==null) {
135                 continue;
136             }
137             
138             String JavaDoc unqname =
139                 (String JavaDoc) 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 JavaDoc m2url, String JavaDoc m1url)
156         throws MalformedXMIException, IOException JavaDoc {
157
158         RefPackage p = createM1(m2url);
159         xmiReader.read(m1url, p);
160         return p;
161     }
162
163 }
164
Popular Tags