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 34 import org.omg.DynamicAny.*; 35 import org.omg.CORBA.ORB ; 36 import org.omg.CORBA.TypeCode ; 37 import org.omg.CORBA.Any ; 38 import org.omg.CORBA.TCKind ; 39 40 48 public class IdlArray extends IdlNode implements IdlWritable { 49 protected int size; 50 protected transient TypeCode content; 51 52 protected IdlArray() { 53 isLeaf = false; 54 isMutable = false; 55 setUserObject(this); 56 } 57 58 protected IdlArray(Any any) { 59 this(); 60 setNode(any); 61 } 62 63 protected IdlArray(TypeCode tc) { 64 this(); 65 setNode(tc); 66 } 67 68 protected void setNode(TypeCode tc) { 69 try { 70 this.tc = tc; 71 if (tc.kind().value() == TCKind._tk_array) { 72 id = ""; 73 try { 74 id = tc.id(); 75 type = "array " + id; 76 } catch (Exception ex) { 77 type = "array"; 79 } 80 size = tc.length(); 81 value = "" + size; 82 content = tc.content_type(); 83 removeAllChildren(); 84 for (int i = 0; i < tc.length(); i++) { 85 IdlNode m = create(content); 86 m.field = "[" + i + "]"; 87 add(m); 88 } 89 } else if (tc.kind().value() == TCKind._tk_alias) { 90 id = tc.id(); 91 type = id; 92 TypeCode stc = tc.content_type(); 93 while (stc.kind().value() == TCKind._tk_alias) { 94 stc = stc.content_type(); 95 } 96 if (stc.kind().value() != TCKind._tk_array) { 97 throw new RuntimeException ("Array type expected"); 98 } 99 size = stc.length(); 100 value = "" + size; 101 content = stc.content_type(); 102 removeAllChildren(); 103 for (int i = 0; i < stc.length(); i++) { 104 IdlNode m = create(content); 105 m.field = "[" + i + "]"; 106 add(m); 107 } 108 } else { 109 throw new RuntimeException ("Union type expected"); 110 } 111 } catch (Exception e) { 112 e.printStackTrace(); 113 } 114 } 115 116 protected void setNode(Any any) { 117 try { 118 setNode(any.type()); 119 org.omg.CORBA.portable.InputStream in = any.create_input_stream(); 120 removeAllChildren(); 121 for (int i = 0; i < size; i++) { 122 Any memberAny = orb.create_any(); 123 memberAny.read_value(in, content); 124 IdlNode m = create(memberAny); 125 m.field = "[" + i + "]"; 126 add(m); 127 } 128 } catch (Exception e) { 129 e.printStackTrace(); 130 } 131 } 132 133 138 public int getSize() { 139 return size; 140 } 141 142 147 public Any toAny() { 148 try { 149 Any any = orb.create_any(); 150 any.type(tc); 151 org.omg.CORBA.portable.OutputStream out = any.create_output_stream(); 152 for (int i = 0; i < size; i++) { 153 ((IdlNode)getChildAt(i)).toAny().write_value(out); 154 } 155 any.read_value(out.create_input_stream(), tc); 156 return any; 157 } catch (Exception e) { 158 e.printStackTrace(); 159 } 160 return null; 161 } 162 163 168 public void write(org.omg.CORBA.portable.OutputStream os) { 169 try { 170 for (int i = 0; i < size; i++) { 171 ((IdlNode)getChildAt(i)).write(os); 172 } 173 } catch (Exception e) { 174 e.printStackTrace(); 175 } 176 } 177 178 183 public void read(org.omg.CORBA.portable.InputStream is) { 184 try { 185 for (int i = 0; i < size; i++) { 186 ((IdlNode)getChildAt(i)).read(is); 187 } 188 } catch (Exception e) { 189 e.printStackTrace(); 190 } 191 } 192 193 195 207 public IdlArray(String xml) { 208 this(XmlNode.getNode(xml)); 209 } 210 211 IdlArray(Node n) { 212 this(null, n); 213 } 214 215 IdlArray(org.omg.CORBA.TypeCode t, Node n) { 216 this(); 217 try { 218 id = XmlNode.getId(n); 219 if (!id.equals("")) { 220 t = XmlNode.type(id); 221 } 222 setNode(t); 223 org.omg.CORBA.TypeCode content = tc.content_type(); 224 225 if (XmlNode.getLength(n) != size) { 226 throw new RuntimeException ("Array length does not match actual length " + size + "."); 227 } 228 Node [] nodes = XmlNode.childElements(n); 229 230 if (nodes.length != size) { 231 throw new RuntimeException ("Number of array elements does not match " + size + "."); 232 } 233 removeAllChildren(); 234 for (int i = 0; i < size; i++) { 235 IdlNode m = XmlNode.getIdlNode(content, nodes[i]); 236 m.setField("[" + i + "]"); 237 add(m); 238 } 239 value = "" + size; 240 } catch (Exception e) { 241 throw new RuntimeException (e.toString()); 242 } 243 } 244 245 250 public void write(IdlWriter w) { 251 write(this, w); 252 } 253 254 public static void write(IdlArray n, IdlWriter w) { 255 w.write_start_array(n.getId(), n.getSize()); 256 for(int i = 0; i < n.getChildCount(); i++) { 257 XmlNode.write((IdlNode)n.getChildAt(i), w); 258 } 259 w.write_end_array(); 260 } 261 } | Popular Tags |