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 32 import org.omg.CORBA.ORB ; 33 import org.omg.CORBA.TypeCode ; 34 import org.omg.CORBA.Any ; 35 import org.omg.CORBA.TCKind ; 36 37 44 public class IdlException extends IdlNode implements IdlWritable { 45 protected String [] members; 46 protected transient TypeCode [] types; 47 48 protected IdlException() { 49 isLeaf = false; 50 isMutable = false; 51 setUserObject(this); 52 } 53 54 public IdlException(Any any) { 55 this(); 56 setNode(any); 57 } 58 59 protected IdlException(org.omg.CORBA.TypeCode tc) { 60 this(); 61 setNode(tc); 62 } 63 64 protected void setNode(TypeCode tc) { 65 try { 66 this.tc = tc; 67 if (tc.kind().value() == TCKind._tk_except || tc.kind().value() == TCKind._tk_struct) { 68 id = tc.id(); 69 type = "exception " + id; 70 removeAllChildren(); 71 types = new TypeCode [tc.member_count()]; 72 members = new String [tc.member_count()]; 73 for (int i = 0; i < tc.member_count(); i++) { 74 types[i] = tc.member_type(i); 75 members[i] = tc.member_name(i); 76 IdlNode m = IdlNode.create(types[i]); 77 m.field = members[i]; 78 add(m); 79 } 80 } else if (tc.kind().value() == TCKind._tk_alias) { 81 id = tc.id(); 82 type = id; 83 TypeCode stc = tc.content_type(); 84 while (stc.kind().value() == TCKind._tk_alias) { 85 stc = stc.content_type(); 86 } 87 if (stc.kind().value() != TCKind._tk_except && stc.kind().value() != TCKind._tk_struct) { 88 throw new RuntimeException ("Exception type expected"); 89 } 90 91 removeAllChildren(); 92 types = new TypeCode [stc.member_count()]; 93 members = new String [stc.member_count()]; 94 for (int i = 0; i < stc.member_count(); i++) { 95 types[i] = stc.member_type(i); 96 members[i] = stc.member_name(i); 97 IdlNode m = IdlNode.create(types[i]); 98 m.field = members[i]; 99 add(m); 100 } 101 } else { 102 throw new RuntimeException ("Exception type expected"); 103 } 104 } catch (Exception e) { 105 e.printStackTrace(); 106 } 107 } 108 109 protected void setNode(Any any) { 110 try { 111 setNode(any.type()); 112 org.omg.CORBA.portable.InputStream in = any.create_input_stream(); 113 String xid = in.read_string(); 114 115 removeAllChildren(); 116 for (int i = 0; i < members.length; i++) { 117 Any memberAny = orb.create_any(); 118 memberAny.read_value(in, types[i]); 119 IdlNode m = create(memberAny); 120 m.field = members[i]; 121 add(m); 122 } 123 } catch (Throwable e) { 124 e.printStackTrace(); 125 } 126 } 127 128 133 public String [] getMembers() { 134 return members; 135 } 136 137 142 public Any toAny() { 143 try { 144 Any any = orb.create_any(); 145 any.type(tc); 146 org.omg.CORBA.portable.OutputStream out = any.create_output_stream(); 147 out.write_string(id); 148 for (int i = 0; i < members.length; i++) { 149 ((IdlNode)getChildAt(i)).toAny().write_value(out); 150 } 151 any.read_value(out.create_input_stream(), tc); 152 return any; 153 } catch (Throwable e) { 154 e.printStackTrace(); 155 } 156 return null; 157 } 158 159 164 public void write(org.omg.CORBA.portable.OutputStream os) { 165 try { 166 for (int i = 0; i < members.length; i++) { 167 ((IdlNode)getChildAt(i)).write(os); 168 } 169 } catch (Exception e) { 170 e.printStackTrace(); 171 } 172 } 173 174 179 public void read(org.omg.CORBA.portable.InputStream is) { 180 try { 181 for (int i = 0; i < members.length; i++) { 182 ((IdlNode)getChildAt(i)).read(is); 183 } 184 } catch (Exception e) { 185 e.printStackTrace(); 186 } 187 } 188 189 191 204 public IdlException(String xml) { 205 this(XmlNode.getNode(xml)); 206 } 207 208 IdlException(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 for (int i = 0; i < nodes.length; i++) { 218 if (nodes[i].getNodeName().toUpperCase().equals("MEMBER")) { 219 xmlmembers[i] = XmlNode.getName(nodes[i]); 220 IdlNode m = XmlNode.getIdlNode(tc.member_type(i), XmlNode.firstChildElement(nodes[i])); 221 m.setField(xmlmembers[i]); 222 add(m); 223 } else { 224 throw new RuntimeException ("Member expected."); 225 } 226 } 227 if (members.length >= xmlmembers.length) { 228 for (int i = 0; i < members.length; i++) { 229 if (i >= xmlmembers.length || !members[i].equals(xmlmembers[i])) { 230 throw new RuntimeException ("exception member " + members[i] + " is missing for " + id); 231 } 232 } 233 } else { 234 for (int i = 0; i < xmlmembers.length; i++) { 235 if (i >= members.length || !members[i].equals(xmlmembers[i])) { 236 throw new RuntimeException (xmlmembers[i] + " is not a valid exception member in " + id); 237 } 238 } 239 } 240 } catch (Exception e) { 241 e.printStackTrace(); 242 } 243 } 244 245 250 public void write(IdlWriter w) { 251 write(this, w); 252 } 253 254 public static void write(IdlException n, IdlWriter w) { 255 w.write_start_exception(n.getId()); 256 String [] mbrs = n.getMembers(); 257 for(int i = 0; i < n.getChildCount(); i++) { 258 w.write_start_member(mbrs[i]); 259 XmlNode.write((IdlNode)n.getChildAt(i), w); 260 w.write_end_member(); 261 } 262 w.write_end_exception(); 263 } 264 } | Popular Tags |