KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.modfact.jmi.reflect;
2
3 import java.util.*;
4 import javax.jmi.reflect.*;
5
6 public abstract class DataTypeContainer extends RefBaseObjectImpl {
7     
8     List structNames = new Vector();
9     List[] structQNames = null;;
10     List[] structFieldNameLists = null;
11     
12     List enumTypeNames = new Vector();
13     Object JavaDoc[] enumTypes = null;
14     
15     void readDataTypeContents() {
16
17         List structList = new Vector();
18         List enumList = new Vector();
19         
20         Iterator it = ((Collection)metaObject.refGetValue("contents")).iterator();
21         
22         while(it.hasNext()) {
23             RefObject content = (RefObject)it.next();
24             String JavaDoc contentName = (String JavaDoc) content.refGetValue("name");
25             String JavaDoc contentType = (String JavaDoc) content.refMetaObject().refGetValue("name");
26
27             if(contentType.equals("StructureType")) {
28                 structNames.add(contentName);
29                 List fieldNames = new Vector();
30                 Iterator it2 = ((Collection) content.refGetValue("contents")).iterator();
31                 while(it2.hasNext()) {
32                     RefObject cc = (RefObject)it2.next();
33                     String JavaDoc ccType = (String JavaDoc) cc.refMetaObject().refGetValue("name");
34                     if(ccType.equals("StructureField")) {
35                         fieldNames.add(cc.refGetValue("name"));
36                         // System.out.println("Structure field: " + cc.refGetValue("name"));
37
}
38                 }
39                 structList.add(fieldNames);
40                 
41             } else if(contentType.equals("EnumerationType")) {
42                 enumTypeNames.add(contentName);
43                 List labels = (List) content.refGetValue("labels");
44                 Object JavaDoc m = newEnum(contentName);
45                 List typeName = this.refTypeName(); //qualified container name
46
typeName.add(contentName); //qualified type name
47
if(m==null) {
48                   m = new EnumType(typeName,labels);
49                 }
50                 enumList.add( m );
51             }
52         }
53         structFieldNameLists = (List[]) structList.toArray(new List[0]);
54         enumTypes = (Object JavaDoc[]) enumList.toArray(new Object JavaDoc[0]);
55         
56     }
57    
58     public RefStruct refCreateStruct(javax.jmi.reflect.RefObject refObject, java.util.List JavaDoc list) {
59         String JavaDoc name = (String JavaDoc) refObject.refGetValue("name");
60         return refCreateStruct(name, list);
61     }
62     
63     public RefStruct refCreateStruct(String JavaDoc type, java.util.List JavaDoc list) {
64         int index = structNames.indexOf(type);
65         if(index==-1) {
66             throw new RuntimeException JavaDoc("Illegal struct type: " +type);
67         }
68         if(structQNames==null) {
69           List containerQName = refTypeName();
70           structQNames = new List[structNames.size()];
71           for(int i=0; i<structQNames.length; i++) {
72             List l = new Vector(containerQName);
73             l.add(structNames.get(i));
74             structQNames[i] = l;
75           }
76         }
77         
78         RefStructImpl s = newStruct(type);
79         s.typeNames = structQNames[index];
80         s.fieldNames = structFieldNameLists[index];
81         s.setValues(list);
82         return s;
83     }
84     
85     public RefEnum refGetEnum(javax.jmi.reflect.RefObject refObject, String JavaDoc val) {
86         String JavaDoc type = (String JavaDoc) refObject.refGetValue("name");
87         return refGetEnum(type, val);
88     }
89     
90     public RefEnum refGetEnum(String JavaDoc type, String JavaDoc val) {
91         int index = enumTypeNames.indexOf(type);
92         if(index==-1) {
93             throw new RuntimeException JavaDoc("Invalide Enum type: " +type);
94         }
95         if(enumTypes[index] instanceof EnumType)
96             return ((EnumType)enumTypes[index]).getEnum(val);
97         Class JavaDoc[] argType = { String JavaDoc.class };
98         Object JavaDoc[] args = { val };
99         try {
100           return (RefEnum) ((Class JavaDoc)enumTypes[index])
101             .getMethod("forName", argType)
102             .invoke(
103             null, args
104           );
105         } catch(Exception JavaDoc e) {
106           System.out.println("error at " +enumTypes[index]);
107           throw new RuntimeException JavaDoc(e);
108         }
109     }
110     
111     /////////////// to be over writen ////////////////
112
public Class JavaDoc newEnum(String JavaDoc name) {
113       return null;
114     }
115     
116     public RefStructImpl newStruct(String JavaDoc name) {
117       return new RefStructImpl();
118     }
119
120 }
121
Popular Tags