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 javax.swing.tree.*; 29 30 import java.util.*; 31 import java.io.*; 32 import java.lang.reflect.*; 33 import org.omg.CORBA.ORB ; 34 import org.omg.CORBA.TypeCode ; 35 import org.omg.CORBA.Any ; 36 import org.omg.CORBA.TCKind ; 37 38 47 public class IdlSequence extends IdlNode implements IdlWritable { 48 protected boolean isBounded; 49 protected int size; 50 protected transient TypeCode content; 51 52 protected IdlSequence() { 53 isLeaf = false; 54 setUserObject(this); 55 } 56 57 protected IdlSequence(Any any) { 58 this(); 59 setNode(any); 60 } 61 62 protected IdlSequence(TypeCode tc) { 63 this(); 64 setNode(tc); 65 } 66 67 protected void setNode(TypeCode tc) { 68 try { 69 this.tc = tc; 70 if (tc.kind().value() == TCKind._tk_sequence) { 71 id = ""; 72 try { 73 id = tc.id(); 74 type = "sequence " + id; 75 } catch (Exception ex) { 76 type = "sequence"; 78 } 79 if (tc.length() == 0) { 80 isBounded = false; 81 } else { 82 isBounded = true; 83 size = tc.length(); 84 } 85 content = tc.content_type(); 86 } else if (tc.kind().value() == TCKind._tk_alias) { 87 id = tc.id(); 88 type = id; 89 TypeCode stc = tc.content_type(); 90 while (stc.kind().value() == TCKind._tk_alias) { 91 stc = stc.content_type(); 92 } 93 if (stc.kind().value() != TCKind._tk_sequence) { 94 throw new RuntimeException ("Sequence type expected"); 95 } 96 if (stc.length() == 0) { 97 isBounded = false; 98 } else { 99 isBounded = true; 100 size = stc.length(); 101 } 102 content = stc.content_type(); 103 } else { 104 throw new RuntimeException ("Sequence type expected"); 105 } 106 removeAllChildren(); 107 if (size == 0) { 108 isBounded = false; 109 size = 1; 110 value = "1"; 111 IdlNode m = create(content); 113 m.field = "[0]"; 114 add(m); 115 } else { 116 isBounded = true; 117 value = "" + size; 118 for (int i = 0; i < size; i++) { 119 IdlNode m = create(content); 120 m.field = "[" + i + "]"; 121 add(m); 122 } 123 } 124 } catch (Exception e) { 125 e.printStackTrace(); 126 } 127 } 128 129 protected void setNode(Any any) { 130 try { 131 setNode(any.type()); 132 org.omg.CORBA.portable.InputStream in = any.create_input_stream(); 133 if (isBounded) { 134 int receivedSize = in.read_ulong(); 135 if (receivedSize > size) { 136 throw new RuntimeException ("Unexpected sequence size " + receivedSize + ". " + id + " has a maximum size of " + size + "."); 137 } 138 } else { 139 size = in.read_ulong(); 140 } 141 removeAllChildren(); 142 for (int i = 0; i < size; i++) { 143 Any memberAny = orb.create_any(); 144 memberAny.read_value(in, content); 145 IdlNode m = create(memberAny); 146 m.field = "[" + i + "]"; 147 add(m); 148 } 149 value = "" + size; 150 } catch (Exception e) { 151 e.printStackTrace(); 152 } 153 } 154 155 160 public boolean isBounded() { 161 return isBounded; 162 } 163 164 169 public int getSize() { 170 return size; 171 } 172 173 178 public void setValue(String v) { 179 try { 180 setSize(Integer.parseInt(v)); 181 } catch (Exception e) { 182 e.printStackTrace(); 183 } 184 } 185 186 191 public void setSize(int s) { 192 try { 193 if (isBounded && s > tc.length()) { 194 throw new RuntimeException ("Unexpected sequence size " + s + ". " + id + " has a maximum size of " + tc.length() + "."); 195 } 196 removeAllChildren(); 197 size = s; 198 value = "" + size; 199 for (int i = 0; i < size; i++) { 200 IdlNode m = create(content); 201 m.field = "[" + i + "]"; 202 add(m); 203 } 204 } catch (Exception e) { 205 e.printStackTrace(); 206 } 207 } 208 209 214 public Any toAny() { 215 try { 216 Any any = orb.create_any(); 217 any.type(tc); 218 org.omg.CORBA.portable.OutputStream out = any.create_output_stream(); 219 out.write_ulong(size); 220 for (int i = 0; i < size; i++) { 221 ((IdlNode)getChildAt(i)).toAny().write_value(out); 222 } 223 any.read_value(out.create_input_stream(), tc); 224 return any; 225 } catch (Exception e) { 226 e.printStackTrace(); 227 } 228 return null; 229 } 230 231 236 public void write(org.omg.CORBA.portable.OutputStream os) { 237 try { 238 for (int i = 0; i < size; i++) { 239 ((IdlNode)getChildAt(i)).write(os); 240 } 241 } catch (Exception e) { 242 e.printStackTrace(); 243 } 244 } 245 246 251 public void read(org.omg.CORBA.portable.InputStream is) { 252 try { 253 for (int i = 0; i < size; i++) { 254 ((IdlNode)getChildAt(i)).read(is); 255 } 256 } catch (Exception e) { 257 e.printStackTrace(); 258 } 259 } 260 261 263 public IdlSequence(String xml) { 264 this(XmlNode.getNode(xml)); 265 } 266 267 IdlSequence(Node n) { 268 this(null, n); 269 } 270 271 IdlSequence(org.omg.CORBA.TypeCode t, Node n) { 272 this(); 273 try { 274 id = XmlNode.getId(n); 275 if (!id.equals("")) { 276 t = XmlNode.type(id); 277 } 278 setNode(t); 279 280 if (tc.kind().value() == TCKind._tk_alias) { 281 org.omg.CORBA.TypeCode content = tc.content_type().content_type(); 282 size = tc.content_type().length(); 283 } else { 284 org.omg.CORBA.TypeCode content = tc.content_type(); 285 size = tc.length(); 286 } 287 int length = XmlNode.getLength(n); 288 289 if (size != 0 && size < length) { 290 throw new RuntimeException ("This sequence length has a maximum of " + size + " elements."); 291 } 292 293 Node [] nodes = XmlNode.childElements(n); 294 295 if (nodes.length != length) { 296 throw new RuntimeException ("Number of sequence elements does not match " + length + "."); 297 } 298 removeAllChildren(); 299 for (int i = 0; i < length; i++) { 300 IdlNode m = XmlNode.getIdlNode(content, nodes[i]); 301 m.setField(field + "[" + i + "]"); 302 add(m); 303 } 304 value = "" + length; 305 } catch (Exception e) { 306 e.printStackTrace(); 307 } 309 } 310 311 316 public void write(IdlWriter w) { 317 write(this, w); 318 } 319 320 public static void write(IdlSequence n, IdlWriter w) { 321 w.write_start_sequence(n.getId(), n.getSize()); 322 for(int i = 0; i < n.getChildCount(); i++) { 323 XmlNode.write((IdlNode)n.getChildAt(i), w); 324 } 325 w.write_end_sequence(); 326 } 327 } | Popular Tags |