001: /*
002: * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: /*
026: * COMPONENT_NAME: idl.toJava
027: *
028: * ORIGINS: 27
029: *
030: * Licensed Materials - Property of IBM
031: * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
032: * RMI-IIOP v1.0
033: *
034: * @(#)ValueBoxGen.java 1.20 07/05/05
035: */
036:
037: package com.sun.tools.corba.se.idl.toJavaPortable;
038:
039: // NOTES:
040: // -D60929 <klr> Update for RTF2.4 changes
041: // -D61056 <klr> Use Util.helperName
042: // -D62023 <klr> Fix generation botch in helper.read for boxed bounded strings
043:
044: import java.io.File;
045: import java.io.PrintWriter;
046: import java.util.Hashtable;
047: import java.util.Enumeration;
048: import java.util.Vector;
049:
050: import com.sun.tools.corba.se.idl.GenFileStream;
051: import com.sun.tools.corba.se.idl.InterfaceEntry;
052: import com.sun.tools.corba.se.idl.SymtabEntry;
053: import com.sun.tools.corba.se.idl.TypedefEntry;
054: import com.sun.tools.corba.se.idl.ValueEntry;
055: import com.sun.tools.corba.se.idl.ValueBoxEntry;
056: import com.sun.tools.corba.se.idl.InterfaceState;
057: import com.sun.tools.corba.se.idl.MethodEntry;
058: import com.sun.tools.corba.se.idl.PrimitiveEntry;
059: import com.sun.tools.corba.se.idl.SequenceEntry;
060: import com.sun.tools.corba.se.idl.StringEntry;
061:
062: /**
063: *
064: **/
065: public class ValueBoxGen implements
066: com.sun.tools.corba.se.idl.ValueBoxGen, JavaGenerator {
067: /**
068: * Public zero-argument constructor.
069: **/
070: public ValueBoxGen() {
071: } // ctor
072:
073: /**
074: *
075: **/
076: public void generate(Hashtable symbolTable, ValueBoxEntry v,
077: PrintWriter str) {
078: this .symbolTable = symbolTable;
079: this .v = v;
080:
081: TypedefEntry member = ((InterfaceState) v.state().elementAt(0)).entry;
082: SymtabEntry mType = member.type();
083: // if it's primitive type, generate a java class
084: if (mType instanceof PrimitiveEntry) {
085: openStream();
086: if (stream == null)
087: return;
088: writeHeading();
089: writeBody();
090: writeClosing();
091: closeStream();
092: } else {
093: // If a constructed type is nested in the value box,
094: // value v struct s {...};
095: // the bindings for the nested type must be handled here
096: Enumeration e = v.contained().elements();
097: while (e.hasMoreElements()) {
098: SymtabEntry contained = (SymtabEntry) e.nextElement();
099:
100: // in case of value box w/ nested enum, ex: value v enum e {e0, e1,...};
101: // the SymtabEntry for the enum and labels are contained in the vector.
102: // Must check the type to ignore the SymtabEntry for labels.
103: if (contained.type() != null)
104: contained.type().generate(symbolTable, stream);
105: }
106: }
107: generateHelper();
108: generateHolder();
109: } // generate
110:
111: /**
112: *
113: **/
114: protected void openStream() {
115: stream = Util.stream(v, ".java");
116: } // openStream
117:
118: /**
119: *
120: **/
121: protected void generateHelper() {
122: ((Factories) Compile.compiler.factories()).helper().generate(
123: symbolTable, v);
124: } // generateHelper
125:
126: /**
127: *
128: **/
129: protected void generateHolder() {
130: ((Factories) Compile.compiler.factories()).holder().generate(
131: symbolTable, v);
132: } // generateHolder
133:
134: /**
135: *
136: **/
137: protected void writeHeading() {
138: Util.writePackage(stream, v);
139: Util.writeProlog(stream, ((GenFileStream) stream).name());
140: if (v.comment() != null)
141: v.comment().generate("", stream);
142: // stream.println ("public class " + v.name () + " implements org.omg.CORBA.portable.ValueBase, org.omg.CORBA.portable.Streamable");
143: stream.println("public class " + v.name()
144: + " implements org.omg.CORBA.portable.ValueBase"); // <d60929>
145: stream.println("{");
146: } // writeHeading
147:
148: /**
149: *
150: **/
151: protected void writeBody() {
152: InterfaceState member = (InterfaceState) v.state().elementAt(0);
153: SymtabEntry entry = (SymtabEntry) member.entry;
154: Util.fillInfo(entry);
155: if (entry.comment() != null)
156: entry.comment().generate(" ", stream);
157: stream.println(" public " + Util.javaName(entry) + " value;");
158: stream.println(" public " + v.name() + " ("
159: + Util.javaName(entry) + " initial)");
160: stream.println(" {");
161: stream.println(" value = initial;");
162: stream.println(" }");
163: stream.println();
164: writeTruncatable(); // <d60929>
165: // writeStreamableMethods ();
166: } // writeBody
167:
168: /**
169: *
170: **/
171: protected void writeTruncatable() // <d60929>
172: {
173: // Per Simon, 4/6/98, emit _truncatable_ids()
174: stream.println(" public String[] _truncatable_ids() {");
175: stream.println(" return " + Util.helperName(v, true)
176: + ".get_instance().get_truncatable_base_ids();"); // <d61056>
177: stream.println(" }");
178: stream.println();
179: } // writeTruncatable
180:
181: /**
182: *
183: **/
184: protected void writeClosing() {
185: stream.println("} // class " + v.name());
186: } // writeClosing
187:
188: /**
189: *
190: **/
191: protected void closeStream() {
192: stream.close();
193: } // closeStream
194:
195: /**
196: *
197: **/
198: protected void writeStreamableMethods() {
199: stream
200: .println(" public void _read (org.omg.CORBA.portable.InputStream istream)");
201: stream.println(" {");
202: streamableRead("this", v, stream);
203: stream.println(" }");
204: stream.println();
205: stream
206: .println(" public void _write (org.omg.CORBA.portable.OutputStream ostream)");
207: stream.println(" {");
208: write(0, " ", "this", v, stream);
209: stream.println(" }");
210: stream.println();
211: stream.println(" public org.omg.CORBA.TypeCode _type ()");
212: stream.println(" {");
213: stream.println(" return " + Util.helperName(v, false)
214: + ".type ();"); // <d61056>
215: stream.println(" }");
216: } // writeStreamableMethods
217:
218: ///////////////
219: // From JavaGenerator
220:
221: public int helperType(int index, String indent,
222: TCOffsets tcoffsets, String name, SymtabEntry entry,
223: PrintWriter stream) {
224: ValueEntry vt = (ValueEntry) entry;
225: TypedefEntry member = (TypedefEntry) ((InterfaceState) (vt
226: .state()).elementAt(0)).entry;
227: SymtabEntry mType = Util.typeOf(member);
228: index = ((JavaGenerator) mType.generator()).type(index, indent,
229: tcoffsets, name, mType, stream);
230: stream.println(indent + name
231: + " = org.omg.CORBA.ORB.init ().create_value_box_tc ("
232: + "_id, " + '"' + entry.name() + "\", " + name + ");");
233: return index;
234: } // helperType
235:
236: public int type(int index, String indent, TCOffsets tcoffsets,
237: String name, SymtabEntry entry, PrintWriter stream) {
238: stream.println(indent + name + " = "
239: + Util.helperName(entry, true) + ".type ();"); // <d61056>
240: return index;
241: } // type
242:
243: public int read(int index, String indent, String name,
244: SymtabEntry entry, PrintWriter stream) {
245: return index;
246: } // read
247:
248: public void helperRead(String entryName, SymtabEntry entry,
249: PrintWriter stream) {
250: // <d59418 - KLR> per Simon, make "static" read call istream.read_value.
251: // put real marshalling code in read_value.
252: stream
253: .println(" return ("
254: + entryName
255: + ") ((org.omg.CORBA_2_3.portable.InputStream) istream).read_value (get_instance());"); // <d60929>
256: stream.println(" }");
257: stream.println();
258:
259: // done with "read", now do "read_value with real marshalling code.
260:
261: stream
262: .println(" public java.io.Serializable read_value (org.omg.CORBA.portable.InputStream istream)"); // <d60929>
263: stream.println(" {");
264: // end of <d59418> changes
265:
266: String indent = " ";
267: Vector vMembers = ((ValueBoxEntry) entry).state();
268: TypedefEntry member = ((InterfaceState) vMembers.elementAt(0)).entry;
269: SymtabEntry mType = member.type();
270: if (mType instanceof PrimitiveEntry
271: || mType instanceof SequenceEntry
272: || mType instanceof TypedefEntry
273: || mType instanceof StringEntry
274: || !member.arrayInfo().isEmpty()) {
275: stream.println(indent + Util.javaName(mType) + " tmp;"); // <d62023>
276: ((JavaGenerator) member.generator()).read(0, indent, "tmp",
277: member, stream);
278: } else if (mType instanceof ValueEntry
279: || mType instanceof ValueBoxEntry)
280: stream
281: .println(indent
282: + Util.javaQualifiedName(mType)
283: + " tmp = ("
284: + Util.javaQualifiedName(mType)
285: + ") ((org.omg.CORBA_2_3.portable.InputStream)istream).read_value ("
286: + Util.helperName(mType, true)
287: + ".get_instance ());"); // <d60929> // <d61056>
288: else
289: stream
290: .println(indent + Util.javaName(mType) + " tmp = "
291: + Util.helperName(mType, true)
292: + ".read (istream);"); // <d61056>
293: if (mType instanceof PrimitiveEntry)
294: stream.println(indent + "return new " + entryName
295: + " (tmp);");
296: else
297: stream.println(indent + "return tmp;");
298: } // helperRead
299:
300: public void helperWrite(SymtabEntry entry, PrintWriter stream) {
301: // <d59418 - KLR> per Simon, make "static" write call istream.write_value.
302: // put real marshalling code in write_value.
303: stream
304: .println(" ((org.omg.CORBA_2_3.portable.OutputStream) ostream).write_value (value, get_instance());"); // <d60929>
305: stream.println(" }");
306: stream.println();
307:
308: // done with "write", now do "write_value with real marshalling code.
309:
310: stream
311: .println(" public void write_value (org.omg.CORBA.portable.OutputStream ostream, java.io.Serializable obj)"); // <d60929>
312: stream.println(" {");
313:
314: String entryName = Util.javaName(entry);
315: stream.println(" " + entryName + " value = (" + entryName
316: + ") obj;");
317: write(0, " ", "value", entry, stream);
318: } // helperWrite
319:
320: public int write(int index, String indent, String name,
321: SymtabEntry entry, PrintWriter stream) {
322: Vector vMembers = ((ValueEntry) entry).state();
323: TypedefEntry member = ((InterfaceState) vMembers.elementAt(0)).entry;
324: SymtabEntry mType = member.type();
325:
326: if (mType instanceof PrimitiveEntry
327: || !member.arrayInfo().isEmpty())
328: index = ((JavaGenerator) member.generator()).write(index,
329: indent, name + ".value", member, stream);
330: else if (mType instanceof SequenceEntry
331: || mType instanceof StringEntry
332: || mType instanceof TypedefEntry
333: || !member.arrayInfo().isEmpty())
334: index = ((JavaGenerator) member.generator()).write(index,
335: indent, name, member, stream);
336: else if (mType instanceof ValueEntry
337: || mType instanceof ValueBoxEntry)
338: stream
339: .println(indent
340: + "((org.omg.CORBA_2_3.portable.OutputStream)ostream).write_value ((java.io.Serializable) value, " // <d60929>
341: + Util.helperName(mType, true) // <d61056>
342: + ".get_instance ());"); // <d61056>
343: else
344: stream.println(indent + Util.helperName(mType, true)
345: + ".write (ostream, " + name + ");"); // <d61056>
346: return index;
347: } // write
348:
349: protected void writeAbstract() {
350: } // writeAbstract
351:
352: protected void streamableRead(String entryName, SymtabEntry entry,
353: PrintWriter stream) {
354: Vector vMembers = ((ValueBoxEntry) entry).state();
355: TypedefEntry member = ((InterfaceState) vMembers.elementAt(0)).entry;
356: SymtabEntry mType = member.type();
357: if (mType instanceof PrimitiveEntry
358: || mType instanceof SequenceEntry
359: || mType instanceof TypedefEntry
360: || mType instanceof StringEntry
361: || !member.arrayInfo().isEmpty()) {
362: SymtabEntry mEntry = (SymtabEntry) ((InterfaceState) vMembers
363: .elementAt(0)).entry;
364: ((JavaGenerator) member.generator()).read(0, " ",
365: entryName + ".value", member, stream);
366: } else if (mType instanceof ValueEntry
367: || mType instanceof ValueBoxEntry)
368: stream
369: .println(" "
370: + entryName
371: + ".value = ("
372: + Util.javaQualifiedName(mType)
373: + ") ((org.omg.CORBA_2_3.portable.InputStream)istream).read_value ("
374: + Util.helperName(mType, true)
375: + ".get_instance ());"); // <d60929> // <d61056>
376: else
377: stream
378: .println(" " + entryName + ".value = "
379: + Util.helperName(mType, true)
380: + ".read (istream);"); // <d61056>
381: } // streamableRead
382:
383: protected Hashtable symbolTable = null;
384: protected ValueBoxEntry v = null;
385: protected PrintWriter stream = null;
386: } // class ValueBoxGen
|