1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package org.coach.idltree; 26 27 import org.w3c.dom.Node ; 28 import java.util.*; 29 import java.io.*; 30 import java.lang.reflect.*; 31 import org.omg.CORBA.ORB ; 32 import org.omg.CORBA.TypeCode ; 33 import org.omg.CORBA.Any ; 34 import org.omg.CORBA.TCKind ; 35 36 43 public class IdlStruct extends IdlNode implements IdlWritable { 44 protected String [] members; 45 protected transient TypeCode [] types; 46 47 protected IdlStruct() { 48 isLeaf = false; 49 isMutable = false; 50 setUserObject(this); 51 } 52 53 protected IdlStruct(Any any) { 54 this(); 55 setNode(any); 56 } 57 58 protected IdlStruct(TypeCode tc) { 59 this(); 60 setNode(tc); 61 } 62 63 protected void setNode(TypeCode tc) { 64 try { 65 this.tc = tc; 66 if (tc.kind().value() == TCKind._tk_struct) { 67 id = tc.id(); 68 type = "struct " + id; 69 members = new String [tc.member_count()]; 70 types = new TypeCode [tc.member_count()]; 71 removeAllChildren(); 72 for (int i = 0; i < tc.member_count(); i++) { 73 types[i] = tc.member_type(i); 74 members[i] = tc.member_name(i); 75 IdlNode m = IdlNode.create(types[i]); 76 m.field = members[i]; 77 add(m); 78 } 79 } else if (tc.kind().value() == TCKind._tk_alias) { 80 id = tc.id(); 81 type = id; 82 TypeCode stc = tc.content_type(); 83 while (stc.kind().value() == TCKind._tk_alias) { 84 stc = stc.content_type(); 85 } 86 if (stc.kind().value() != TCKind._tk_struct) { 87 throw new RuntimeException ("Struct type expected"); 88 } 89 members = new String [tc.member_count()]; 90 types = new TypeCode [tc.member_count()]; 91 removeAllChildren(); 92 for (int i = 0; i < stc.member_count(); i++) { 93 types[i] = stc.member_type(i); 94 members[i] = stc.member_name(i); 95 IdlNode m = IdlNode.create(types[i]); 96 m.field = members[i]; 97 add(m); 98 } 99 } else { 100 throw new RuntimeException ("Struct type expected"); 101 } 102 } catch (Exception e) { 103 e.printStackTrace(); 104 } 105 } 106 107 protected void setNode(Any any) { 108 try { 109 setNode(any.type()); 110 org.omg.CORBA.portable.InputStream in = any.create_input_stream(); 111 removeAllChildren(); 112 for (int i = 0; i < members.length; i++) { 113 Any memberAny = orb.create_any(); 114 memberAny.read_value(in, types[i]); 115 IdlNode m = create(memberAny); 116 m.field = members[i]; 117 add(m); 118 } 119 } catch (Throwable e) { 120 e.printStackTrace(); 121 } 122 } 123 124 129 public String [] getMembers() { 130 return members; 131 } 132 133 138 public Any toAny() { 139 try { 140 Any any = orb.create_any(); 141 any.type(tc); 142 org.omg.CORBA.portable.OutputStream out = any.create_output_stream(); 143 for (int i = 0; i < members.length; i++) { 144 Any memberAny = ((IdlNode)getChildAt(i)).toAny(); 145 memberAny.write_value(out); 146 } 147 any.read_value(out.create_input_stream(), tc); 148 return any; 149 } catch (Throwable e) { 150 e.printStackTrace(); 151 } 152 return null; 153 } 154 155 160 public void write(org.omg.CORBA.portable.OutputStream os) { 161 try { 162 for (int i = 0; i < members.length; i++) { 163 ((IdlNode)getChildAt(i)).write(os); 164 } 165 } catch (Exception e) { 166 e.printStackTrace(); 167 } 168 } 169 170 175 public void read(org.omg.CORBA.portable.InputStream is) { 176 try { 177 for (int i = 0; i < members.length; i++) { 178 ((IdlNode)getChildAt(i)).read(is); 179 } 180 } catch (Exception e) { 181 e.printStackTrace(); 182 } 183 } 184 185 187 204 public IdlStruct(String xml) { 205 this(XmlNode.getNode(xml)); 206 } 207 208 IdlStruct(Node n) { 209 this(); 210 try { 211 id = XmlNode.getId(n); 212 setNode(XmlNode.type(id)); 213 214 Node [] nodes = XmlNode.childElements(n); 215 String [] xmlmembers = new String [nodes.length]; 216 217 removeAllChildren(); 218 for (int i = 0; i < nodes.length; i++) { 219 if (nodes[i].getNodeName().toUpperCase().equals("MEMBER")) { 220 xmlmembers[i] = XmlNode.getName(nodes[i]); 221 IdlNode m = XmlNode.getIdlNode(tc.member_type(i), XmlNode.firstChildElement(nodes[i])); 222 m.setField(xmlmembers[i]); 223 add(m); 224 } else { 225 throw new RuntimeException ("Member expected."); 226 } 227 } 228 if (members.length >= xmlmembers.length) { 229 for (int i = 0; i < members.length; i++) { 230 if (i >= xmlmembers.length || !members[i].equals(xmlmembers[i])) { 231 throw new RuntimeException ("exception member " + members[i] + " is missing for " + id); 232 } 233 } 234 } else { 235 for (int i = 0; i < xmlmembers.length; i++) { 236 if (i >= members.length || !members[i].equals(xmlmembers[i])) { 237 throw new RuntimeException (xmlmembers[i] + " is not a valid exception member in " + id); 238 } 239 } 240 } 241 } catch (Exception e) { 242 e.printStackTrace(); 243 } 244 } 245 246 251 public void write(IdlWriter w) { 252 write(this, w); 253 } 254 255 public static void write(IdlStruct n, IdlWriter w) { 256 w.write_start_struct(n.getId()); 257 String [] mbrs = n.getMembers(); 258 for(int i = 0; i < n.getChildCount(); i++) { 259 w.write_start_member(mbrs[i]); 260 XmlNode.write((IdlNode)n.getChildAt(i), w); 261 w.write_end_member(); 262 } 263 w.write_end_struct(); 264 } 265 } | Popular Tags |