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.CORBA.ORB ; 35 import org.omg.CORBA.TypeCode ; 36 import org.omg.CORBA.Any ; 37 import org.omg.CORBA.TCKind ; 38 39 49 public class IdlAny extends IdlNode implements IdlWritable { 50 protected IdlAny() { 51 setUserObject(this); 52 type = "any"; 53 isLeaf = false; 54 tc = orb.get_primitive_tc(TCKind.tk_any); 55 } 56 57 protected IdlAny(Any any) { 58 this(); 59 setNode(any); 60 } 61 62 protected IdlAny(TypeCode tc) { 63 this(); 64 setNode(tc); 65 } 66 67 protected void setNode(Any any) { 68 try { 69 setNode(any.type()); 70 removeAllChildren(); 71 Any content = any.extract_any(); 72 if (content.type().kind().value() != TCKind._tk_null) { 74 add(IdlNode.create(content)); 75 } 76 } catch (Exception e) { 77 e.printStackTrace(); 78 } 79 } 80 81 86 public Any toAny() { 87 try { 88 Any any = orb.create_any(); 89 any.type(tc); 90 if (getChildCount() == 1) { 91 IdlNode n = (IdlNode)getChildAt(0); 92 any.insert_any(n.toAny()); 93 } else { 94 Any content = orb.create_any(); 95 content.type(orb.get_primitive_tc(TCKind.tk_null)); 96 any.insert_any(content); 97 } 98 return any; 99 } catch (Exception e) { 100 e.printStackTrace(); 101 } 102 return null; 103 } 104 105 113 public void setContent(String id) { 114 try { 115 removeAllChildren(); 116 TypeCode t = type(id); 117 add(IdlNode.create(t)); 118 } catch (Exception e) { 119 e.printStackTrace(); 120 } 121 } 122 123 protected static org.omg.CORBA.TypeCode type(String id) { 124 String type = idToType(id); 125 Integer index = (Integer )table.get(type.toLowerCase()); 126 127 if (index == null) { 128 try { 130 Class hc = Class.forName(type + "Helper"); 131 Method hm = hc.getMethod("type", null); 132 return (org.omg.CORBA.TypeCode )hm.invoke(null, null); 133 } catch (Exception e) { 134 e.printStackTrace(); 135 return null; 136 } 137 } else { 138 switch (index.intValue()) { 140 case 0: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_boolean); 141 case 1: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_octet); 142 case 2: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_short); 143 case 3: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_ushort); 144 case 4: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_long); 145 case 5: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_ulong); 146 case 6: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_longlong); 147 case 7: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_ulonglong); 148 case 8: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_float); 149 case 9: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_double); 150 case 10: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_longdouble); 151 case 11: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_char); 152 case 12: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_wchar); 153 case 13: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_string); 154 case 14: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_wstring); 155 case 15: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_objref); 156 case 17: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_fixed); 157 case 18: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_any); 158 default: return null; 159 } 160 } 161 } 162 163 168 public void write(org.omg.CORBA.portable.OutputStream os) { 169 if (getChildCount() == 1) { 170 IdlNode n = (IdlNode)getChildAt(0); 171 n.write(os); 172 } 173 } 174 175 180 public void read(org.omg.CORBA.portable.InputStream is) { 181 if (getChildCount() == 1) { 182 IdlNode n = (IdlNode)getChildAt(0); 183 n.read(is); 184 } 185 } 186 187 191 static private String idToType(String id) { 192 String javaName = ""; 193 if (id.indexOf(":") > 0) { 195 id = id.substring(id.indexOf(":") + 1, id.lastIndexOf(":")); 196 } 197 if (id.indexOf(".") > 0) { 198 String pragma = id.substring(0, id.indexOf("/")); 200 String [] el = splitName(pragma, "."); 201 String prefix = el[el.length - 1]; 202 for (int i = el.length - 2; i >= 0; i--) { 203 prefix += "." + el[i]; 204 } 205 javaName = prefix + id.substring(id.indexOf("/")).replace('/', '.'); 206 } else { 207 javaName = id.replace('/', '.'); 208 } 209 return javaName; 210 } 211 212 static private final java.util.Hashtable table = new java.util.Hashtable (); 213 static { 214 table.put("boolean", new Integer (0)); 215 table.put("octet", new Integer (1)); 216 table.put("short", new Integer (2)); 217 table.put("ushort", new Integer (3)); 218 table.put("long", new Integer (4)); 219 table.put("ulong", new Integer (5)); 220 table.put("longlong", new Integer (6)); 221 table.put("ulonglong", new Integer (7)); 222 table.put("float", new Integer (8)); 223 table.put("double", new Integer (9)); 224 table.put("longdouble", new Integer (10)); 225 table.put("char", new Integer (11)); 226 table.put("wchar", new Integer (12)); 227 table.put("string", new Integer (13)); 228 table.put("wstring", new Integer (14)); 229 table.put("object", new Integer (15)); 230 table.put("interface", new Integer (16)); 231 table.put("fixed", new Integer (17)); 232 table.put("any", new Integer (18)); 233 table.put("enum", new Integer (19)); 234 table.put("struct", new Integer (20)); 235 table.put("union", new Integer (21)); 236 table.put("sequence", new Integer (22)); 237 table.put("array", new Integer (23)); 238 } 239 240 242 253 public IdlAny(String xml) { 254 this(XmlNode.getNode(xml)); 255 } 256 257 IdlAny(Node node) { 258 this(); 259 260 if (node == null || !node.getNodeName().toUpperCase().equals("ANY")) { 261 throw new RuntimeException ("any expected"); 262 } 263 264 Node child = XmlNode.firstChildElement(node); 265 266 if (child != null) { 267 add(XmlNode.getIdlNode(child)); 268 } 269 } 270 271 276 public void write(IdlWriter w) { 277 write(this, w); 278 } 279 280 public static void write(IdlAny n, IdlWriter w) { 281 w.write_start_any(); 282 if (n.getChildCount() == 1) { 283 XmlNode.write((IdlNode)n.getChildAt(0), w); 284 } 285 w.write_end_any(); 286 } 287 } | Popular Tags |