1 20 21 package org.jacorb.idl; 22 23 import java.io.File ; 24 import java.io.FileWriter ; 25 import java.io.IOException ; 26 import java.io.PrintWriter ; 27 import java.util.*; 28 29 33 34 public class ValueDecl 35 extends Value 36 { 37 private MemberList stateMembers; 38 private List operations; 39 private List exports; 40 private List factories; 41 private ValueInheritanceSpec inheritanceSpec; 42 43 private boolean isCustomMarshalled = false; 45 private boolean hasStatefulBases = false; 46 private boolean hasBody = false; 47 48 49 50 public ValueDecl(int num) 51 { 52 super(num); 53 stateMembers = new MemberList(new_num()); 54 operations = new ArrayList(); 55 exports = new ArrayList(); 56 factories = new ArrayList(); 57 } 58 59 public void setValueElements(Definitions d) 60 { 61 hasBody = true; 62 63 for(Iterator i = d.v.iterator(); i.hasNext();) 64 { 65 Declaration dec = ((Definition)(i.next())).get_declaration(); 66 dec.setPackage(name); 67 if (dec instanceof StateMember) 68 stateMembers.v.add(dec); 69 else if (dec instanceof OpDecl) 70 operations.add(dec); 71 else if (dec instanceof InitDecl) 72 factories.add(dec); 73 else 74 exports.add(dec); 75 } 76 stateMembers.setContainingType(this); 77 stateMembers.setPackage(name); 78 stateMembers.setEnclosingSymbol(this); 79 80 for(Iterator i = operations.iterator(); i.hasNext();) 81 ((OpDecl)i.next()).setEnclosingSymbol(this); 82 83 for(Iterator i = exports.iterator(); i.hasNext();) 84 ((IdlSymbol)i.next()).setEnclosingSymbol(this); 85 86 for(Iterator i = factories.iterator(); i.hasNext();) 87 ((IdlSymbol)i.next()).setEnclosingSymbol(this); 88 } 89 90 public void setInheritanceSpec(ValueInheritanceSpec spec) 91 { 92 inheritanceSpec = spec; 93 } 94 95 public ValueInheritanceSpec getInheritanceSpec() 96 { 97 return inheritanceSpec; 98 } 99 100 public void isCustomMarshalled(boolean flag) 101 { 102 this.isCustomMarshalled = flag; 103 } 104 105 public boolean isCustomMarshalled() 106 { 107 return this.isCustomMarshalled; 108 } 109 110 public void setPackage(String s) 111 { 112 s = parser.pack_replace(s); 113 if (pack_name.length() > 0) 114 pack_name = s + "." + pack_name; 115 else 116 pack_name = s; 117 118 stateMembers.setPackage(s); 119 120 if (inheritanceSpec != null) 121 inheritanceSpec.setPackage(s); 122 123 for(Iterator i = operations.iterator(); i.hasNext();) 124 ((IdlSymbol)i.next()).setPackage(s); 125 126 for(Iterator i = exports.iterator(); i.hasNext();) 127 ((IdlSymbol)i.next()).setPackage(s); 128 129 for(Iterator i = factories.iterator(); i.hasNext();) 130 ((IdlSymbol)i.next()).setPackage(s); 131 } 132 133 public TypeDeclaration declaration() 134 { 135 return this; 136 } 137 138 public void parse() 139 { 140 if (inheritanceSpec != null) 141 inheritanceSpec.parse(); 142 143 boolean justAnotherOne = false; 144 145 if (isCustomMarshalled() && 146 inheritanceSpec != null && 147 inheritanceSpec.truncatable != null) 148 { 149 parser.error("Valuetype " + typeName() + 150 " may no be BOTH custom AND truncatable", token); 151 } 152 153 ConstrTypeSpec ctspec = new ConstrTypeSpec(new_num()); 154 155 try 156 { 157 escapeName(); 158 ScopedName.definePseudoScope(full_name()); 159 160 ctspec.c_type_spec = this; 161 162 NameTable.define(full_name(), "type"); 163 TypeMap.typedef(full_name(), ctspec); 164 } 165 catch (NameAlreadyDefined nad) 166 { 167 if (parser.get_pending (full_name ()) != null) 168 { 169 if (stateMembers.size () != 0) 170 { 171 justAnotherOne = true; 172 } 173 if (! full_name().equals("org.omg.CORBA.TypeCode") && 174 stateMembers.size () != 0) 175 { 176 TypeMap.replaceForwardDeclaration(full_name(), ctspec); 177 } 178 } 179 else 180 { 181 parser.error("Valuetype " + typeName() + " already defined", token); 182 } 183 } 184 185 if (hasBody) 186 { 187 ScopedName.addRecursionScope(typeName()); 188 stateMembers.parse(); 189 ScopedName.removeRecursionScope(typeName()); 190 191 if (logger.isWarnEnabled()) 192 logger.warn("valueDecl.parse(): operations"); 193 194 Iterator iter = operations.iterator(); 196 while(iter.hasNext()) 197 { 198 IdlSymbol sym = (IdlSymbol)iter.next(); 199 sym.parse(); 200 } 201 202 if (logger.isWarnEnabled()) 203 logger.warn("valueDecl.parse(): exports"); 204 205 iter = exports.iterator(); 207 while(iter.hasNext()) 208 { 209 IdlSymbol sym = (IdlSymbol)iter.next(); 210 sym.parse(); 211 212 if (sym instanceof AttrDecl) 213 { 214 Enumeration e = ((AttrDecl)sym).getOperations(); 215 while(e.hasMoreElements()) 216 operations.add(e.nextElement()); 217 } 218 } 219 220 iter = factories.iterator(); 222 while(iter.hasNext()) 223 { 224 IdlSymbol sym = (IdlSymbol)iter.next(); 225 sym.parse(); 226 } 227 228 230 if (inheritanceSpec != null) 231 { 232 Hashtable h = new Hashtable(); 233 for(Enumeration e = inheritanceSpec.getValueTypes(); 234 e.hasMoreElements();) 235 { 236 ScopedName name = (ScopedName)e.nextElement(); 237 ConstrTypeSpec ts = 238 (ConstrTypeSpec)name.resolvedTypeSpec().typeSpec(); 239 240 if (ts.declaration() instanceof Value) 241 { 242 if (h.containsKey(ts.full_name())) 243 { 244 parser.fatal_error("Illegal inheritance spec: " + 245 inheritanceSpec + 246 " (repeated inheritance not allowed).", 247 token); 248 } 249 h.put(ts.full_name(), ""); 251 continue; 252 } 253 else 254 { 255 logger.error(" Declaration is " + ts.declaration().getClass()); 256 parser.fatal_error("Non-value type in inheritance spec: \n\t" + 257 inheritanceSpec, token); 258 } 259 } 260 261 for(Enumeration e = inheritanceSpec.getSupportedInterfaces(); 262 e.hasMoreElements();) 263 { 264 ScopedName name = (ScopedName)e.nextElement(); 265 ConstrTypeSpec ts = (ConstrTypeSpec)name.resolvedTypeSpec().typeSpec(); 266 if (ts.declaration() instanceof Interface) 267 { 268 continue; 269 } 270 else 271 { 272 parser.fatal_error("Non-interface type in supported interfaces list:\n\t" + 273 inheritanceSpec, token); 274 } 275 } 276 } 277 NameTable.parsed_interfaces.put(full_name(), ""); 278 parser.remove_pending(full_name()); 279 } 280 else if (! justAnotherOne) 281 { 282 parser.set_pending(full_name()); 285 } 286 287 } 288 289 public void setEnclosingSymbol(IdlSymbol s) 290 { 291 if (enclosing_symbol != null && enclosing_symbol != s) 292 { 293 logger.error("was " + enclosing_symbol.getClass().getName() + 294 " now: " + s.getClass().getName()); 295 throw new RuntimeException ("Compiler Error: trying to reassign container for " + 296 name); 297 } 298 299 enclosing_symbol = s; 300 } 301 302 public void set_included(boolean i) 303 { 304 included = i; 305 } 306 307 public boolean basic() 308 { 309 return true; 310 } 311 312 public String toString() 313 { 314 return full_name(); 315 } 316 317 public String holderName() 318 { 319 return javaName() + "Holder"; 320 } 321 322 public String typeName() 323 { 324 return full_name(); 325 } 326 327 public String getTypeCodeExpression() 328 { 329 return this.getTypeCodeExpression(new HashSet()); 330 } 331 332 public String getTypeCodeExpression(Set knownTypes) 333 { 334 if (knownTypes.contains(this)) 335 { 336 return this.getRecursiveTypeCodeExpression(); 337 } 338 else 339 { 340 knownTypes.add(this); 341 StringBuffer result = new StringBuffer 342 ("org.omg.CORBA.ORB.init().create_value_tc (" + 343 "\"" + id() + "\", " + "\"" + name + "\", " + 345 "(short)" + 347 (this.isCustomMarshalled() 348 ? 1 : 0 ) + ", " + 352 "null, " + 354 "new org.omg.CORBA.ValueMember[] {"); 356 for(Iterator i = stateMembers.v.iterator(); i.hasNext();) 357 { 358 StateMember m = (StateMember)i.next(); 359 result.append(getValueMemberExpression(m, knownTypes)); 360 if (i.hasNext()) result.append(", "); 361 } 362 result.append("})"); 363 return result.toString(); 364 } 365 } 366 367 private String getValueMemberExpression(StateMember m, Set knownTypes) 368 { 369 TypeSpec typeSpec = m.typeSpec(); 370 short access = m.isPublic 371 ? (short)1 : (short)0; 375 return "new org.omg.CORBA.ValueMember (" + 376 "\"" + m.name + "\", \"" + typeSpec.id() + 377 "\", \"" + name + "\", \"1.0\", " + 378 typeSpec.getTypeCodeExpression(knownTypes) + ", null, " + 379 "(short)" + access + ")"; 380 } 381 382 public void print(PrintWriter ps) 383 { 384 if (included && !generateIncluded()) 386 { 387 return; 388 } 389 390 try 391 { 392 String path = parser.out_dir 393 + fileSeparator 394 + pack_name.replace('.', fileSeparator); 395 396 File dir = new File (path); 397 398 if (!dir.exists()) 399 { 400 if (!dir.mkdirs()) 401 org.jacorb.idl.parser.fatal_error 402 ("Unable to create " + path, null); 403 } 404 405 printClass(dir); 406 printFactory(dir); 407 printHelper(dir); 408 printHolder(dir); 409 } 410 catch (IOException e) 411 { 412 org.jacorb.idl.parser.fatal_error 413 ("I/O error writing " + javaName() + ": " + e, null); 414 } 415 } 416 417 public String printWriteStatement(String var_name, String streamname) 418 { 419 return "((org.omg.CORBA_2_3.portable.OutputStream)" + streamname + ")" 420 + ".write_value (" + var_name + ");"; 421 } 422 423 public String printReadExpression(String streamname) 424 { 425 return "(" + javaName() + ")" 426 + "((org.omg.CORBA_2_3.portable.InputStream)" + streamname + ")" 427 + ".read_value (\"" + id() + "\")"; 428 } 429 430 public String printReadStatement(String var_name, String streamname) 431 { 432 return var_name + " = " + printReadExpression(streamname); 433 } 434 435 private void printClassComment(PrintWriter out) 436 { 437 out.println("/**"); 438 out.println(" *\tGenerated from IDL definition of valuetype " + 439 "\"" + name + "\""); 440 out.println(" *\t@author JacORB IDL compiler "); 441 out.println(" */\n"); 442 } 443 444 447 448 private void printClass(File dir) 449 throws IOException 450 { 451 File outfile = new File (dir, name + ".java"); 452 453 if (GlobalInputStream.isMoreRecentThan(outfile)) 454 { 455 PrintWriter out = new PrintWriter (new FileWriter (outfile)); 456 457 if (pack_name.length() > 0) 458 out.println("package " + pack_name + ";\n"); 459 460 printClassComment(out); 461 out.println("public abstract class " + name); 462 463 465 StringBuffer extendsBuffer = new StringBuffer ("extends "); 466 StringBuffer implementsBuffer = new StringBuffer ("implements "); 467 468 if (this.isCustomMarshalled()) 469 implementsBuffer.append("org.omg.CORBA.portable.CustomValue"); 470 else 471 implementsBuffer.append("org.omg.CORBA.portable.StreamableValue"); 472 473 if (inheritanceSpec != null) 474 { 475 boolean first = true; 476 477 Enumeration e = inheritanceSpec.getValueTypes(); 479 if (e.hasMoreElements() || inheritanceSpec.truncatable != null) 480 { 481 if (e.hasMoreElements()) 482 { 483 ScopedName scopedName = (ScopedName)e.nextElement(); 484 ConstrTypeSpec ts = 485 (ConstrTypeSpec)scopedName.resolvedTypeSpec().typeSpec(); 486 487 if (ts.c_type_spec instanceof ValueAbsDecl) 490 { 491 implementsBuffer.append(", " + scopedName.toString()); 492 } 493 else 494 { 495 first = false; 498 extendsBuffer.append(scopedName.toString()); 499 } 500 } 501 502 for(; e.hasMoreElements();) 503 { 504 ScopedName scopedName = (ScopedName)e.nextElement(); 505 ConstrTypeSpec ts = 506 (ConstrTypeSpec)scopedName.resolvedTypeSpec().typeSpec(); 507 508 if (ts.c_type_spec instanceof ValueAbsDecl) 511 { 512 implementsBuffer.append(", " + scopedName.toString()); 513 } 514 else 515 { 516 extendsBuffer.append(", " + scopedName.toString()); 519 } 520 } 521 522 if (inheritanceSpec.truncatable != null) 525 { 526 extendsBuffer.append 527 ( 528 (first ? "" : ", ") + 529 inheritanceSpec.truncatable.scopedName 530 ); 531 } 532 } 533 534 Enumeration enumeration = inheritanceSpec.getSupportedInterfaces(); 536 if (enumeration.hasMoreElements()) 537 { 538 for(; enumeration.hasMoreElements();) 539 { 540 ScopedName sne = (ScopedName)enumeration.nextElement(); 541 implementsBuffer.append (", " + sne); 542 if (Interface.abstractInterfaces == null || 543 !Interface.abstractInterfaces.contains (sne.toString())) 544 { 545 implementsBuffer.append ("Operations"); 546 } 547 } 548 } 549 550 } 551 552 if (extendsBuffer.length() > 8) 553 { 554 hasStatefulBases = true; 555 out.println("\t" + extendsBuffer.toString()); 556 } 557 558 out.println("\t" + implementsBuffer.toString()); 559 560 out.println("{"); 561 562 565 out.print("\tprivate String[] _truncatable_ids = {\"" + id() + "\""); 566 StringBuffer sb = new StringBuffer (); 567 568 if (inheritanceSpec != null) 569 { 570 Truncatable trunc = inheritanceSpec.truncatable; 571 572 if (trunc != null) 573 { 574 sb.append(", \"" + trunc.getId() + "\""); 575 ScopedName scopedName = trunc.scopedName; 576 while(scopedName != null) 577 { 578 ValueDecl v = 579 (ValueDecl)((ConstrTypeSpec)scopedName.resolvedTypeSpec()).c_type_spec; 580 581 if (v.inheritanceSpec == null) 582 { 583 break; 584 } 585 else 586 { 587 Truncatable t = v.inheritanceSpec.truncatable; 588 if (t != null) 589 { 590 sb.append(", \"" + t.getId() + "\""); 591 scopedName = t.scopedName; 592 } 593 else 594 { 595 break; 596 } 597 } 598 } 599 } 600 } 601 out.println(sb.toString() + "};"); 602 603 for(Iterator i = stateMembers.v.iterator(); i.hasNext();) 604 { 605 ((StateMember)i.next()).print(out); 606 out.println(); 607 } 608 609 for(Iterator i = operations.iterator(); i.hasNext();) 610 { 611 ((Operation)i.next()).printSignature(out, true); 612 out.println(); 613 } 614 615 if (!this.isCustomMarshalled()) 616 { 617 printWriteMethod(out); 618 printReadMethod(out); 619 } 620 621 out.println("\tpublic String[] _truncatable_ids()"); 622 out.println("\t{"); 623 out.println("\t\treturn _truncatable_ids;"); out.println("\t}"); 625 626 out.println("\tpublic org.omg.CORBA.TypeCode _type()"); 627 out.println("\t{"); 628 out.println("\t\treturn " + javaName() + "Helper.type();"); 629 out.println("\t}"); 630 631 out.println("}"); 632 out.close(); 633 } 634 } 635 636 640 641 private void printFactory(File dir) 642 throws IOException 643 { 644 if (factories.size() == 0) 645 return; 646 647 File outfile = new File (dir, name + "ValueFactory.java"); 648 if (GlobalInputStream.isMoreRecentThan(outfile)) 649 { 650 PrintWriter out = new PrintWriter (new FileWriter (outfile)); 651 652 if (pack_name.length() > 0) 653 out.println("package " + pack_name + ";\n"); 654 655 printClassComment(out); 656 657 out.println("public interface " + name + "ValueFactory"); 658 out.println("\textends org.omg.CORBA.portable.ValueFactory"); 659 out.println("{"); 660 661 for(Iterator i = factories.iterator(); i.hasNext();) 662 { 663 ((InitDecl)i.next()).print(out, name); 664 } 665 666 out.println("}"); 667 out.close(); 668 } 669 } 670 671 672 676 private void printWriteMethod(PrintWriter out) 677 { 678 out.println("\tpublic void _write " + 679 "(org.omg.CORBA.portable.OutputStream os)"); 680 out.println("\t{"); 681 682 if (hasStatefulBases) 683 { 684 out.println("\t\tsuper._write(os);"); 685 } 686 687 for(Iterator i = stateMembers.v.iterator(); i.hasNext();) 688 { 689 out.println("\t\t" + ((StateMember)i.next()).writeStatement("os")); 690 } 691 out.println("\t}\n"); 692 } 693 694 698 699 private void printReadMethod(PrintWriter out) 700 { 701 out.println("\tpublic void _read " + 702 "(final org.omg.CORBA.portable.InputStream os)"); 703 out.println("\t{"); 704 705 if (hasStatefulBases) 706 { 707 out.println("\t\tsuper._read(os);"); 708 } 709 710 for(Iterator i = stateMembers.v.iterator(); i.hasNext();) 711 { 712 out.println("\t\t" + ((StateMember)i.next()).readStatement("os")); 713 } 714 out.println("\t}\n"); 715 } 716 717 private void printHelper(File dir) 718 throws IOException 719 { 720 File outfile = new File (dir, name + "Helper.java"); 721 if (GlobalInputStream.isMoreRecentThan(outfile)) 722 { 723 PrintWriter out = new PrintWriter (new FileWriter (outfile)); 724 725 if (pack_name.length() > 0) 726 out.println("package " + pack_name + ";\n"); 727 728 printClassComment(out); 729 730 out.println("public abstract class " + name + "Helper"); 731 out.println("{"); 732 733 out.println("\tprivate static org.omg.CORBA.TypeCode type = null;"); 734 735 737 out.println("\tpublic static void insert " + 738 "(org.omg.CORBA.Any a, " + javaName() + " v)"); 739 out.println("\t{"); 740 out.println("\t\ta.insert_Value (v, v._type());"); 741 out.println("\t}"); 742 out.println("\tpublic static " + javaName() + " extract " + 743 "(org.omg.CORBA.Any a)"); 744 out.println("\t{"); 745 out.println("\t\treturn (" + javaName() + ")a.extract_Value();"); 746 out.println("\t}"); 747 748 750 out.println("\tpublic static org.omg.CORBA.TypeCode type()"); 751 out.println("\t{"); 752 out.println("\t\tif (type == null)"); 753 out.println("\t\t\ttype = " + getTypeCodeExpression() + ";"); 754 out.println("\t\treturn type;"); 755 out.println("\t}"); 756 out.println("\tpublic static String id()"); 757 out.println("\t{"); 758 out.println("\t\treturn \"" + id() + "\";"); 759 out.println("\t}"); 760 761 763 out.println("\tpublic static " + javaName() + " read " + 764 "(org.omg.CORBA.portable.InputStream is)"); 765 out.println("\t{"); 766 out.println("\t\treturn (" + javaName() + ")((org.omg.CORBA_2_3.portable.InputStream)is).read_value (\"" + id() + "\");"); 767 out.println("\t}"); 768 769 out.println("\tpublic static void write " + 770 "(org.omg.CORBA.portable.OutputStream os, " + 771 javaName() + " val)"); 772 out.println("\t{"); 773 out.println("((org.omg.CORBA_2_3.portable.OutputStream)os)" + 774 ".write_value (val, \"" + id() + "\");"); 775 out.println("\t}"); 776 777 779 for (Iterator i = factories.iterator(); i.hasNext();) 780 { 781 InitDecl d = (InitDecl)i.next(); 782 d.printHelperMethod (out, name); 783 } 784 785 out.println("}"); 786 out.close(); 787 } 788 } 789 790 private void printHolder(File dir) throws IOException 791 { 792 File outfile = new File (dir, name + "Holder.java"); 793 if (GlobalInputStream.isMoreRecentThan(outfile)) 794 { 795 PrintWriter out = new PrintWriter (new FileWriter (outfile)); 796 797 if (pack_name.length() > 0) 798 out.println("package " + pack_name + ";\n"); 799 800 printClassComment(out); 801 802 out.println("public" + parser.getFinalString() + " class " + name + "Holder"); 803 out.println("\timplements org.omg.CORBA.portable.Streamable"); 804 out.println("{"); 805 out.println("\tpublic " + javaName() + " value;"); 806 out.println("\tpublic " + name + "Holder () {}"); 807 out.println("\tpublic " + name + "Holder (final " 808 + javaName() + " initial)"); 809 out.println("\t{"); 810 out.println("\t\tvalue = initial;"); 811 out.println("\t}"); 812 out.println("\tpublic void _read " + 813 "(final org.omg.CORBA.portable.InputStream is)"); 814 out.println("\t{"); 815 out.println("\t\tvalue = " + javaName() + "Helper.read (is);"); 816 out.println("\t}"); 817 out.println("\tpublic void _write " + 818 "(final org.omg.CORBA.portable.OutputStream os)"); 819 out.println("\t{"); 820 out.println("\t\t" + javaName() + "Helper.write (os, value);"); 821 out.println("\t}"); 822 out.println("\tpublic org.omg.CORBA.TypeCode _type ()"); 823 out.println("\t{"); 824 out.println("\t\treturn value._type ();"); 825 out.println("\t}"); 826 out.println("}"); 827 out.close(); 828 } 829 } 830 831 832 834 835 public void accept(IDLTreeVisitor visitor) 836 { 837 visitor.visitValue(this); 838 } 839 840 841 842 } 843 | Popular Tags |