| 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 javax.xml.parsers.DocumentBuilder ; 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.parsers.FactoryConfigurationError ; 30 import javax.xml.parsers.ParserConfigurationException ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.SAXParseException ; 33 import org.xml.sax.InputSource ; 34 import org.w3c.dom.*; 35 import javax.swing.tree.*; 36 37 import java.util.*; 38 import java.io.*; 39 import java.lang.reflect.*; 40 41 public class XmlNode { 42 static org.objectweb.ccm.IDL3.Repository repository; 43 static org.objectweb.openccm.ir3.api.ComponentRepository repositoryRef; 44 static Hashtable repositoryCache = new Hashtable(); 45 static org.omg.CORBA.ORB orb = null; 46 47 static { 48 try { 49 if ( orb == null ){ 50 orb = org.objectweb.openccm.corba.TheORB.getORB(); 51 } 52 repositoryRef = 54 org.objectweb.openccm.ir3.api.ComponentRepositoryHelper.narrow( 55 org.objectweb.openccm.corba.TheInterfaceRepository.getRepository()); 56 57 repository = new org.objectweb.ccm.IDL3.Repository(repositoryRef.as_IDL2_repository()); 59 60 } catch (Throwable ex) { 61 ex.printStackTrace(); 62 } 63 } 64 65 public static void setOrb(org.omg.CORBA.ORB ob) { 66 orb = ob; 67 } 68 69 static Node getNode(String xml) { 70 Node node = null; 71 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 72 factory.setValidating(true); 73 try { 74 DocumentBuilder builder = factory.newDocumentBuilder(); 75 InputSource is = new InputSource (new StringReader(xml)); 76 is.setSystemId("file://" + System.getProperty("dsc.home") + "/etc/"); 77 node = builder.parse(is); 78 node.normalize(); 79 } catch (SAXParseException spe) { 80 throw new RuntimeException ("Xml parsing error, line " + spe.getLineNumber() + ", uri " + spe.getSystemId() + " " + spe.getMessage()); 81 } catch (Exception x) { 82 x.printStackTrace(); 83 } 84 return firstChildElement(node); 85 } 86 87 static String getId(Node n) { 88 return getAttribute(n, "id"); 89 } 90 91 static String getName(Node n) { 92 return getAttribute(n, "name"); 93 } 94 95 static int getLength(Node n) { 96 String a = getAttribute(n, "length"); 97 try { 98 return Integer.parseInt(a); 99 } catch (Exception e) { 100 throw new RuntimeException ("unable to read length attribute"); 101 } 102 } 103 104 static String getValue(Node n) { 105 return getAttribute(n, "value"); 106 } 107 108 113 static String [] getOperations(String id) { 114 String [] operations = new String [0]; 115 if (repositoryRef != null) { 116 try { 117 org.omg.CORBA.Contained c = lookup(id); 118 HashSet v = new HashSet(); 119 addOperations(v, id); 120 operations = new String [v.size()]; 121 v.toArray(operations); 122 } catch (Exception ex) { 123 System.err.println(ex.toString()); 124 } 125 } 126 return operations; 127 } 128 129 static void addOperations(HashSet v, String id) { 130 if (repositoryRef != null) { 131 try { 132 org.omg.CORBA.Contained c = lookup(id); 133 org.omg.CORBA.InterfaceDef interfaceDef = org.omg.CORBA.InterfaceDefHelper.narrow(c); 134 org.omg.CORBA.InterfaceDefPackage.FullInterfaceDescription desc = interfaceDef.describe_interface(); 135 for (int i = 0; i < desc.operations.length; i++) { 136 v.add(desc.operations[i].name); 137 } 138 for (int i = 0; i < desc.attributes.length; i++) { 139 v.add("_get_" + desc.attributes[i].name); 140 if (desc.attributes[i].mode == org.omg.CORBA.AttributeMode.ATTR_NORMAL) { 141 v.add("_set_" + desc.attributes[i].name); 142 } 143 } 144 for(int i = 0; i < desc.base_interfaces.length; i++) { 145 addOperations(v, desc.base_interfaces[i]); 146 } 147 } catch (Exception ex) { 148 System.err.println(ex.toString()); 149 } 150 } 151 } 152 153 static void addOperations(Hashtable v, String id) { 154 if (repositoryRef != null) { 155 try { 156 org.omg.CORBA.Contained c = lookup(id); 157 org.omg.CORBA.InterfaceDef interfaceDef = org.omg.CORBA.InterfaceDefHelper.narrow(c); 158 org.omg.CORBA.InterfaceDefPackage.FullInterfaceDescription desc = interfaceDef.describe_interface(); 159 for (int i = 0; i < desc.operations.length; i++) { 160 v.put(desc.operations[i].name, desc.operations[i]); 161 } 162 for (int i = 0; i < desc.attributes.length; i++) { 163 v.put("_get_" + desc.attributes[i].name, desc.attributes[i]); 164 if (desc.attributes[i].mode == org.omg.CORBA.AttributeMode.ATTR_NORMAL) { 165 v.put("_set_" + desc.attributes[i].name, desc.attributes[i]); 166 } 167 } 168 for(int i = 0; i < desc.base_interfaces.length; i++) { 169 addOperations(v, desc.base_interfaces[i]); 170 } 171 } catch (Exception ex) { 172 System.err.println(ex.toString()); 173 } 174 } 175 } 176 177 182 static String [] getParameterNames(String id, String name) { 183 String [] names = new String [0]; 184 if (repositoryRef != null) { 185 try { 186 Hashtable t = new Hashtable(); 187 addOperations(t, id); 188 Object desc = t.get(name); 189 if (desc == null) { 190 throw new RuntimeException ("no operation " + name + " found in interface " + id); 191 } 192 if (desc instanceof org.omg.CORBA.OperationDescription ) { 193 org.omg.CORBA.OperationDescription opDesc = (org.omg.CORBA.OperationDescription )desc; 194 names = new String [opDesc.parameters.length + 1]; 195 for (int j = 0; j < opDesc.parameters.length; j++) { 196 names[j] = opDesc.parameters[j].name; 197 } 198 if (opDesc.mode == org.omg.CORBA.OperationMode.OP_ONEWAY) { 199 names[names.length - 1] = "oneway"; 200 } else { 201 names[names.length - 1] = "return"; 202 } 203 } 204 if (desc instanceof org.omg.CORBA.AttributeDescription ) { 205 if (name.startsWith("_set_")) { 206 names = new String [] { 207 "val", 208 "return" 209 }; 210 } else { 211 names = new String [] { 212 "return" 213 }; 214 } 215 } 216 } catch (Exception ex) { 217 System.err.println(ex.toString()); 218 } 219 } 220 return names; 221 } 222 223 229 static String [] getParameterDirections(String id, String name) { 230 String [] directions = new String [0]; 231 if (repositoryRef != null) { 232 try { 233 Hashtable t = new Hashtable(); 234 addOperations(t, id); 235 Object desc = t.get(name); 236 if (desc == null) { 237 throw new RuntimeException ("no operation " + name + " found in interface " + id); 238 } 239 if (desc instanceof org.omg.CORBA.OperationDescription ) { 240 org.omg.CORBA.OperationDescription opDesc = (org.omg.CORBA.OperationDescription )desc; 241 directions = new String [opDesc.parameters.length + 1]; 242 for (int j = 0; j < opDesc.parameters.length; j++) { 243 directions[j] = parameterMode(opDesc.parameters[j].mode); 244 } 245 if (opDesc.mode == org.omg.CORBA.OperationMode.OP_ONEWAY) { 246 directions[directions.length - 1] = "oneway"; 247 } else { 248 directions[directions.length - 1] = "return"; 249 } 250 } 251 if (desc instanceof org.omg.CORBA.AttributeDescription ) { 252 if (name.startsWith("_set_")) { 253 directions = new String [] { 254 "in", 255 "return" 256 }; 257 } else { 258 directions = new String [] { 259 "return" 260 }; 261 } 262 } 263 } catch (Exception ex) { 264 System.err.println(ex.toString()); 265 } 266 } 267 268 return directions; 269 } 270 271 private static String parameterMode(org.omg.CORBA.ParameterMode m) { 272 if (m == org.omg.CORBA.ParameterMode.PARAM_IN) { 273 return "in"; 274 } 275 if (m == org.omg.CORBA.ParameterMode.PARAM_OUT) { 276 return "out"; 277 } 278 if (m == org.omg.CORBA.ParameterMode.PARAM_INOUT) { 279 return "inout"; 280 } 281 return ""; 282 } 283 284 291 static String [] getParameterTypes(String id, String name) { 292 String [] types = new String [0]; 293 if (repositoryRef != null) { 294 try { 295 Hashtable t = new Hashtable(); 296 addOperations(t, id); 297 Object desc = t.get(name); 298 if (desc == null) { 299 throw new RuntimeException ("no operation " + name + " found in interface " + id); 300 } 301 if (desc instanceof org.omg.CORBA.OperationDescription ) { 302 org.omg.CORBA.OperationDescription opDesc = (org.omg.CORBA.OperationDescription )desc; 303 types = new String [opDesc.parameters.length + 1]; 304 for (int j = 0; j < opDesc.parameters.length; j++) { 305 types[j] = parameterType(opDesc.parameters[j].type); 306 } 307 if (opDesc.mode == org.omg.CORBA.OperationMode.OP_ONEWAY) { 308 types[types.length - 1] = ""; 309 } else { 310 types[types.length - 1] = parameterType(opDesc.result); 311 } 312 } 313 if (desc instanceof org.omg.CORBA.AttributeDescription ) { 314 org.omg.CORBA.AttributeDescription attDesc = (org.omg.CORBA.AttributeDescription )desc; 315 if (name.startsWith("_set_")) { 316 types = new String [] { 317 parameterType(attDesc.type), 318 "void" 319 }; 320 } else { 321 types = new String [] { 322 parameterType(attDesc.type) 323 }; 324 } 325 } 326 } catch (Exception ex) { 327 System.err.println(ex.toString()); 328 } 329 } 330 return types; 331 } 332 333 static String [] getParameterExceptions(String id, String name) { 334 String [] exceptions = new String [0]; 335 if (repositoryRef != null) { 336 try { 337 Hashtable t = new Hashtable(); 338 addOperations(t, id); 339 Object desc = t.get(name); 340 if (desc == null) { 341 throw new RuntimeException ("no operation " + name + " found in interface " + id); 342 } 343 if (desc instanceof org.omg.CORBA.OperationDescription ) { 344 org.omg.CORBA.OperationDescription opDesc = (org.omg.CORBA.OperationDescription )desc; 345 exceptions = new String [opDesc.exceptions.length]; 346 for (int j = 0; j < opDesc.exceptions.length; j++) { 347 exceptions[j] = opDesc.exceptions[j].id; 348 } 349 } 350 } catch (Exception ex) { 351 System.err.println(ex.toString()); 352 } 353 } 354 355 return exceptions; 356 } 357 358 private static String parameterType(org.omg.CORBA.TypeCode tc) { 359 try { 360 switch (tc.kind().value()) { 361 case org.omg.CORBA.TCKind._tk_any: { 362 return "any"; 363 } 364 case org.omg.CORBA.TCKind._tk_octet: { 365 return "octet"; 366 } 367 case org.omg.CORBA.TCKind._tk_short: { 368 return "short"; 369 } 370 case org.omg.CORBA.TCKind._tk_ushort: { 371 return "ushort"; 372 } 373 case org.omg.CORBA.TCKind._tk_long: { 374 return "long"; 375 } 376 case org.omg.CORBA.TCKind._tk_ulong: { 377 return "ulong"; 378 } 379 case org.omg.CORBA.TCKind._tk_longlong: { 380 return "longlong"; 381 } 382 case org.omg.CORBA.TCKind._tk_ulonglong: { 383 return "ulonglong"; 384 } 385 case org.omg.CORBA.TCKind._tk_float: { 386 return "float"; 387 } 388 case org.omg.CORBA.TCKind._tk_double: { 389 return "double"; 390 } 391 case org.omg.CORBA.TCKind._tk_longdouble: { 392 return "longdouble"; 393 } 394 case org.omg.CORBA.TCKind._tk_boolean: { 395 return "boolean"; 396 } 397 case org.omg.CORBA.TCKind._tk_char: { 398 return "char"; 399 } 400 case org.omg.CORBA.TCKind._tk_wchar: { 401 return "wchar"; 402 } 403 case org.omg.CORBA.TCKind._tk_string: { 404 return "string"; 405 } 406 case org.omg.CORBA.TCKind._tk_wstring: { 407 return "wstring"; 408 } 409 case org.omg.CORBA.TCKind._tk_void: { 410 return "void"; 411 } 412 case org.omg.CORBA.TCKind._tk_null: { 413 return "null"; 414 } 415 default: { 416 return tc.id(); 417 } 418 } 419 } catch (Exception e) { 420 e.printStackTrace(); 421 } 422 return null; 423 } 424 425 429 static String idToType(String id) { 430 String javaName = ""; 431 if (id.indexOf(":") > 0) { 433 id = id.substring(id.indexOf(":") + 1, id.lastIndexOf(":")); 434 } 435 if (id.indexOf(".") > 0) { 436 String pragma = id.substring(0, id.indexOf("/")); 438 String [] el = splitName(pragma, "."); 439 String prefix = el[el.length - 1]; 440 for (int i = el.length - 2; i >= 0; i--) { 441 prefix += "." + el[i]; 442 } 443 javaName = prefix + id.substring(id.indexOf("/")).replace('/', '.'); 444 } else { 445 javaName = id.replace('/', '.'); 446 } 447 if (javaName.equals("org.omg.CORBA.Object")) { 448 javaName = "object"; 449 } 450 return javaName; 451 } 452 453 static String [] splitName(String name, String separator) { 454 if (name == null) { 455 return new String [0]; 456 } 457 StringTokenizer st = new StringTokenizer(name, separator); 458 Vector v = new Vector(); 459 if (name.startsWith(separator)) { 460 v.addElement(""); 461 } 462 while (st.hasMoreTokens()) { 463 v.addElement(st.nextToken()); 464 } 465 String [] s = new String [v.size()]; 466 v.copyInto(s); 467 return s; 468 } 469 470 static String getText(Node domNode) { 471 String text = ""; 472 NodeList list = domNode.getChildNodes(); 473 int items = list.getLength(); 474 for (int i = 0; i < items; i++) { 475 if (list.item(i).getNodeType() == Node.TEXT_NODE) { 476 text = list.item(i).getNodeValue().trim(); 477 if (!text.equals("")) { 478 return list.item(i).getNodeValue().trim(); 479 } 480 } 481 } 482 return text; 483 } 484 485 static String getAttribute(Node domNode, String name) { 486 NamedNodeMap map = domNode.getAttributes(); 487 Node attr = map.getNamedItem(name); 488 if (attr != null) { 489 return attr.getNodeValue(); 490 } else { 491 return ""; 492 } 493 } 494 495 static Node nextElement(Node n) { 496 if (n != null) { 497 n = n.getNextSibling(); 498 while (n != null && n.getNodeType() != Node.ELEMENT_NODE) { 499 n = n.getNextSibling(); 500 } 501 } 502 return n; 503 } 504 505 static Node firstElement(Node n) { 506 while (n != null && n.getNodeType() != Node.ELEMENT_NODE) { 507 System.out.println("" + n.getNodeType() + " " + n.getNodeName()); 508 n = n.getNextSibling(); 509 } 510 return n; 511 } 512 513 static Node firstChildElement(Node n) { 514 NodeList list = n.getChildNodes(); 515 int items = list.getLength(); 516 for (int i = 0; i < items; i++) { 517 if (list.item(i).getNodeType() == Node.ELEMENT_NODE) { 518 return list.item(i); 519 } 520 } 521 return null; 522 } 523 524 static Node[] childElements(Node n) { 525 Vector v = new Vector(); 526 NodeList list = n.getChildNodes(); 527 int items = list.getLength(); 528 for (int i = 0; i < items; i++) { 529 if (list.item(i).getNodeType() == Node.ELEMENT_NODE) { 530 v.add(list.item(i)); 531 } 532 } 533 Node[] nn = new Node[v.size()]; 534 v.toArray(nn); 535 return nn; 536 } 537 538 static org.omg.CORBA.Contained lookup(String id) { 539 org.omg.CORBA.Contained c = (org.omg.CORBA.Contained )repositoryCache.get(id); 540 if (c == null) { 541 c = repositoryRef.lookup_id(id); 543 if (c == null) { 544 javax.swing.JOptionPane.showMessageDialog(null, id + " not found in InterfaceRepository.\nUse ir3_feed to populate the InterfaceRepository with the proper idl files"); 545 return null; 546 } 547 repositoryCache.put(id, c); 548 } 549 return c; 550 } 551 552 static public org.omg.CORBA.TypeCode type(String id) { 553 org.omg.CORBA.TypeCode tc = null; 554 String type = idToType(id); 555 Integer index = (Integer )table.get(type.toLowerCase()); 556 557 if (index == null) { 558 try { 559 org.omg.CORBA.Contained c = lookup(id); 560 if (c == null) { 561 throw new RuntimeException (id + " not found in InterfaceRepository.\nUse ir3_feed to populate the InterfaceRepository with the proper idl files"); 562 } 563 try { 564 org.omg.CORBA.IDLType idlType = org.omg.CORBA.IDLTypeHelper.narrow(c); 565 tc = idlType.type(); 566 return tc; 567 } catch (Exception e1) { 568 } 569 org.omg.CORBA.ExceptionDef idlType = org.omg.CORBA.ExceptionDefHelper.narrow(c); 570 tc = idlType.type(); 571 return tc; 572 } catch (Exception ex) { 573 ex.printStackTrace(); 574 } 575 } else { 576 switch (index.intValue()) { 577 case 0: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_boolean); 578 case 1: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_octet); 579 case 2: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_short); 580 case 3: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_ushort); 581 case 4: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_long); 582 case 5: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_ulong); 583 case 6: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_longlong); 584 case 7: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_ulonglong); 585 case 8: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_float); 586 case 9: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_double); 587 case 10: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_longdouble); 588 case 11: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_char); 589 case 12: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_wchar); 590 case 13: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_string); 591 case 14: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_wstring); 592 case 15: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_objref); 593 case 17: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_fixed); 594 case 18: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_any); 595 case 24: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_void); 596 } 597 } 598 return tc; 600 } 601 602 609 static IdlNode getIdlNode(String id) { 610 if (id == null) { 611 throw new RuntimeException ("invalid id"); 612 } 613 614 String type = idToType(id); 615 Integer index = (Integer )table.get(type.toLowerCase()); 616 617 if (index == null) { 618 try { 620 return IdlNode.create(XmlNode.type(id)); 621 } catch (Exception e) { 622 e.printStackTrace(); 623 return null; 624 } 625 } 626 627 switch (index.intValue()) { 628 case 0: return new IdlBoolean(); 629 case 1: return new IdlOctet(); 630 case 2: return new IdlShort(); 631 case 3: return new IdlUshort(); 632 case 4: return new IdlLong(); 633 case 5: return new IdlUlong(); 634 case 6: return new |