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.swing.tree.*; 28 29 import java.util.*; 30 import java.io.*; 31 import java.lang.reflect.*; 32 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 78 79 public abstract class IdlNode extends DefaultMutableTreeNode { 80 protected static transient ORB orb = null; 83 protected transient TypeCode tc; 85 protected boolean isLeaf = true; 87 protected boolean isMutable = true; 89 protected String field = ""; 93 protected String type = ""; 95 protected String id = ""; 97 protected String value = ""; 99 100 static { 101 try { 102 if ( orb == null ) 103 { 104 orb = org.objectweb.openccm.corba.TheORB.getORB(); 105 } 106 } 107 catch (Error e) { 108 } 109 } 110 111 protected IdlNode() { 112 } 113 114 public static void setOrb(ORB ob) { 115 orb = ob; 116 } 117 118 123 protected void setNode(TypeCode tc) { 124 this.tc = tc; 125 if (tc.kind().value() == TCKind._tk_alias) { 126 try { 127 type = tc.id(); 128 id = tc.id(); 129 } catch (Exception ex) { 130 } 131 } 132 } 133 134 141 public static IdlNode create(TypeCode tc) { 142 try { 143 switch (tc.kind().value()) { 144 case TCKind._tk_struct: { 145 return new IdlStruct(tc); 146 } 147 case TCKind._tk_except: { 148 return new IdlException(tc); 149 } 150 case TCKind._tk_union: { 151 return new IdlUnion(tc); 152 } 153 case TCKind._tk_array: { 154 return new IdlArray(tc); 155 } 156 case TCKind._tk_sequence: { 157 return new IdlSequence(tc); 158 } 159 case TCKind._tk_enum: { 160 return new IdlEnum(tc); 161 } 162 case TCKind._tk_any: { 163 return new IdlAny(tc); 164 } 165 case TCKind._tk_octet: { 166 return new IdlOctet(tc); 167 } 168 case TCKind._tk_short: { 169 return new IdlShort(tc); 170 } 171 case TCKind._tk_ushort: { 172 return new IdlUshort(tc); 173 } 174 case TCKind._tk_long: { 175 return new IdlLong(tc); 176 } 177 case TCKind._tk_ulong: { 178 return new IdlUlong(tc); 179 } 180 case TCKind._tk_longlong: { 181 return new IdlLonglong(tc); 182 } 183 case TCKind._tk_ulonglong: { 184 return new IdlUlonglong(tc); 185 } 186 case TCKind._tk_float: { 187 return new IdlFloat(tc); 188 } 189 case TCKind._tk_double: { 190 return new IdlDouble(tc); 191 } 192 case TCKind._tk_longdouble: { 193 return new IdlLongdouble(tc); 194 } 195 case TCKind._tk_boolean: { 196 return new IdlBoolean(tc); 197 } 198 case TCKind._tk_char: { 199 return new IdlChar(tc); 200 } 201 case TCKind._tk_wchar: { 202 return new IdlWchar(tc); 203 } 204 case TCKind._tk_objref: { 205 return new IdlObject(tc); 206 } 207 case TCKind._tk_value: { 208 return new IdlValue(tc); 209 } 210 case TCKind._tk_string: { 211 return new IdlString(tc); 212 } 213 case TCKind._tk_wstring: { 214 return new IdlWstring(tc); 215 } 216 case TCKind._tk_fixed: { 217 return new IdlFixed(tc); 218 } 219 case TCKind._tk_alias: { 220 return create(tc.content_type(), tc); 221 } 222 default: { 223 throw new RuntimeException ("Unexpected type: " + tc.kind().value()); 224 } 225 } 226 } catch (Exception e) { 227 e.printStackTrace(); 228 } 229 return null; 230 } 231 232 240 private static IdlNode create(TypeCode alias, TypeCode tc) { 241 try { 242 switch (alias.kind().value()) { 243 case TCKind._tk_struct: { 244 return new IdlStruct(tc); 245 } 246 case TCKind._tk_except: { 247 return new IdlException(tc); 248 } 249 case TCKind._tk_union: { 250 return new IdlUnion(tc); 251 } 252 case TCKind._tk_array: { 253 return new IdlArray(tc); 254 } 255 case TCKind._tk_sequence: { 256 return new IdlSequence(tc); 257 } 258 case TCKind._tk_enum: { 259 return new IdlEnum(tc); 260 } 261 case TCKind._tk_any: { 262 return new IdlAny(tc); 263 } 264 case TCKind._tk_octet: { 265 return new IdlOctet(tc); 266 } 267 case TCKind._tk_short: { 268 return new IdlShort(tc); 269 } 270 case TCKind._tk_ushort: { 271 return new IdlUshort(tc); 272 } 273 case TCKind._tk_long: { 274 return new IdlLong(tc); 275 } 276 case TCKind._tk_ulong: { 277 return new IdlUlong(tc); 278 } 279 case TCKind._tk_longlong: { 280 return new IdlLonglong(tc); 281 } 282 case TCKind._tk_ulonglong: { 283 return new IdlUlonglong(tc); 284 } 285 case TCKind._tk_float: { 286 return new IdlFloat(tc); 287 } 288 case TCKind._tk_double: { 289 return new IdlDouble(tc); 290 } 291 case TCKind._tk_longdouble: { 292 return new IdlLongdouble(tc); 293 } 294 case TCKind._tk_boolean: { 295 return new IdlBoolean(tc); 296 } 297 case TCKind._tk_char: { 298 return new IdlChar(tc); 299 } 300 case TCKind._tk_wchar: { 301 return new IdlWchar(tc); 302 } 303 case TCKind._tk_objref: { 304 return new IdlObject(tc); 305 } 306 case TCKind._tk_value: { 307 return new IdlValue(tc); 308 } 309 case TCKind._tk_string: { 310 return new IdlString(tc); 311 } 312 case TCKind._tk_wstring: { 313 return new IdlWstring(tc); 314 } 315 case TCKind._tk_fixed: { 316 return new IdlFixed(tc); 317 } 318 case TCKind._tk_alias: { 319 return create(alias.content_type(), tc); 320 } 321 default: { 322 throw new RuntimeException ("Unexpected type: " + tc.kind().value()); 323 } 324 } 325 } catch (Exception e) { 326 e.printStackTrace(); 327 } 328 return null; 329 } 330 331 338 public static IdlNode create(Any any) { 339 return create(any.type(), any); 340 } 341 342 347 public void write(IdlWriter w) { 348 } 349 350 355 public void write(org.omg.CORBA.portable.OutputStream os) { 356 } 357 358 363 public void read(org.omg.CORBA.portable.InputStream is) { 364 } 365 366 374 private static IdlNode create(TypeCode tc, Any any) { 375 try { 376 switch (tc.kind().value()) { 377 case TCKind._tk_struct: { 378 return new IdlStruct(any); 379 } 380 case TCKind._tk_except: { 381 return new IdlException(any); 382 } 383 case TCKind._tk_union: { 384 return new IdlUnion(any); 385 } 386 case TCKind._tk_any: { 387 return new IdlAny(any); 388 } 389 case TCKind._tk_sequence: { 390 return new IdlSequence(any); 391 } 392 case TCKind._tk_array: { 393 return new IdlArray(any); 394 } 395 case TCKind._tk_enum: { 396 return new IdlEnum(any); 397 } 398 case TCKind._tk_short: { 399 return new IdlShort(any); 400 } 401 case TCKind._tk_ushort: { 402 return new IdlUshort(any); 403 } 404 case TCKind._tk_char: { 405 return new IdlChar(any); 406 } 407 case TCKind._tk_wchar: { 408 return new IdlWchar(any); 409 } 410 case TCKind._tk_string: { 411 return new IdlString(any); 412 } 413 case TCKind._tk_wstring: { 414 return new IdlWstring(any); 415 } 416 case TCKind._tk_long: { 417 return new IdlLong(any); 418 } 419 case TCKind._tk_ulong: { 420 return new IdlUlong(any); 421 } 422 case TCKind._tk_longlong: { 423 return new IdlLonglong(any); 424 } 425 case TCKind._tk_ulonglong: { 426 return new IdlUlonglong(any); 427 } 428 case TCKind._tk_octet: { 429 return new IdlOctet(any); 430 } 431 case TCKind._tk_longdouble: { 432 return new IdlLongdouble(any); 433 } 434 case TCKind._tk_objref: { 435 return new IdlObject(any); 436 } 437 case TCKind._tk_value: { 438 return new IdlValue(any); 439 } 440 case TCKind._tk_float: { 441 return new IdlFloat(any); 442 } 443 case TCKind._tk_double: { 444 return new IdlDouble(any); 445 } 446 case TCKind._tk_boolean: { 447 return new IdlBoolean(any); 448 } 449 case TCKind._tk_fixed: { 450 return new IdlFixed(any); 451 } 452 case TCKind._tk_alias: { 453 return create(tc.content_type(), any); 454 } 455 default: { 456 throw new RuntimeException ("Unexpected type: " + any.type().kind()); 457 } 458 } 459 } catch (Exception e) { 460 e.printStackTrace(); 461 } 462 return null; 463 } 464 465 470 public String toString() { 471 if (field.equals("")) { 472 return (type + " " + value).trim(); 473 } else { 474 return (field + " " + type + " " + value).trim(); 475 } 476 } 477 478 492 public IdlNode getNode(String path) { 493 if (path == null || path.equals("")) { 494 return this; 495 } 496 String current = path; 497 int idx = path.indexOf("."); 498 if (idx > 0) { 499 current = path.substring(0, idx); 501 path = path.substring(idx + 1); 502 } else { 503 path = null; 505 } 506 int cc = getChildCount(); 507 for (int i = 0; i < cc; i++) { 508 IdlNode n = (IdlNode)getChildAt(i); 509 if (n instanceof IdlParameter) { 510 n = ((IdlParameter)n).getParameter(); 511 } 512 String f = (n.field.replace('[', ' ').replace(']', ' ')).trim(); 514 if (f.equals(current)) { 515 if (path != null) { 516 return n.getNode(path); 518 } else { 519 return n; 521 } 522 } 523 } 524 throw new RuntimeException ("No such field '" + current + "' in node type: " + type); 525 } 526 527 537 public String printFields(boolean values) { 538 StringBuffer sb = new StringBuffer (); 539 540 for(Enumeration e = depthFirstEnumeration(); e.hasMoreElements();) { 541 IdlNode n = (IdlNode)e.nextElement(); 542 if (!(n instanceof IdlParameter) && n != this) { 543 TreeNode[] nn = n.getPath(); 544 String path = ""; 545 String dot = ""; 546 for(int i = getLevel() + 1; i < nn.length; i++) { 547 if (!(nn[i] instanceof IdlParameter)) { 548 path += dot + (((IdlNode)nn[i]).field.replace('[', ' ').replace(']', ' ')).trim(); 549 dot = "."; 550 } 551 } 552 if (values) { 553 sb.append(path + ":" + n.getValue() + "\n"); 554 } else { 555 sb.append(path + "\n"); 556 } 557 } 558 } 559 560 return sb.toString(); 561 } 562 563 570 public String printValues() { 571 StringBuffer sb = new StringBuffer (); 572 573 for(Enumeration e =depthFirstEnumeration(); e.hasMoreElements();) { 574 IdlNode n = (IdlNode)e.nextElement(); 575 if (!(n instanceof IdlParameter) && n != this) { 576 sb.append(n.getValue() + "\n"); 577 } 578 } 579 580 return sb.toString(); 581 } 582 583 public void setField(String f) { 584 field = f; 585 } 586 587 594 public void replace(IdlNode n) { 595 if (n.getType().equals(getType()) && n.getId().equals(getId())) { 597 n.field = field; 598 IdlNode p = (IdlNode)getParent(); 599 if (p != null) { 600 int idx = p.getIndex(this); 601 if (idx >= 0) { 602 p.insert(n, idx); 603 return; 604 } 605 throw new RuntimeException ("No such child node."); 606 } else { 607 throw new RuntimeException ("Can't replace root in IdlNode."); 608 } 609 } else { 610 throw new RuntimeException ("Unmatching types for IdlNode replacement. Expected: " + getType()); 611 } 612 } 613 614 619 public void setValue(String v) { 620 value = v; 621 } 622 623 628 public String getType() { 629 return type; 630 } 631 632 637 public String getValue() { 638 return value; 639 } 640 641 646 public String getId() { 647 return id; 648 } 649 650 655 public TypeCode getTypeCode() { 656 return tc; 657 } 658 659 660 665 public Any toAny() { 666 return toDynAny().to_any(); 667 } 668 669 public org.omg.DynamicAny.DynAny toDynAny() { 670 throw new RuntimeException ("Illegal operation on this nodetype: " + this.getClass().getName()); 671 } 672 673 678 public boolean getAllowsChildren() { 679 return !isLeaf; 681 } 682 683 688 public boolean isLeaf() { 689 return isLeaf; 690 } 691 692 697 public boolean isMutable() { 698 return isMutable; 699 } 700 701 706 public Object clone() { 707 try { 708 IdlNode t = (IdlNode)super.clone(); 709 t.parent = null; 710 t.children = null; 711 t.userObject = t; 712 return t; 714 } catch (Exception e) { 715 e.printStackTrace(); 716 } 717 return null; 718 } 719 720 725 public IdlNode copy() { 726 return (IdlNode)clone(); 727 } 728 729 735 public boolean canPaste(IdlNode t) { 736 return false; 737 } 738 739 744 void paste(IdlNode t) { 745 type = t.type; 747 field = t.field; 748 children = null; 749 allowsChildren = t.allowsChildren; 750 userObject = this; 751 752 } 754 755 static protected String [] splitName(String name, String separator) { 756 if (name == null) { 757 return new String [0]; 758 } 759 StringTokenizer st = new StringTokenizer(name, separator); 760 Vector v = new Vector(); 761 if (name.startsWith(separator)) { 762 v.addElement(""); 763 } 764 while (st.hasMoreTokens()) { 765 v.addElement(st.nextToken()); 766 } 767 String [] s = new String [v.size()]; 768 v.copyInto(s); 769 return s; 770 } 771 } | Popular Tags |