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: * @(#)EnumGen.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.File;
043: import java.io.PrintWriter;
044: import java.util.Enumeration;
045: import java.util.Hashtable;
046: import java.util.Vector;
047:
048: import com.sun.tools.corba.se.idl.GenFileStream;
049: import com.sun.tools.corba.se.idl.EnumEntry;
050: import com.sun.tools.corba.se.idl.SymtabEntry;
051:
052: /**
053: *
054: **/
055: public class EnumGen implements com.sun.tools.corba.se.idl.EnumGen,
056: JavaGenerator {
057: /**
058: * Public zero-argument constructor.
059: **/
060: public EnumGen() {
061: } // ctor
062:
063: /**
064: * Generate the Java code for an IDL enumeration.
065: **/
066: public void generate(Hashtable symbolTable, EnumEntry e,
067: PrintWriter s) {
068: this .symbolTable = symbolTable;
069: this .e = e;
070: init();
071:
072: openStream();
073: if (stream == null)
074: return;
075: generateHolder();
076: generateHelper();
077: writeHeading();
078: writeBody();
079: writeClosing();
080: closeStream();
081: } // generate
082:
083: /**
084: * Initialize members unique to this generator.
085: **/
086: protected void init() {
087: className = e.name();
088: fullClassName = Util.javaName(e);
089: }
090:
091: /**
092: * Open the print stream to which to write the enumeration class.
093: **/
094: protected void openStream() {
095: stream = Util.stream(e, ".java");
096: }
097:
098: /**
099: * Generate the holder class for this enumeration.
100: **/
101: protected void generateHolder() {
102: ((Factories) Compile.compiler.factories()).holder().generate(
103: symbolTable, e);
104: }
105:
106: /**
107: * Generate the helper class for this enumeration.
108: **/
109: protected void generateHelper() {
110: ((Factories) Compile.compiler.factories()).helper().generate(
111: symbolTable, e);
112: }
113:
114: /**
115: * Write the heading of the enumeration class, including the package,
116: * imports, class statement, and open curly.
117: **/
118: protected void writeHeading() {
119: Util.writePackage(stream, e);
120: Util.writeProlog(stream, ((GenFileStream) stream).name());
121: if (e.comment() != null)
122: e.comment().generate("", stream);
123: stream.println("public class " + className
124: + " implements org.omg.CORBA.portable.IDLEntity");
125: stream.println("{");
126: }
127:
128: /**
129: * Write the members of enumeration class.
130: **/
131: protected void writeBody() {
132: stream.println(" private int __value;");
133: stream.println(" private static int __size = "
134: + (e.elements().size()) + ';');
135: stream.println(" private static " + fullClassName
136: + "[] __array = new " + fullClassName + " [__size];");
137: stream.println();
138: for (int i = 0; i < e.elements().size(); ++i) {
139: String label = (String) e.elements().elementAt(i);
140: stream.println(" public static final int _" + label
141: + " = " + i + ';');
142: stream.println(" public static final " + fullClassName
143: + ' ' + label + " = new " + fullClassName + "(_"
144: + label + ");");
145: }
146: stream.println();
147: writeValue();
148: writeFromInt();
149: writeCtors();
150: }
151:
152: /**
153: * Write the value method for the enumeration class.
154: **/
155: protected void writeValue() {
156: stream.println(" public int value ()");
157: stream.println(" {");
158: stream.println(" return __value;");
159: stream.println(" }");
160: stream.println();
161: } // writeValue
162:
163: /**
164: * Write the from_int method for the enumeration class.
165: **/
166: protected void writeFromInt() {
167: stream.println(" public static " + fullClassName
168: + " from_int (int value)");
169: stream.println(" {");
170: stream.println(" if (value >= 0 && value < __size)");
171: stream.println(" return __array[value];");
172: stream.println(" else");
173: stream.println(" throw new org.omg.CORBA.BAD_PARAM ();");
174: stream.println(" }");
175: stream.println();
176: }
177:
178: /**
179: * Write the protected constructor for the enumeration class.
180: **/
181: protected void writeCtors() {
182: stream.println(" protected " + className + " (int value)");
183: stream.println(" {");
184: stream.println(" __value = value;");
185: stream.println(" __array[__value] = this;");
186: stream.println(" }");
187: }
188:
189: /**
190: * Close the enumeration class.
191: **/
192: protected void writeClosing() {
193: stream.println("} // class " + className);
194: }
195:
196: /**
197: * Close the print stream, which writes the stream to file.
198: **/
199: protected void closeStream() {
200: stream.close();
201: }
202:
203: ///////////////
204: // From JavaGenerator
205:
206: public int helperType(int index, String indent,
207: TCOffsets tcoffsets, String name, SymtabEntry entry,
208: PrintWriter stream) {
209: tcoffsets.set(entry);
210: EnumEntry enumEntry = (EnumEntry) entry;
211: StringBuffer emit = new StringBuffer("new String[] { ");
212: Enumeration e = enumEntry.elements().elements();
213: boolean firstTime = true;
214: while (e.hasMoreElements()) {
215: if (firstTime)
216: firstTime = false;
217: else
218: emit.append(", ");
219: emit.append('"' + Util.stripLeadingUnderscores((String) e
220: .nextElement()) + '"');
221: }
222: emit.append("} ");
223: stream.println(indent
224: + name
225: + " = org.omg.CORBA.ORB.init ().create_enum_tc ("
226: + Util.helperName(enumEntry, true)
227: + ".id (), \"" // <54697> // <d61056>
228: // + "_id, \"" <54697>
229: + Util.stripLeadingUnderscores(entry.name()) + "\", "
230: + new String(emit) + ");");
231: return index + 1;
232:
233: } // helperType
234:
235: public int type(int index, String indent, TCOffsets tcoffsets,
236: String name, SymtabEntry entry, PrintWriter stream) {
237: stream.println(indent + name + " = "
238: + Util.helperName(entry, true) + ".type ();"); // <d61056>
239: return index;
240: } // type
241:
242: public void helperRead(String entryName, SymtabEntry entry,
243: PrintWriter stream) {
244: stream.println(" return " + Util.javaQualifiedName(entry)
245: + ".from_int (istream.read_long ());");
246: } // helperRead
247:
248: public void helperWrite(SymtabEntry entry, PrintWriter stream) {
249: stream.println(" ostream.write_long (value.value ());");
250: } // helperWrite
251:
252: public int read(int index, String indent, String name,
253: SymtabEntry entry, PrintWriter stream) {
254: stream.println(indent + name + " = "
255: + Util.javaQualifiedName(entry)
256: + ".from_int (istream.read_long ());");
257: return index;
258: } // read
259:
260: public int write(int index, String indent, String name,
261: SymtabEntry entry, PrintWriter stream) {
262: stream.println(indent + "ostream.write_long (" + name
263: + ".value ());");
264: return index;
265: } // write
266:
267: // From JavaGenerator
268: ///////////////
269:
270: protected Hashtable symbolTable = null;
271: protected EnumEntry e = null;
272: protected PrintWriter stream = null;
273:
274: // Member data unique to this generator
275: String className = null;
276: String fullClassName = null;
277: } // class EnumGen
278:
279: /*============================================================================
280: DATE<AUTHOR> ACTION
281: ----------------------------------------------------------------------------
282: 31jul1997<daz> Modified to write comment immediately preceding class defining
283: enumeration declaration.
284: 12dec1998<klr> D55971 - omg 98-11-03 Java 2.4 RTF - make subclassable
285: ===========================================================================*/
|