KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > xmi > PropertyTypeManager


1 package org.objectweb.modfact.jmi.xmi;
2
3 import java.util.*;
4
5 import javax.jmi.reflect.*;
6
7 import org.objectweb.modfact.jmi.reflect.ReflectHelper;
8
9
10 public class PropertyTypeManager {
11
12     Hashtable typeCach = new Hashtable();
13     
14     public PropertyTypeManager() {
15     }
16     
17     
18
19     public TypeInfo findTypeFor(RefObject metaObjectOwner, String JavaDoc unqPropName ) {
20         RefObject propMeta = (RefObject) ReflectHelper.lookupElementExtended(metaObjectOwner, unqPropName);
21         return findTypeFor(propMeta);
22     }
23     
24
25     // propMetaObject describes the property
26
// propMetaObject.name = property name
27
// propMetaObject.type = property type meta object
28
// propMetaObject.type.name = type name
29
private TypeInfo findTypeFor(RefObject propMetaObject) {
30        return findTypeFor(propMetaObject, false);
31     }
32     
33
34     private TypeInfo findTypeFor(RefObject propMetaObject, boolean isStructField) {
35         // find in Cache
36
TypeInfo typeInfo = (TypeInfo)typeCach.get(propMetaObject);
37         if(typeInfo!=null) return typeInfo;
38         // find type info in M2 (to load M1 XMI data)
39
// or in M3 (to load M2 XMI data)
40
typeInfo = new TypeInfo();
41         if(!isStructField) {
42           typeInfo.isMultiValued = (
43             ((Integer JavaDoc)((RefStruct)propMetaObject.refGetValue("multiplicity")).refGetValue("upper")).intValue()!=1
44           );
45         }
46         RefObject type= (RefObject)propMetaObject.refGetValue("type");
47         typeInfo.name = (String JavaDoc) type.refGetValue("name");
48         typeInfo.qname = (String JavaDoc) ReflectHelper.dotQName(ReflectHelper.getQualifiedName(type));
49         String JavaDoc typeKind = (String JavaDoc) type.refMetaObject().refGetValue("name");
50         if(typeKind.equals("PrimitiveType")) {
51           typeInfo.isPrimitive = true;
52         } else if(typeKind.equals("EnumerationType")) {
53           typeInfo.isEnum = true;
54           typeInfo.enumUnprefix = ReflectHelper.findTagValue(type,"org.omg.xmi.enumerationUnprefix");
55           
56         } else if(typeKind.equals("StructureType")) {
57           typeInfo.isStruct = true;
58           List fieldTypes = new Vector();
59           List fieldNames = new Vector();
60           Iterator it = ((List)type.refGetValue("contents")).iterator();
61           while(it.hasNext()) {
62             RefObject content = (RefObject)it.next();
63             if(content.refMetaObject().refGetValue("name").equals("StructureField")) {
64               fieldTypes.add(findTypeFor(content, true));
65               fieldNames.add(content.refGetValue("name"));
66             }
67           }
68           typeInfo.structFieldTypes = (TypeInfo[]) fieldTypes.toArray(new TypeInfo[0]);
69           typeInfo.structFieldNames = (String JavaDoc[]) fieldNames.toArray(new String JavaDoc[0]);
70         }
71         // put in cache
72
typeCach.put(propMetaObject, typeInfo);
73         return typeInfo;
74     }
75        
76
77
78    
79
80     public static class TypeInfo {
81         public String JavaDoc name;
82         public String JavaDoc qname;
83         public boolean isMultiValued = false;
84         public boolean isEnum = false;
85         public String JavaDoc enumUnprefix = null; //correspond to tag "xmi.enumerationUnprefix"
86
public boolean isStruct = false;
87         public boolean isPrimitive = false;
88         public TypeInfo[] structFieldTypes = null;
89         public String JavaDoc[] structFieldNames = null;
90     }
91 }
92
Popular Tags