001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1997-2004 Gerald Brose.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: */
020:
021: package org.jacorb.idl;
022:
023: import java.io.File;
024: import java.io.FileWriter;
025: import java.io.IOException;
026: import java.io.PrintWriter;
027: import java.util.*;
028:
029: /**
030: * @author Andre Spiegel
031: * @version $Id: ValueDecl.java,v 1.50 2006/07/18 13:22:20 alphonse.bendt Exp $
032: */
033:
034: public class ValueDecl extends Value {
035: private MemberList stateMembers;
036: private List operations;
037: private List exports;
038: private List factories;
039: private ValueInheritanceSpec inheritanceSpec;
040:
041: // some flags...
042: private boolean isCustomMarshalled = false;
043: private boolean hasStatefulBases = false;
044: private boolean hasBody = false;
045:
046: /** public c'tor, called by parser */
047:
048: public ValueDecl(int num) {
049: super (num);
050: stateMembers = new MemberList(new_num());
051: operations = new ArrayList();
052: exports = new ArrayList();
053: factories = new ArrayList();
054: }
055:
056: public void setValueElements(Definitions d) {
057: hasBody = true;
058:
059: for (Iterator i = d.v.iterator(); i.hasNext();) {
060: Declaration dec = ((Definition) (i.next()))
061: .get_declaration();
062: dec.setPackage(name);
063: if (dec instanceof StateMember)
064: stateMembers.v.add(dec);
065: else if (dec instanceof OpDecl)
066: operations.add(dec);
067: else if (dec instanceof InitDecl)
068: factories.add(dec);
069: else
070: exports.add(dec);
071: }
072: stateMembers.setContainingType(this );
073: stateMembers.setPackage(name);
074: stateMembers.setEnclosingSymbol(this );
075:
076: for (Iterator i = operations.iterator(); i.hasNext();)
077: ((OpDecl) i.next()).setEnclosingSymbol(this );
078:
079: for (Iterator i = exports.iterator(); i.hasNext();)
080: ((IdlSymbol) i.next()).setEnclosingSymbol(this );
081:
082: for (Iterator i = factories.iterator(); i.hasNext();)
083: ((IdlSymbol) i.next()).setEnclosingSymbol(this );
084: }
085:
086: public void setInheritanceSpec(ValueInheritanceSpec spec) {
087: inheritanceSpec = spec;
088: }
089:
090: public ValueInheritanceSpec getInheritanceSpec() {
091: return inheritanceSpec;
092: }
093:
094: public void isCustomMarshalled(boolean flag) {
095: this .isCustomMarshalled = flag;
096: }
097:
098: public boolean isCustomMarshalled() {
099: return this .isCustomMarshalled;
100: }
101:
102: public void setPackage(String s) {
103: s = parser.pack_replace(s);
104: if (pack_name.length() > 0)
105: pack_name = s + "." + pack_name;
106: else
107: pack_name = s;
108:
109: stateMembers.setPackage(s);
110:
111: if (inheritanceSpec != null)
112: inheritanceSpec.setPackage(s);
113:
114: for (Iterator i = operations.iterator(); i.hasNext();)
115: ((IdlSymbol) i.next()).setPackage(s);
116:
117: for (Iterator i = exports.iterator(); i.hasNext();)
118: ((IdlSymbol) i.next()).setPackage(s);
119:
120: for (Iterator i = factories.iterator(); i.hasNext();)
121: ((IdlSymbol) i.next()).setPackage(s);
122: }
123:
124: public TypeDeclaration declaration() {
125: return this ;
126: }
127:
128: public void parse() {
129: if (inheritanceSpec != null) {
130: inheritanceSpec.parse();
131: }
132:
133: boolean justAnotherOne = false;
134:
135: if (isCustomMarshalled() && inheritanceSpec != null
136: && inheritanceSpec.truncatable != null) {
137: parser.error("Valuetype " + typeName()
138: + " may no be BOTH custom AND truncatable", token);
139: }
140:
141: ConstrTypeSpec ctspec = new ConstrTypeSpec(new_num());
142:
143: try {
144: escapeName();
145: ScopedName.definePseudoScope(full_name());
146:
147: ctspec.c_type_spec = this ;
148:
149: NameTable.define(full_name(), "type");
150: TypeMap.typedef(full_name(), ctspec);
151: } catch (NameAlreadyDefined nad) {
152: if (parser.get_pending(full_name()) != null) {
153: if (stateMembers.size() != 0) {
154: justAnotherOne = true;
155: }
156: if (!full_name().equals("org.omg.CORBA.TypeCode")
157: && stateMembers.size() != 0) {
158: TypeMap.replaceForwardDeclaration(full_name(),
159: ctspec);
160: }
161: } else {
162: parser.error("Valuetype " + typeName()
163: + " already defined", token);
164: }
165: }
166:
167: if (hasBody) {
168: logger
169: .warn("valueDecl.parse(): exports (but not attributes)");
170:
171: // parse exports
172: Iterator iter = exports.iterator();
173: while (iter.hasNext()) {
174: IdlSymbol idlSymbol = (IdlSymbol) iter.next();
175:
176: if (!(idlSymbol instanceof AttrDecl)) {
177: idlSymbol.parse();
178: }
179: }
180:
181: logger.warn("valueDecl.parse(): members");
182:
183: ScopedName.addRecursionScope(typeName());
184: stateMembers.parse();
185: ScopedName.removeRecursionScope(typeName());
186:
187: logger.warn("valueDecl.parse(): operations");
188:
189: // parse operations
190: iter = operations.iterator();
191: while (iter.hasNext()) {
192: IdlSymbol idlSymbol = (IdlSymbol) iter.next();
193: idlSymbol.parse();
194: }
195:
196: logger.warn("valueDecl.parse(): exports(attributes)");
197:
198: // parser exports
199: iter = exports.iterator();
200: while (iter.hasNext()) {
201: IdlSymbol idlSymbol = (IdlSymbol) iter.next();
202:
203: if (idlSymbol instanceof AttrDecl) {
204: idlSymbol.parse();
205: Enumeration e = ((AttrDecl) idlSymbol)
206: .getOperations();
207: while (e.hasMoreElements()) {
208: operations.add(e.nextElement());
209: }
210: }
211: }
212:
213: logger.warn("valueDecl.parse(): factories");
214:
215: // parse factories
216: iter = factories.iterator();
217: while (iter.hasNext()) {
218: IdlSymbol idlSymbol = (IdlSymbol) iter.next();
219: idlSymbol.parse();
220: }
221:
222: // check inheritance rules
223: logger.warn("valueDecl.parse(): check inheritance");
224:
225: if (inheritanceSpec != null) {
226: HashSet h = new HashSet();
227: for (Enumeration e = inheritanceSpec.getValueTypes(); e
228: .hasMoreElements();) {
229: ScopedName scopedName = (ScopedName) e
230: .nextElement();
231:
232: ConstrTypeSpec ts = unwindTypedefs(scopedName);
233:
234: if (ts.declaration() instanceof Value) {
235: if (h.contains(ts.full_name())) {
236: parser
237: .fatal_error(
238: "Illegal inheritance spec: "
239: + inheritanceSpec
240: + " (repeated inheritance not allowed).",
241: token);
242: }
243: // else:
244: h.add(ts.full_name());
245: continue;
246: }
247: logger.error(" Declaration is "
248: + ts.declaration().getClass());
249: parser.fatal_error(
250: "Non-value type in inheritance spec: \n\t"
251: + inheritanceSpec, token);
252: }
253:
254: for (Enumeration e = inheritanceSpec
255: .getSupportedInterfaces(); e.hasMoreElements();) {
256: ScopedName scopedName = (ScopedName) e
257: .nextElement();
258: ConstrTypeSpec ts = (ConstrTypeSpec) scopedName
259: .resolvedTypeSpec().typeSpec();
260: if (ts.declaration() instanceof Interface) {
261: continue;
262: }
263: parser.fatal_error(
264: "Non-interface type in supported interfaces list:\n\t"
265: + inheritanceSpec, token);
266: }
267: }
268: NameTable.parsed_interfaces.put(full_name(), "");
269: parser.remove_pending(full_name());
270: } else if (!justAnotherOne) {
271: // i am forward declared, must set myself as
272: // pending further parsing
273: parser.set_pending(full_name());
274: }
275:
276: }
277:
278: private ConstrTypeSpec unwindTypedefs(ScopedName scopedName) {
279: TypeSpec resolvedTSpec = scopedName.resolvedTypeSpec();
280: //unwind any typedefs
281: while (resolvedTSpec instanceof AliasTypeSpec) {
282: resolvedTSpec = ((AliasTypeSpec) resolvedTSpec)
283: .originalType();
284: }
285:
286: if (!(resolvedTSpec instanceof ConstrTypeSpec)) {
287: if (logger.isDebugEnabled()) {
288: logger
289: .debug("Illegal inheritance spec, not a constr. type but "
290: + resolvedTSpec.getClass()
291: + ", name "
292: + scopedName);
293: }
294: parser.fatal_error(
295: "Illegal inheritance spec (not a constr. type): "
296: + inheritanceSpec, token);
297: }
298:
299: return (ConstrTypeSpec) resolvedTSpec;
300: }
301:
302: public void setEnclosingSymbol(IdlSymbol s) {
303: if (enclosing_symbol != null && enclosing_symbol != s) {
304: logger.error("was " + enclosing_symbol.getClass().getName()
305: + " now: " + s.getClass().getName());
306: throw new RuntimeException(
307: "Compiler Error: trying to reassign container for "
308: + name);
309: }
310:
311: enclosing_symbol = s;
312: }
313:
314: public void set_included(boolean i) {
315: included = i;
316: }
317:
318: public boolean basic() {
319: return true;
320: }
321:
322: public String toString() {
323: return full_name();
324: }
325:
326: public String holderName() {
327: return javaName() + "Holder";
328: }
329:
330: public String helperName() {
331: return javaName() + "Helper";
332: }
333:
334: public String typeName() {
335: return full_name();
336: }
337:
338: public String getTypeCodeExpression() {
339: return this .getTypeCodeExpression(new HashSet());
340: }
341:
342: public String getTypeCodeExpression(Set knownTypes) {
343: if (knownTypes.contains(this )) {
344: return this .getRecursiveTypeCodeExpression();
345: }
346:
347: knownTypes.add(this );
348:
349: String baseType = "null";
350:
351: // Only add e.g. FooHelper.type() for those inherited non-abstract ValueTypes.
352: if (hasStatefulBases && inheritanceSpec != null
353: && inheritanceSpec.v.size() > 0) {
354: baseType = ((ScopedName) inheritanceSpec.v.get(0))
355: .resolvedName()
356: + "Helper.type()";
357: }
358:
359: StringBuffer result = new StringBuffer(
360: "org.omg.CORBA.ORB.init().create_value_tc (" +
361: // id, name
362: "\"" + id() + "\", " + "\"" + name + "\", " +
363: // type modifier
364: "(short)" + (this .isCustomMarshalled()
365: // symbolic constants might not be defined under jdk 1.1
366: ? 1 // org.omg.CORBA.VM_CUSTOM.value
367: : 0 // org.omg.CORBA.VM_NONE.value
368: ) + ", " +
369: // concrete base type
370: baseType + ", " +
371: // value members
372: "new org.omg.CORBA.ValueMember[] {");
373: for (Iterator i = stateMembers.v.iterator(); i.hasNext();) {
374: StateMember m = (StateMember) i.next();
375: result.append(getValueMemberExpression(m, knownTypes));
376: if (i.hasNext())
377: result.append(", ");
378: }
379: result.append("})");
380: return result.toString();
381: }
382:
383: private String getValueMemberExpression(StateMember m,
384: Set knownTypes) {
385: TypeSpec typeSpec = m.typeSpec();
386: short access = m.isPublic
387: // the symbolic constants might not be defined under jdk 1.1
388: ? (short) 1 // org.omg.CORBA.PUBLIC_MEMBER.value
389: : (short) 0; // org.omg.CORBA.PRIVATE_MEMBER.value
390:
391: return "new org.omg.CORBA.ValueMember (" + "\"" + m.name
392: + "\", \"" + typeSpec.id() + "\", \"" + name
393: + "\", \"1.0\", "
394: + typeSpec.getTypeCodeExpression(knownTypes)
395: + ", null, " + "(short)" + access + ")";
396: }
397:
398: public void print(PrintWriter ps) {
399: // no code generation for included definitions
400: if (included && !generateIncluded()) {
401: return;
402: }
403:
404: //no code generation for forward declarations (bug #539)
405: if (!hasBody) {
406: return;
407: }
408:
409: try {
410: String path = parser.out_dir + fileSeparator
411: + pack_name.replace('.', fileSeparator);
412:
413: File dir = new File(path);
414:
415: if (!dir.exists()) {
416: if (!dir.mkdirs())
417: org.jacorb.idl.parser.fatal_error(
418: "Unable to create " + path, null);
419: }
420:
421: printClass(dir);
422: printFactory(dir);
423: printHelper(dir);
424: printHolder(dir);
425:
426: // print class files for exports definitions
427: for (Iterator i = exports.iterator(); i.hasNext();) {
428: ((IdlSymbol) i.next()).print(null);
429: }
430: } catch (IOException e) {
431: org.jacorb.idl.parser.fatal_error("I/O error writing "
432: + javaName() + ": " + e, null);
433: }
434: }
435:
436: public String printWriteStatement(String var_name, String streamname) {
437: return "((org.omg.CORBA_2_3.portable.OutputStream)"
438: + streamname + ")" + ".write_value (" + var_name + ");";
439: }
440:
441: public String printReadExpression(String streamname) {
442: return "(" + javaName() + ")"
443: + "((org.omg.CORBA_2_3.portable.InputStream)"
444: + streamname + ")" + ".read_value (\"" + id() + "\")";
445: }
446:
447: public String printReadStatement(String var_name, String streamname) {
448: return var_name + " = " + printReadExpression(streamname);
449: }
450:
451: /**
452: * Prints the abstract Java class to which this valuetype is mapped.
453: */
454:
455: private void printClass(File dir) throws IOException {
456: File outfile = new File(dir, name + ".java");
457:
458: // If we have a body (i.e. we've defined any pending_interface) and the 'more
459: // recent check' is ok then write the file.
460: if (hasBody && GlobalInputStream.isMoreRecentThan(outfile)) {
461: PrintWriter out = new PrintWriter(new FileWriter(outfile));
462:
463: if (pack_name.length() > 0) {
464: out.println("package " + pack_name + ";\n");
465: }
466:
467: printClassComment("valuetype", name, out);
468: out.println("public abstract class " + name);
469:
470: // set up extends and implements clauses
471:
472: StringBuffer extendsBuffer = new StringBuffer("extends ");
473: StringBuffer implements Buffer = new StringBuffer(
474: "implements ");
475:
476: if (this .isCustomMarshalled())
477: implements Buffer
478: .append("org.omg.CORBA.portable.CustomValue");
479: else
480: implements Buffer
481: .append("org.omg.CORBA.portable.StreamableValue");
482:
483: if (inheritanceSpec != null) {
484: boolean first = true;
485:
486: // go through ancestor value types
487: Enumeration e = inheritanceSpec.getValueTypes();
488: if (e.hasMoreElements()
489: || inheritanceSpec.truncatable != null) {
490: if (e.hasMoreElements()) {
491: ScopedName scopedName = (ScopedName) e
492: .nextElement();
493: ConstrTypeSpec ts = unwindTypedefs(scopedName);
494: //(ConstrTypeSpec)scopedName.resolvedTypeSpec().typeSpec();
495:
496: // abstract base valuetypes are mapped to interfaces, so
497: // we "implement"
498: if (ts.c_type_spec instanceof ValueAbsDecl) {
499: implements Buffer.append(", "
500: + ts.toString());
501: } else {
502: // stateful base valuetypes are mapped to classes, so
503: // we "extend"
504: first = false;
505: extendsBuffer.append(ts.toString());
506: }
507: }
508:
509: for (; e.hasMoreElements();) {
510: ScopedName scopedName = (ScopedName) e
511: .nextElement();
512: ConstrTypeSpec ts = (ConstrTypeSpec) scopedName
513: .resolvedTypeSpec().typeSpec();
514:
515: // abstract base valuetypes are mapped to interfaces, so
516: // we "implement"
517: if (ts.c_type_spec instanceof ValueAbsDecl) {
518: implements Buffer.append(", "
519: + scopedName.toString());
520: } else {
521: // stateful base valuetypes are mapped to classes, so
522: // we "extend"
523: //
524: // applied patch by Thomas Leineweber for bug #492
525: //
526: if (first) {
527: extendsBuffer.append(scopedName
528: .toString());
529: first = false;
530: } else {
531: extendsBuffer.append(", "
532: + scopedName.toString());
533: }
534: }
535: }
536:
537: // also check for the presence of a stateful base value type
538: // that we can be truncated to
539: if (inheritanceSpec.truncatable != null) {
540: extendsBuffer
541: .append((first ? "" : ", ")
542: + inheritanceSpec.truncatable.scopedName);
543: }
544: }
545:
546: // go through supported interfaces
547: Enumeration enumeration = inheritanceSpec
548: .getSupportedInterfaces();
549: if (enumeration.hasMoreElements()) {
550: for (; enumeration.hasMoreElements();) {
551: ScopedName sne = (ScopedName) enumeration
552: .nextElement();
553: implements Buffer.append(", " + sne);
554: if (Interface.abstractInterfaces == null
555: || !Interface.abstractInterfaces
556: .contains(sne.toString())) {
557: implements Buffer.append("Operations");
558: }
559: }
560: }
561:
562: }
563:
564: if (extendsBuffer.length() > 8) {
565: hasStatefulBases = true;
566: out.println("\t" + extendsBuffer.toString());
567: }
568:
569: out.println("\t" + implements Buffer.toString());
570:
571: out.println("{");
572:
573: // collect and print repository ids that this value type can
574: // truncated to.
575:
576: out.print("\tprivate String[] _truncatable_ids = {\""
577: + id() + "\"");
578: StringBuffer sb = new StringBuffer();
579:
580: if (inheritanceSpec != null) {
581: Truncatable trunc = inheritanceSpec.truncatable;
582:
583: if (trunc != null) {
584: sb.append(", \"" + trunc.getId() + "\"");
585: ScopedName scopedName = trunc.scopedName;
586: while (scopedName != null) {
587: ValueDecl v = (ValueDecl) ((ConstrTypeSpec) scopedName
588: .resolvedTypeSpec()).c_type_spec;
589:
590: if (v.inheritanceSpec == null) {
591: break;
592: }
593: Truncatable t = v.inheritanceSpec.truncatable;
594: if (t != null) {
595: sb.append(", \"" + t.getId() + "\"");
596: scopedName = t.scopedName;
597: } else {
598: break;
599: }
600: }
601: }
602: }
603: out.println(sb.toString() + "};");
604:
605: for (Iterator i = stateMembers.v.iterator(); i.hasNext();) {
606: ((StateMember) i.next()).print(out);
607: out.println();
608: }
609:
610: for (Iterator i = operations.iterator(); i.hasNext();) {
611: ((Operation) i.next()).printSignature(out, true);
612: out.println();
613: }
614:
615: if (!this .isCustomMarshalled()) {
616: printWriteMethod(out);
617: printReadMethod(out);
618: }
619:
620: out.println("\tpublic String[] _truncatable_ids()");
621: out.println("\t{");
622: out.println("\t\treturn _truncatable_ids;"); // FIXME
623: out.println("\t}");
624:
625: out.println("\tpublic org.omg.CORBA.TypeCode _type()");
626: out.println("\t{");
627: out.println("\t\treturn " + javaName() + "Helper.type();");
628: out.println("\t}");
629:
630: out.println("}");
631: out.close();
632: }
633: }
634:
635: /**
636: * Prints the Factory interface for this valuetype if any
637: * factories were defined.
638: */
639:
640: private void printFactory(File dir) throws IOException {
641: if (factories.size() == 0) {
642: return;
643: }
644:
645: File outfile = new File(dir, name + "ValueFactory.java");
646:
647: // If we have a body (i.e. we've defined any pending_interface) and the 'more
648: // recent check' is ok then write the file.
649: if (hasBody && GlobalInputStream.isMoreRecentThan(outfile)) {
650: PrintWriter out = new PrintWriter(new FileWriter(outfile));
651:
652: if (pack_name.length() > 0) {
653: out.println("package " + pack_name + ";\n");
654: }
655:
656: printClassComment("valuetype", name, out);
657:
658: out.println("public interface " + name + "ValueFactory");
659: out
660: .println("\textends org.omg.CORBA.portable.ValueFactory");
661: out.println("{");
662:
663: for (Iterator i = factories.iterator(); i.hasNext();) {
664: ((InitDecl) i.next()).print(out, name);
665: }
666:
667: out.println("}");
668: out.close();
669: }
670: }
671:
672: /**
673: * Prints the _write() method required by
674: * org.omg.CORBA.portable.StreamableValue.
675: */
676: private void printWriteMethod(PrintWriter out) {
677: out.println("\tpublic void _write "
678: + "(org.omg.CORBA.portable.OutputStream os)");
679: out.println("\t{");
680:
681: if (hasStatefulBases) {
682: out.println("\t\tsuper._write(os);");
683: }
684:
685: for (Iterator i = stateMembers.v.iterator(); i.hasNext();) {
686: out.println("\t\t"
687: + ((StateMember) i.next()).writeStatement("os"));
688: }
689: out.println("\t}\n");
690: }
691:
692: /**
693: * Prints the _read() method required by
694: * org.omg.CORBA.portable.StreamableValue.
695: */
696:
697: private void printReadMethod(PrintWriter out) {
698: out.println("\tpublic void _read "
699: + "(final org.omg.CORBA.portable.InputStream os)");
700: out.println("\t{");
701:
702: if (hasStatefulBases) {
703: out.println("\t\tsuper._read(os);");
704: }
705:
706: for (Iterator i = stateMembers.v.iterator(); i.hasNext();) {
707: out.println("\t\t"
708: + ((StateMember) i.next()).readStatement("os"));
709: }
710: out.println("\t}\n");
711: }
712:
713: private void printHelper(File dir) throws IOException {
714: File outfile = new File(dir, name + "Helper.java");
715:
716: // If we have a body (i.e. we've defined any pending_interface) and the 'more
717: // recent check' is ok then write the file.
718: if (hasBody && GlobalInputStream.isMoreRecentThan(outfile)) {
719: PrintWriter out = new PrintWriter(new FileWriter(outfile));
720:
721: if (pack_name.length() > 0) {
722: out.println("package " + pack_name + ";\n");
723: }
724:
725: printClassComment("valuetype", name, out);
726:
727: out.println("public abstract class " + name + "Helper");
728: out.println("{");
729:
730: out
731: .println("\tprivate static org.omg.CORBA.TypeCode type = null;");
732:
733: // insert() / extract()
734:
735: out.println("\tpublic static void insert "
736: + "(org.omg.CORBA.Any a, " + javaName() + " v)");
737: out.println("\t{");
738: out.println("\t\ta.insert_Value (v, v._type());");
739: out.println("\t}");
740: out.println("\tpublic static " + javaName() + " extract "
741: + "(org.omg.CORBA.Any a)");
742: out.println("\t{");
743: out.println("\t\treturn (" + javaName()
744: + ")a.extract_Value();");
745: out.println("\t}");
746:
747: // type() / id()
748:
749: out
750: .println("\tpublic static org.omg.CORBA.TypeCode type()");
751: out.println("\t{");
752: out.println("\t\tif (type == null)");
753: out
754: .println("\t\t\ttype = " + getTypeCodeExpression()
755: + ";");
756: out.println("\t\treturn type;");
757: out.println("\t}");
758: out.println("\tpublic static String id()");
759: out.println("\t{");
760: out.println("\t\treturn \"" + id() + "\";");
761: out.println("\t}");
762:
763: // read() / write()
764:
765: out.println("\tpublic static " + javaName() + " read "
766: + "(org.omg.CORBA.portable.InputStream is)");
767: out.println("\t{");
768: out
769: .println("\t\treturn ("
770: + javaName()
771: + ")((org.omg.CORBA_2_3.portable.InputStream)is).read_value (\""
772: + id() + "\");");
773: out.println("\t}");
774:
775: out.println("\tpublic static void write "
776: + "(org.omg.CORBA.portable.OutputStream os, "
777: + javaName() + " val)");
778: out.println("\t{");
779: out.println("((org.omg.CORBA_2_3.portable.OutputStream)os)"
780: + ".write_value (val, \"" + id() + "\");");
781: out.println("\t}");
782:
783: // factory methods
784:
785: for (Iterator i = factories.iterator(); i.hasNext();) {
786: InitDecl d = (InitDecl) i.next();
787: d.printHelperMethod(out, name);
788: }
789:
790: out.println("}");
791: out.close();
792: }
793: }
794:
795: private void printHolder(File dir) throws IOException {
796: File outfile = new File(dir, name + "Holder.java");
797:
798: // If we have a body (i.e. we've defined any pending_interface) and the 'more
799: // recent check' is ok then write the file.
800: if (hasBody && GlobalInputStream.isMoreRecentThan(outfile)) {
801: PrintWriter out = new PrintWriter(new FileWriter(outfile));
802:
803: if (pack_name.length() > 0) {
804: out.println("package " + pack_name + ";\n");
805: }
806:
807: printClassComment("valuetype", name, out);
808:
809: out.println("public" + parser.getFinalString() + " class "
810: + name + "Holder");
811: out
812: .println("\timplements org.omg.CORBA.portable.Streamable");
813: out.println("{");
814: out.println("\tpublic " + javaName() + " value;");
815: out.println("\tpublic " + name + "Holder () {}");
816: out.println("\tpublic " + name + "Holder (final "
817: + javaName() + " initial)");
818: out.println("\t{");
819: out.println("\t\tvalue = initial;");
820: out.println("\t}");
821: out.println("\tpublic void _read "
822: + "(final org.omg.CORBA.portable.InputStream is)");
823: out.println("\t{");
824: out.println("\t\tvalue = " + javaName()
825: + "Helper.read (is);");
826: out.println("\t}");
827: out.println("\tpublic void _write "
828: + "(final org.omg.CORBA.portable.OutputStream os)");
829: out.println("\t{");
830: out.println("\t\t" + javaName()
831: + "Helper.write (os, value);");
832: out.println("\t}");
833: out.println("\tpublic org.omg.CORBA.TypeCode _type ()");
834: out.println("\t{");
835: out.println("\t\treturn value._type ();");
836: out.println("\t}");
837: out.println("}");
838: out.close();
839: }
840: }
841:
842: public void printInsertIntoAny(PrintWriter ps, String anyname,
843: String varname) {
844: ps.println("\t\t" + anyname + ".insert_Value(" + varname + ", "
845: + varname + "._type());");
846: }
847:
848: public void printExtractResult(PrintWriter ps, String resultname,
849: String anyname, String resulttype) {
850: ps.println("\t\t" + resultname + " = (" + resulttype + ")"
851: + anyname + ".extract_Value();");
852: }
853:
854: public void accept(IDLTreeVisitor visitor) {
855: visitor.visitValue(this);
856: }
857: }
|