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 IdlValue extends IdlNode implements IdlWritable { 44 protected String [] members; 45 protected transient TypeCode [] types; 46 47 protected IdlValue() { 48 isLeaf = false; 49 isMutable = false; 50 setUserObject(this); 51 } 52 53 protected IdlValue(Any any) { 54 this(); 55 setNode(any); 56 } 57 58 protected IdlValue(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_value) { 67 id = tc.id(); 68 type = "value " + 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_value) { 87 throw new RuntimeException ("Value 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 ("Value 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 200 public IdlValue(String xml) { 201 this(XmlNode.getNode(xml)); 202 } 203 204 IdlValue(Node n) { 205 this(); 206 try { 207 id = XmlNode.getId(n); 208 setNode(XmlNode.type(id)); 209 210 Node [] nodes = XmlNode.childElements(n); 211 String [] xmlmembers = new String [nodes.length]; 212 213 removeAllChildren(); 214 for (int i = 0; i < nodes.length; i++) { 215 if (nodes[i].getNodeName().toUpperCase().equals("MEMBER")) { 216 xmlmembers[i] = XmlNode.getName(nodes[i]); 217 IdlNode m = XmlNode.getIdlNode(tc.member_type(i), XmlNode.firstChildElement(nodes[i])); 218 m.setField(xmlmembers[i]); 219 add(m); 220 } else { 221 throw new RuntimeException ("Member expected."); 222 } 223 } 224 if (members.length >= xmlmembers.length) { 225 for (int i = 0; i < members.length; i++) { 226 if (i >= xmlmembers.length || !members[i].equals(xmlmembers[i])) { 227 throw new RuntimeException ("exception member " + members[i] + " is missing for " + id); 228 } 229 } 230 } else { 231 for (int i = 0; i < xmlmembers.length; i++) { 232 if (i >= members.length || !members[i].equals(xmlmembers[i])) { 233 throw new RuntimeException (xmlmembers[i] + " is not a valid exception member in " + id); 234 } 235 } 236 } 237 } catch (Exception e) { 238 e.printStackTrace(); 239 } 240 } 241 242 247 public void write(IdlWriter w) { 248 write(this, w); 249 } 250 251 public static void write(IdlValue n, IdlWriter w) { 252 w.write_start_value(n.getId()); 253 String [] mbrs = n.getMembers(); 254 for(int i = 0; i < n.getChildCount(); i++) { 255 w.write_start_member(mbrs[i]); 256 XmlNode.write((IdlNode)n.getChildAt(i), w); 257 w.write_end_member(); 258 } 259 w.write_end_value(); 260 } 261 } | Popular Tags |