KickJava   Java API By Example, From Geeks To Geeks.

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


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 JavaDoc;
9 import java.util.*;
10
11 public class RefPackageImpl extends DataTypeContainer
12                              implements RefPackage, java.io.Serializable JavaDoc {
13     
14     // if this package is now in use to describe model instances
15
// then its contents cannot be modified
16
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     // used in serialization
32
// should not be called by users
33
public RefPackageImpl() {
34     }
35       
36     private static Object JavaDoc lock = "";
37
38     /**
39     * create a metamodel repository
40     *
41     * @deprecated To create a M1 RefPackage, RefPackageFactory.createM2(..) should be use instead
42     */

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     /**
55     * create model instance repository
56     *
57     * @deprecated To create a M1 RefPackage, RefPackageFactory.createM1(..) should be use instead
58     */

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 JavaDoc)metaObject.refGetValue("name");
69         p.readContentsFromMetaObject();
70     }
71     
72   // Helper
73
public static RefObject createMetaObject(String JavaDoc packageName, String JavaDoc metaModelURL)
74             throws MalformedXMIException, IOException JavaDoc {
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 JavaDoc)o.refGetValue("name")) ) {
82           return o;
83       }
84     }
85     return null;
86   }
87   
88  /**
89  * return outermost Package object (to be used as metaobject)
90  * - Assume that there is only one othermost package
91  * - This package must contain a class
92  *
93  * @deprecated To create a M1 RefPackage, RefPackageFactory.createM1(..) should be use instead
94  */

95   public static RefObject createMetaObject(String JavaDoc metaModelURL)
96         throws MalformedXMIException, IOException JavaDoc{
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 JavaDoc refAllAssociations() {
106         List l = arrayToList(associations);
107         return l;
108     }
109     
110     public java.util.Collection JavaDoc refAllClasses() {
111         List l = arrayToList(classes);
112         return l;
113     }
114     
115     public java.util.Collection JavaDoc refAllPackages() {
116         List l = arrayToList(packages);
117         return l;
118     }
119     
120     // method needs to be override with check
121
public javax.jmi.reflect.RefAssociation refAssociation(String JavaDoc str) {
122         int index = associationNames.indexOf(str);
123         if(index<0) {
124             throw new RuntimeException JavaDoc("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 JavaDoc name = (String JavaDoc) refObject.refGetValue("name");
131         return refAssociation(name);
132     }
133     
134     public javax.jmi.reflect.RefClass refClass(String JavaDoc 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 JavaDoc(this +" No class named '" +str +"' in '" +name +"'");
143     }
144     
145     public javax.jmi.reflect.RefClass refClass(javax.jmi.reflect.RefObject refObject) {
146         String JavaDoc name = (String JavaDoc) refObject.refGetValue("name");
147         return refClass(name);
148     }
149     
150     public javax.jmi.reflect.RefPackage refPackage(String JavaDoc 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 JavaDoc("No Package named '" +str +"' in '" +name +"'");
161     }
162     
163     public javax.jmi.reflect.RefPackage refPackage(javax.jmi.reflect.RefObject refObject) {
164         String JavaDoc name = (String JavaDoc) refObject.refGetValue("name");
165         return refPackage(name);
166     }
167  
168     public void refDelete() {
169         throw new RuntimeException JavaDoc("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 JavaDoc contentName = (String JavaDoc) content.refGetValue("name");
187             String JavaDoc contentType = (String JavaDoc) 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(); // bug fixed
208

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 JavaDoc[] endNames = new String JavaDoc[2];
217                 String JavaDoc[] typeNames = new String JavaDoc[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 JavaDoc)assoContent.refGetValue("name");
224                         RefObject type = (RefObject)assoContent.refGetValue("type");
225                         typeNames[i] = (String JavaDoc) type.refGetValue("name");
226                         RefStruct mul = (RefStruct)assoContent.refGetValue("multiplicity");
227                         isOrdered[i] = ((Boolean JavaDoc)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                 //to do
245

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 JavaDoc name, RefPackageImpl extent) {
259         importedPackageMap.put(name, extent);
260     }
261     
262     void setImportedClassProxy(String JavaDoc name, RefClassImpl proxy) {
263         importedClassMap.put(name, proxy);
264     }
265     /////////////// to be over writen ////////////////
266
public RefPackageImpl newPackage(String JavaDoc name) {
267       return new RefPackageImpl();
268     
269     }
270     
271     public RefClassImpl newClass(String JavaDoc name) {
272       return new RefClassImpl();
273     }
274     
275     public RefAssociationImpl newAssociation(String JavaDoc name) {
276       return new RefAssociationImpl();
277     }
278     
279     
280 }
281
Popular Tags