incubator-yoko-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dmidd...@apache.org
Subject svn commit: r544470 - /incubator/yoko/trunk/bindings/src/main/java/org/apache/yoko/bindings/corba/utils/CorbaUtils.java
Date Tue, 05 Jun 2007 12:35:50 GMT
Author: dmiddlem
Date: Tue Jun  5 05:35:49 2007
New Revision: 544470

URL: http://svn.apache.org/viewvc?view=rev&rev=544470
Log:
Commit for YOKO-377:
* Adding support for generation of recursive struct and union typecodes.

Modified:
    incubator/yoko/trunk/bindings/src/main/java/org/apache/yoko/bindings/corba/utils/CorbaUtils.java

Modified: incubator/yoko/trunk/bindings/src/main/java/org/apache/yoko/bindings/corba/utils/CorbaUtils.java
URL: http://svn.apache.org/viewvc/incubator/yoko/trunk/bindings/src/main/java/org/apache/yoko/bindings/corba/utils/CorbaUtils.java?view=diff&rev=544470&r1=544469&r2=544470
==============================================================================
--- incubator/yoko/trunk/bindings/src/main/java/org/apache/yoko/bindings/corba/utils/CorbaUtils.java
(original)
+++ incubator/yoko/trunk/bindings/src/main/java/org/apache/yoko/bindings/corba/utils/CorbaUtils.java
Tue Jun  5 05:35:49 2007
@@ -26,6 +26,7 @@
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Stack;
 import java.util.logging.Logger;
 
 import javax.xml.namespace.QName;
@@ -85,13 +86,30 @@
     }
 
     public static TypeCode getTypeCode(ORB orb, QName type, CorbaTypeMap typeMap) {
-        return getTypeCode(orb, type, null, typeMap);
+        Stack<QName> seenTypes = new Stack<QName>();
+        return getTypeCode(orb, type, null, typeMap, seenTypes);
+    }
+
+    public static TypeCode getTypeCode(ORB orb, 
+                                       QName type, 
+                                       CorbaTypeMap typeMap,
+                                       Stack<QName> seenTypes) {
+        return getTypeCode(orb, type, null, typeMap, seenTypes);
     }
 
     public static TypeCode getTypeCode(ORB orb,
                                        QName type,
                                        CorbaType obj,
                                        CorbaTypeMap typeMap) {
+        Stack<QName> seenTypes = new Stack<QName>();
+        return getTypeCode(orb, type, obj, typeMap, seenTypes);
+    }
+
+    public static TypeCode getTypeCode(ORB orb, 
+                                       QName type, 
+                                       CorbaType obj, 
+                                       CorbaTypeMap typeMap,
+                                       Stack<QName> seenTypes) {
         TypeCode tc = null;
         // first see if it is a primitive
         tc = getPrimitiveTypeCode(orb, type);
@@ -113,7 +131,7 @@
                         throw new CorbaBindingException("Unable to locate object definition");
                     }
                 }
-                tc = getComplexTypeCode(orb, type, obj, typeMap);
+                tc = getComplexTypeCode(orb, type, obj, typeMap, seenTypes);
                 if (tc != null) {
                     typeMap.addTypeCode(type, tc);
                 }
@@ -144,20 +162,23 @@
         return null;
     }
 
-    public static TypeCode getComplexTypeCode(ORB orb, QName type, Object obj, CorbaTypeMap
typeMap) {
-
-        TypeCode tc = getAnonTypeCode(orb, type, obj, typeMap);
+    public static TypeCode getComplexTypeCode(ORB orb, 
+                                              QName type, 
+                                              Object obj, 
+                                              CorbaTypeMap typeMap, 
+                                              Stack<QName> seenTypes) {
+        TypeCode tc = getAnonTypeCode(orb, type, obj, typeMap, seenTypes);
         
         if (tc == null) {
             if (obj instanceof Alias) {
                 Alias aliasType = (Alias)obj;
                 tc = orb.create_alias_tc(aliasType.getRepositoryID(), 
                                          getTypeCodeName(aliasType.getName()), 
-                                         getTypeCode(orb, aliasType.getBasetype(), typeMap));
       
+                                         getTypeCode(orb, aliasType.getBasetype(), typeMap,
seenTypes)); 
             } else if (obj instanceof Array) {
                 Array arrayType = (Array)obj;
                 tc = orb.create_array_tc((int) arrayType.getBound(), 
-                                         getTypeCode(orb, arrayType.getElemtype(), typeMap));
+                                         getTypeCode(orb, arrayType.getElemtype(), typeMap,
seenTypes));
             } else if (obj instanceof Enum) {
                 Enum enumType = (Enum)obj;
                 String name = enumType.getName();
@@ -178,7 +199,8 @@
                 for (int i = 0; i < members.length; ++i) {
                     MemberType member = (MemberType) list.get(i);
                     members[i] = new StructMember(member.getName(), 
-                                                  getTypeCode(orb, member.getIdltype(), typeMap),
null);
+                                                  getTypeCode(orb, member.getIdltype(), typeMap,
seenTypes), 
+                                                  null);
                 }
                 String name = getTypeCodeName(exceptType.getName());
                 tc = orb.create_exception_tc(exceptType.getRepositoryID(), name, members);
@@ -197,40 +219,51 @@
             } else if (obj instanceof Sequence) {
                 Sequence seqType = (Sequence)obj;
                 tc = orb.create_sequence_tc((int) seqType.getBound(), 
-                                            getTypeCode(orb, seqType.getElemtype(), typeMap));
+                                            getTypeCode(orb, seqType.getElemtype(), typeMap,
seenTypes));
             } else if (obj instanceof Struct) {
                 Struct structType = (Struct)obj;
                 
                 // TODO: check to see if this is a recursive type.
-                List list = structType.getMember();
-                StructMember[] members = new StructMember[list.size()];
-                for (int i = 0; i < members.length; ++i) {
-                    MemberType member = (MemberType) list.get(i);
-                    members[i] = new StructMember(member.getName(), 
-                                                  getTypeCode(orb, member.getIdltype(), typeMap),
null);
+                if (seenTypes.contains(new QName(structType.getName()))) {
+                    tc = orb.create_recursive_tc(structType.getRepositoryID());
+                } else {
+                    seenTypes.push(new QName(structType.getName()));
+                    List list = structType.getMember();
+                    StructMember[] members = new StructMember[list.size()];
+                    for (int i = 0; i < members.length; ++i) {
+                        MemberType member = (MemberType) list.get(i);
+                        members[i] = new StructMember(member.getName(), 
+                                                 getTypeCode(orb, member.getIdltype(), typeMap,
seenTypes),
+                                                 null);
+                    }
+                    String name = getTypeCodeName(structType.getName());
+                    tc = orb.create_struct_tc(structType.getRepositoryID(), name, members);
+                    seenTypes.pop();
                 }
-                String name = getTypeCodeName(structType.getName());
-                tc = orb.create_struct_tc(structType.getRepositoryID(), name, members);
             } else if (obj instanceof Union) {
-                tc = getUnionTypeCode(orb, obj, typeMap);
+                tc = getUnionTypeCode(orb, obj, typeMap, seenTypes);
             }
         }
         return tc;
     }
     
-    private static TypeCode getAnonTypeCode(ORB orb, QName type, Object obj, CorbaTypeMap
typeMap) {
+    private static TypeCode getAnonTypeCode(ORB orb, 
+                                            QName type, 
+                                            Object obj, 
+                                            CorbaTypeMap typeMap,
+                                            Stack<QName> seenTypes) {
         TypeCode tc = null;
         if (obj instanceof Anonarray) {
             Anonarray anonArrayType = (Anonarray)obj;
             tc = orb.create_array_tc((int) anonArrayType.getBound(), 
-                                     getTypeCode(orb, anonArrayType.getElemtype(), typeMap));
+                                     getTypeCode(orb, anonArrayType.getElemtype(), typeMap,
seenTypes));
         } else if (obj instanceof Anonfixed) {
             Anonfixed anonFixedType = (Anonfixed) obj;
             tc = orb.create_fixed_tc((short) anonFixedType.getDigits(), (short) anonFixedType.getScale());
         } else if (obj instanceof Anonsequence) {
             Anonsequence anonSeqType = (Anonsequence)obj;
             tc = orb.create_sequence_tc((int) anonSeqType.getBound(), 
-                                        getTypeCode(orb, anonSeqType.getElemtype(), typeMap));
+                                        getTypeCode(orb, anonSeqType.getElemtype(), typeMap,
seenTypes));
         } else if (obj instanceof Anonstring) {
             Anonstring anonStringType = (Anonstring)obj;
             tc = orb.create_string_tc((int)anonStringType.getBound());
@@ -241,81 +274,93 @@
         return tc;
     }
 
-    public static TypeCode getUnionTypeCode(ORB orb, Object obj, CorbaTypeMap typeMap) {
+    public static TypeCode getUnionTypeCode(ORB orb, 
+                                            Object obj, 
+                                            CorbaTypeMap typeMap,
+                                            Stack<QName> seenTypes) {
         Union unionType = (Union)obj;
+
+        if (seenTypes.contains(new QName(unionType.getName()))) {
+            return orb.create_recursive_tc(unionType.getRepositoryID());
+        } else {
+            seenTypes.push(new QName(unionType.getName()));
         
-        TypeCode discTC = getTypeCode(orb, unionType.getDiscriminator(), typeMap);
-        Map<String, UnionMember> members = new LinkedHashMap<String, UnionMember>();
-        List<Unionbranch> branches = unionType.getUnionbranch();
-        for (Iterator<Unionbranch> branchIter = branches.iterator(); branchIter.hasNext();)
{
-            Unionbranch branch = branchIter.next();
-            List<CaseType> cases = branch.getCase();
-            for (Iterator<CaseType> caseIter = cases.iterator(); caseIter.hasNext();)
{
-                CaseType cs = caseIter.next();
-                if (!members.containsKey(cs.getLabel())) {
-                    UnionMember member = new UnionMember();
-                    member.name = branch.getName();
-                    member.type = getTypeCode(orb, branch.getIdltype(), typeMap);
-                    member.label = orb.create_any();
-                    // We need to insert the labels in a way that depends on the type of
the discriminator.  
-                    // According to the CORBA specification, the following types are permissable
as 
-                    // discriminator types:
-                    //    * signed & unsigned short
-                    //    * signed & unsigned long
-                    //    * signed & unsigned long long
-                    //    * char
-                    //    * boolean
-                    //    * enum
-                    switch (discTC.kind().value()) {
-                    case TCKind._tk_short:
-                        member.label.insert_short(Short.parseShort(cs.getLabel()));
-                        break;
-                    case TCKind._tk_ushort:
-                        member.label.insert_ushort(Short.parseShort(cs.getLabel()));
-                        break;
-                    case TCKind._tk_long:
-                        member.label.insert_long(Integer.parseInt(cs.getLabel()));
-                        break;
-                    case TCKind._tk_ulong:
-                        member.label.insert_ulong(Integer.parseInt(cs.getLabel()));
-                        break;
-                    case TCKind._tk_longlong:
-                        member.label.insert_longlong(Long.parseLong(cs.getLabel()));
-                        break;
-                    case TCKind._tk_ulonglong:
-                        member.label.insert_ulonglong(Long.parseLong(cs.getLabel()));
-                        break;
-                    case TCKind._tk_char:
-                        member.label.insert_char(cs.getLabel().charAt(0));
-                        break;
-                    case TCKind._tk_boolean:
-                        member.label.insert_boolean(Boolean.parseBoolean(cs.getLabel()));
-                        break;
-                    case TCKind._tk_enum:
-                        org.omg.CORBA.portable.OutputStream out = member.label.create_output_stream();
-                        Enum enumVal = (Enum)getCorbaType(unionType.getDiscriminator(), typeMap);
-                        List<Enumerator> enumerators = enumVal.getEnumerator();
-                        for (int i = 0; i < enumerators.size(); ++i) {
-                            Enumerator e = enumerators.get(i);
-                            if (e.getValue().equals(cs.getLabel())) {
-                                out.write_long(i);
+            TypeCode discTC = getTypeCode(orb, unionType.getDiscriminator(), typeMap, seenTypes);
+            Map<String, UnionMember> members = new LinkedHashMap<String, UnionMember>();
+            List<Unionbranch> branches = unionType.getUnionbranch();
+            for (Iterator<Unionbranch> branchIter = branches.iterator(); branchIter.hasNext();)
{
+                Unionbranch branch = branchIter.next();
+                List<CaseType> cases = branch.getCase();
+                for (Iterator<CaseType> caseIter = cases.iterator(); caseIter.hasNext();)
{
+                    CaseType cs = caseIter.next();
+                    if (!members.containsKey(cs.getLabel())) {
+                        UnionMember member = new UnionMember();
+                        member.name = branch.getName();
+                        member.type = getTypeCode(orb, branch.getIdltype(), typeMap, seenTypes);
+                        member.label = orb.create_any();
+                        // We need to insert the labels in a way that depends on the type
of the 
+                        // discriminator.  According to the CORBA specification, the following
types 
+                        // are permissable as discriminator types:
+                        //    * signed & unsigned short
+                        //    * signed & unsigned long
+                        //    * signed & unsigned long long
+                        //    * char
+                        //    * boolean
+                        //    * enum
+                        switch (discTC.kind().value()) {
+                        case TCKind._tk_short:
+                            member.label.insert_short(Short.parseShort(cs.getLabel()));
+                            break;
+                        case TCKind._tk_ushort:
+                            member.label.insert_ushort(Short.parseShort(cs.getLabel()));
+                            break;
+                        case TCKind._tk_long:
+                            member.label.insert_long(Integer.parseInt(cs.getLabel()));
+                            break;
+                        case TCKind._tk_ulong:
+                            member.label.insert_ulong(Integer.parseInt(cs.getLabel()));
+                            break;
+                        case TCKind._tk_longlong:
+                            member.label.insert_longlong(Long.parseLong(cs.getLabel()));
+                            break;
+                        case TCKind._tk_ulonglong:
+                            member.label.insert_ulonglong(Long.parseLong(cs.getLabel()));
+                            break;
+                        case TCKind._tk_char:
+                            member.label.insert_char(cs.getLabel().charAt(0));
+                            break;
+                        case TCKind._tk_boolean:
+                            member.label.insert_boolean(Boolean.parseBoolean(cs.getLabel()));
+                            break;
+                        case TCKind._tk_enum:
+                            org.omg.CORBA.portable.OutputStream out = 
+                                member.label.create_output_stream();
+                            Enum enumVal = (Enum)getCorbaType(unionType.getDiscriminator(),
typeMap);
+                            List<Enumerator> enumerators = enumVal.getEnumerator();
+                            for (int i = 0; i < enumerators.size(); ++i) {
+                                Enumerator e = enumerators.get(i);
+                                if (e.getValue().equals(cs.getLabel())) {
+                                    out.write_long(i);
+                                }
                             }
+                            member.label.read_value(out.create_input_stream(), discTC);
+                            break;
+                        default:
+                            throw new CorbaBindingException("Unsupported discriminator type");
                         }
-                        member.label.read_value(out.create_input_stream(), discTC);
-                        break;
-                    default:
-                        throw new CorbaBindingException("Unsupported discriminator type");
+                        // Yoko orb is strict on how the case labels are stored for each
member.  So we can't
+                        // simply insert the labels as strings 
+                        members.put(cs.getLabel(), member);
                     }
-                    // Yoko orb is strict on how the case labels are stored for each member.
 So we can't
-                    // simply insert the labels as strings 
-                    members.put(cs.getLabel(), member);
                 }
             }
+            seenTypes.pop();
+            return orb.create_union_tc(unionType.getRepositoryID(), 
+                                       getTypeCodeName(unionType.getName()), 
+                                       discTC, 
+                                       (UnionMember[])members.values().toArray(
+                                           new UnionMember[members.size()]));
         }
-        return orb.create_union_tc(unionType.getRepositoryID(), 
-                                   getTypeCodeName(unionType.getName()), 
-                                   discTC, 
-                                   (UnionMember[])members.values().toArray(new UnionMember[members.size()]));
     }
 
     public static String getTypeCodeName(String name) {



Mime
View raw message