001: /*
002: * Copyright 1999-2000 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: * @(#)ConstGen.java 1.20 07/05/05
035: */
036:
037: package com.sun.tools.corba.se.idl.toJavaPortable;
038:
039: // NOTES:
040:
041: import java.io.File;
042: import java.io.PrintWriter;
043: import java.util.Hashtable;
044: import java.util.Vector;
045:
046: import com.sun.tools.corba.se.idl.GenFileStream;
047: import com.sun.tools.corba.se.idl.ConstEntry;
048: import com.sun.tools.corba.se.idl.ModuleEntry;
049: import com.sun.tools.corba.se.idl.PrimitiveEntry;
050: import com.sun.tools.corba.se.idl.StringEntry;
051: import com.sun.tools.corba.se.idl.SymtabEntry;
052: import com.sun.tools.corba.se.idl.TypedefEntry;
053:
054: /**
055: *
056: **/
057: public class ConstGen implements com.sun.tools.corba.se.idl.ConstGen {
058: /**
059: * Public zero-argument constructor.
060: **/
061: public ConstGen() {
062: } // ctor
063:
064: /**
065: * Generate Java code for an IDL constant. A constant is written to
066: * a new class only when it is not a member of an interface; otherwise
067: * it written to the interface class in which it resides.
068: **/
069: public void generate(Hashtable symbolTable, ConstEntry c,
070: PrintWriter s) {
071: this .symbolTable = symbolTable;
072: this .c = c;
073: this .stream = s;
074: init();
075:
076: if (c.container() instanceof ModuleEntry)
077: generateConst();
078: else if (stream != null)
079: writeConstExpr();
080: } // generate
081:
082: /**
083: * Initialize members unique to this generator.
084: **/
085: protected void init() {
086: } // init
087:
088: /**
089: * Generate the class defining the constant.
090: **/
091: protected void generateConst() {
092: openStream();
093: if (stream == null)
094: return;
095: writeHeading();
096: writeBody();
097: writeClosing();
098: closeStream();
099: } // generateConst
100:
101: /**
102: * Open a new print stream only if the constant is not a member
103: * of an interface.
104: **/
105: protected void openStream() {
106: stream = Util.stream(c, ".java");
107: } // openStream
108:
109: /**
110: * Write the heading for the class defining the constant.
111: **/
112: protected void writeHeading() {
113: Util.writePackage(stream, c);
114: Util.writeProlog(stream, ((GenFileStream) stream).name());
115: stream.println("public interface " + c.name());
116: // should not be done according to the mapping
117: // + " extends org.omg.CORBA.portable.IDLEntity");
118: stream.println("{");
119: } // writeHeading
120:
121: /**
122: * Write the constant expression and any comment, if present.
123: **/
124: protected void writeBody() {
125: writeConstExpr();
126: } // writeBody
127:
128: /**
129: * Write the entire constant expression and any comment, if present.
130: **/
131: protected void writeConstExpr() {
132: if (c.comment() != null)
133: c.comment().generate(" ", stream);
134: if (c.container() instanceof ModuleEntry) {
135:
136: stream.print(" public static final "
137: + Util.javaName(c.type()) + " value = ");
138: } else {
139: stream.print(" public static final "
140: + Util.javaName(c.type()) + ' ' + c.name() + " = ");
141: }
142: writeConstValue(c.type());
143: } // writeConstExpr
144:
145: /**
146: * Write the constant's value according to its type.
147: **/
148: private void writeConstValue(SymtabEntry type) {
149: if (type instanceof PrimitiveEntry)
150: stream.println('(' + Util.javaName(type) + ")("
151: + Util.parseExpression(c.value()) + ");");
152: else if (type instanceof StringEntry)
153: stream.println(Util.parseExpression(c.value()) + ';');
154: else if (type instanceof TypedefEntry) {
155: while (type instanceof TypedefEntry)
156: type = type.type();
157: writeConstValue(type);
158: } else
159: stream.println(Util.parseExpression(c.value()) + ';');
160: } // writeValue
161:
162: /**
163: * Generate any last words and close the class.
164: **/
165: protected void writeClosing() {
166: stream.println("}");
167: } // writeClosing
168:
169: /**
170: * Close the print stream, causing the file to be written.
171: **/
172: protected void closeStream() {
173: stream.close();
174: } // closeStream
175:
176: protected java.util.Hashtable symbolTable = null;
177: protected ConstEntry c = null;
178: protected PrintWriter stream = null;
179: } // class ConstGen
180:
181: /*=======================================================================================
182: DATE-AUTHOR ACTION
183: ---------------------------------------------------------------------------------------
184: 11sep1997daz Return when print stream is null and container is NOT a module. Fixes
185: -keep option, which causes null print stream to be sent to ConstGen.
186: 31jul1997daz Write source comment immediately preceding constant declaration.
187: =======================================================================================*/
|