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: * @(#)Holder.java 1.20 07/05/05
035: */
036:
037: package com.sun.tools.corba.se.idl.toJavaPortable;
038:
039: // NOTES:
040: // -D61056 <klr> Use Util.helperName
041:
042: import java.io.PrintWriter;
043:
044: import com.sun.tools.corba.se.idl.GenFileStream;
045: import com.sun.tools.corba.se.idl.SymtabEntry;
046: import com.sun.tools.corba.se.idl.ValueBoxEntry;
047: import com.sun.tools.corba.se.idl.InterfaceState;
048: import com.sun.tools.corba.se.idl.TypedefEntry;
049: import com.sun.tools.corba.se.idl.StringEntry;
050: import com.sun.tools.corba.se.idl.PrimitiveEntry;
051:
052: /**
053: *
054: **/
055: public class Holder implements AuxGen {
056: /**
057: * Public zero-argument constructor.
058: **/
059: public Holder() {
060: } // ctor
061:
062: /**
063: * Generate the holder class. Provides general algorithm for
064: * auxiliary binding generation:
065: * 1.) Initialize symbol table and symbol table entry members,
066: * common to all generators.
067: * 2.) Initialize members unique to this generator.
068: * 3.) Open print stream
069: * 4.) Write class heading (package, prologue, source comment, class
070: * statement, open curly
071: * 5.) Write class body (member data and methods)
072: * 6.) Write class closing (close curly)
073: * 7.) Close the print stream
074: **/
075: public void generate(java.util.Hashtable symbolTable,
076: com.sun.tools.corba.se.idl.SymtabEntry entry) {
077: this .symbolTable = symbolTable;
078: this .entry = entry;
079: init();
080:
081: openStream();
082: if (stream == null)
083: return;
084: writeHeading();
085: writeBody();
086: writeClosing();
087: closeStream();
088: } // generate
089:
090: /**
091: * Initialize variables unique to this generator.
092: **/
093: protected void init() {
094: holderClass = entry.name() + "Holder";
095: helperClass = Util.helperName(entry, true); // <d61056>
096: if (entry instanceof ValueBoxEntry) {
097: ValueBoxEntry v = (ValueBoxEntry) entry;
098: TypedefEntry member = ((InterfaceState) v.state()
099: .elementAt(0)).entry;
100: SymtabEntry mType = member.type();
101: holderType = Util.javaName(mType);
102: } else
103: holderType = Util.javaName(entry);
104: } // init
105:
106: /**
107: * Open the print stream for subsequent output.
108: **/
109: protected void openStream() {
110: stream = Util.stream(entry, "Holder.java");
111: } // openStream
112:
113: /**
114: * Generate the heading, including the package, imports,
115: * source comment, class statement, and left curly.
116: **/
117: protected void writeHeading() {
118: Util.writePackage(stream, entry, Util.HolderFile);
119: Util.writeProlog(stream, stream.name());
120: if (entry.comment() != null)
121: entry.comment().generate("", stream);
122: stream.println("public final class " + holderClass
123: + " implements org.omg.CORBA.portable.Streamable");
124: stream.println('{');
125: } // writeHeading
126:
127: /**
128: * Generate members of this class.
129: **/
130: protected void writeBody() {
131: if (entry instanceof ValueBoxEntry)
132: stream.println(" public " + holderType + " value;");
133: else
134: Util.writeInitializer(" public ", "value", "", entry,
135: stream);
136: stream.println();
137: writeCtors();
138: writeRead();
139: writeWrite();
140: writeType();
141: } // writeBody
142:
143: /**
144: * Generate the closing statements.
145: **/
146: protected void writeClosing() {
147: stream.println('}');
148: } // writeClosing
149:
150: /**
151: * Write the stream to file by closing the print stream.
152: **/
153: protected void closeStream() {
154: stream.close();
155: } // closeStream
156:
157: /**
158: * Generate the constructors.
159: **/
160: protected void writeCtors() {
161: stream.println(" public " + holderClass + " ()");
162: stream.println(" {");
163: stream.println(" }");
164: stream.println();
165: stream.println(" public " + holderClass + " (" + holderType
166: + " initialValue)");
167: stream.println(" {");
168: stream.println(" value = initialValue;");
169: stream.println(" }");
170: stream.println();
171: } // writeCtors
172:
173: /**
174: * Generate the _read method.
175: **/
176: protected void writeRead() {
177: stream
178: .println(" public void _read (org.omg.CORBA.portable.InputStream i)");
179: stream.println(" {");
180: if (entry instanceof ValueBoxEntry) {
181: TypedefEntry member = ((InterfaceState) ((ValueBoxEntry) entry)
182: .state().elementAt(0)).entry;
183: SymtabEntry mType = member.type();
184: if (mType instanceof StringEntry)
185: stream.println(" value = i.read_string ();");
186:
187: else if (mType instanceof PrimitiveEntry)
188: stream.println(" value = " + helperClass
189: + ".read (i).value;");
190:
191: else
192: stream.println(" value = " + helperClass
193: + ".read (i);");
194: } else
195: stream.println(" value = " + helperClass + ".read (i);");
196: stream.println(" }");
197: stream.println();
198: } // writeRead
199:
200: /**
201: * Generate the _write method.
202: **/
203: protected void writeWrite() {
204: stream
205: .println(" public void _write (org.omg.CORBA.portable.OutputStream o)");
206: stream.println(" {");
207: if (entry instanceof ValueBoxEntry) {
208: TypedefEntry member = ((InterfaceState) ((ValueBoxEntry) entry)
209: .state().elementAt(0)).entry;
210: SymtabEntry mType = member.type();
211: if (mType instanceof StringEntry)
212: stream.println(" o.write_string (value);");
213:
214: else if (mType instanceof PrimitiveEntry) {
215: String name = entry.name();
216: stream.println(" " + name + " vb = new " + name
217: + " (value);");
218: stream
219: .println(" " + helperClass
220: + ".write (o, vb);");
221: }
222:
223: else
224: stream.println(" " + helperClass
225: + ".write (o, value);");
226: } else
227: stream.println(" " + helperClass + ".write (o, value);");
228: stream.println(" }");
229: stream.println();
230: } // writeWrite
231:
232: /**
233: * Generate the _type method.
234: **/
235: protected void writeType() {
236: stream.println(" public org.omg.CORBA.TypeCode _type ()");
237: stream.println(" {");
238: stream.println(" return " + helperClass + ".type ();");
239: stream.println(" }");
240: stream.println();
241: } // writeType
242:
243: protected java.util.Hashtable symbolTable;
244: protected com.sun.tools.corba.se.idl.SymtabEntry entry;
245: protected GenFileStream stream;
246:
247: // Unique to this generator
248: protected String holderClass;
249: protected String helperClass;
250: protected String holderType;
251: } // class Holder
|