1 /*
   2  * Copyright (c) 1999, 2000, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 /*
  26  * COMPONENT_NAME: idl.toJava
  27  *
  28  * ORIGINS: 27
  29  *
  30  * Licensed Materials - Property of IBM
  31  * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
  32  * RMI-IIOP v1.0
  33  *
  34  */
  35 
  36 package com.sun.tools.corba.se.idl.toJavaPortable;
  37 
  38 // NOTES:
  39 
  40 import java.io.File;
  41 import java.io.PrintWriter;
  42 import java.util.Hashtable;
  43 import java.util.Vector;
  44 
  45 import com.sun.tools.corba.se.idl.GenFileStream;
  46 import com.sun.tools.corba.se.idl.ConstEntry;
  47 import com.sun.tools.corba.se.idl.ModuleEntry;
  48 import com.sun.tools.corba.se.idl.PrimitiveEntry;
  49 import com.sun.tools.corba.se.idl.StringEntry;
  50 import com.sun.tools.corba.se.idl.SymtabEntry;
  51 import com.sun.tools.corba.se.idl.TypedefEntry;
  52 
  53 /**
  54  *
  55  **/
  56 public class ConstGen implements com.sun.tools.corba.se.idl.ConstGen
  57 {
  58   /**
  59    * Public zero-argument constructor.
  60    **/
  61   public ConstGen ()
  62   {
  63   } // ctor
  64 
  65   /**
  66    * Generate Java code for an IDL constant.  A constant is written to
  67    * a new class only when it is not a member of an interface; otherwise
  68    * it written to the interface class in which it resides.
  69    **/
  70   public void generate (Hashtable symbolTable, ConstEntry c, PrintWriter s)
  71   {
  72     this.symbolTable = symbolTable;
  73     this.c           = c;
  74     this.stream      = s;
  75     init ();
  76 
  77     if (c.container () instanceof ModuleEntry)
  78       generateConst ();
  79     else if (stream != null)
  80       writeConstExpr ();
  81   } // generate
  82 
  83   /**
  84    * Initialize members unique to this generator.
  85    **/
  86   protected void init ()
  87   {
  88   } // init
  89 
  90   /**
  91    * Generate the class defining the constant.
  92    **/
  93   protected void generateConst ()
  94   {
  95     openStream ();
  96     if (stream == null)
  97       return;
  98     writeHeading ();
  99     writeBody ();
 100     writeClosing ();
 101     closeStream ();
 102   } // generateConst
 103 
 104   /**
 105    * Open a new print stream only if the constant is not a member
 106    * of an interface.
 107    **/
 108   protected void openStream ()
 109   {
 110     stream = Util.stream (c, ".java");
 111   } // openStream
 112 
 113   /**
 114    * Write the heading for the class defining the constant.
 115    **/
 116   protected void writeHeading ()
 117   {
 118     Util.writePackage (stream, c);
 119     Util.writeProlog (stream, ((GenFileStream)stream).name ());
 120     stream.println ("public interface " + c.name ());
 121         // should not be done according to the mapping
 122         // + " extends org.omg.CORBA.portable.IDLEntity");
 123     stream.println ("{");
 124   } // writeHeading
 125 
 126   /**
 127    * Write the constant expression and any comment, if present.
 128    **/
 129   protected void writeBody ()
 130   {
 131     writeConstExpr ();
 132   } // writeBody
 133 
 134   /**
 135    * Write the entire constant expression and any comment, if present.
 136    **/
 137   protected void writeConstExpr ()
 138   {
 139     if (c.comment () != null)
 140       c.comment ().generate ("  ", stream);
 141     if (c.container () instanceof ModuleEntry) {
 142 
 143       stream.print ("  public static final " + Util.javaName (c.type ()) + " value = ");
 144     } else {
 145       stream.print ("  public static final " + Util.javaName (c.type ()) + ' ' + c.name () + " = ");
 146     }
 147     writeConstValue (c.type ());
 148   } // writeConstExpr
 149 
 150   /**
 151    * Write the constant's value according to its type.
 152    **/
 153   private void writeConstValue (SymtabEntry type)
 154   {
 155     if (type instanceof PrimitiveEntry)
 156       stream.println ('(' + Util.javaName (type) + ")(" + Util.parseExpression (c.value ()) + ");");
 157     else if (type instanceof StringEntry)
 158       stream.println (Util.parseExpression (c.value ()) + ';');
 159     else if (type instanceof TypedefEntry)
 160     {
 161       while (type instanceof TypedefEntry)
 162         type = type.type ();
 163       writeConstValue (type);
 164     }
 165     else
 166       stream.println (Util.parseExpression (c.value ()) + ';');
 167   } // writeValue
 168 
 169   /**
 170    * Generate any last words and close the class.
 171    **/
 172   protected void writeClosing ()
 173   {
 174     stream.println ("}");
 175   } // writeClosing
 176 
 177   /**
 178    * Close the print stream, causing the file to be written.
 179    **/
 180   protected void closeStream ()
 181   {
 182     stream.close ();
 183   } // closeStream
 184 
 185   protected java.util.Hashtable  symbolTable = null;
 186   protected ConstEntry           c           = null;
 187   protected PrintWriter          stream      = null;
 188 } // class ConstGen
 189 
 190 
 191 /*=======================================================================================
 192   DATE-AUTHOR   ACTION
 193   ---------------------------------------------------------------------------------------
 194   11sep1997daz  Return when print stream is null and container is NOT a module. Fixes
 195                 -keep option, which causes null print stream to be sent to ConstGen.
 196   31jul1997daz  Write source comment immediately preceding constant declaration.
 197   =======================================================================================*/