1 /*
   2  * Copyright (c) 1999, 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 // -D62023 klr new class
  40 
  41 import java.io.PrintWriter;
  42 import java.util.Vector;
  43 
  44 import com.sun.tools.corba.se.idl.GenFileStream;
  45 import com.sun.tools.corba.se.idl.SymtabEntry;
  46 import com.sun.tools.corba.se.idl.MethodEntry;
  47 import com.sun.tools.corba.se.idl.ValueEntry;
  48 
  49 /**
  50  *
  51  **/
  52 public class DefaultFactory implements AuxGen
  53 {
  54   /**
  55    * Public zero-argument constructor.
  56    **/
  57   public DefaultFactory ()
  58   {
  59   } // ctor
  60 
  61   /**
  62    * Generate the default value factory class. Provides general algorithm for
  63    * auxiliary binding generation:
  64    * 1.) Initialize symbol table and symbol table entry members,
  65    *     common to all generators.
  66    * 2.) Initialize members unique to this generator.
  67    * 3.) Open print stream
  68    * 4.) Write class heading (package, prologue, source comment, class
  69    *     statement, open curly
  70    * 5.) Write class body (member data and methods)
  71    * 6.) Write class closing (close curly)
  72    * 7.) Close the print stream
  73    **/
  74   public void generate (java.util.Hashtable symbolTable, com.sun.tools.corba.se.idl.SymtabEntry entry)
  75   {
  76     this.symbolTable = symbolTable;
  77     this.entry       = entry;
  78     init ();
  79     openStream ();
  80     if (stream == null)
  81       return;
  82     writeHeading ();
  83     writeBody ();
  84     writeClosing ();
  85     closeStream ();
  86   } // generate
  87 
  88   /**
  89    * Initialize variables unique to this generator.
  90    **/
  91   protected void init ()
  92   {
  93     factoryClass = entry.name () + "DefaultFactory";
  94     factoryInterface = entry.name () + "ValueFactory";
  95     factoryType = Util.javaName (entry);
  96     implType = entry.name () + "Impl"; // default implementation class
  97   } // init
  98 
  99   /**
 100    * @return true if entry has any factory methods declared
 101    **/
 102   protected boolean hasFactoryMethods ()
 103   {
 104     Vector init = ((ValueEntry)entry).initializers ();
 105     if (init != null && init.size () > 0)
 106       return true;
 107     else
 108       return false;
 109   } // hasFactoryMethods
 110 
 111   /**
 112    * Open the print stream for subsequent output.
 113    **/
 114   protected void openStream ()
 115   {
 116     stream = Util.stream (entry, "DefaultFactory.java");
 117   } // openStream
 118 
 119   /**
 120    * Generate the heading, including the package, imports,
 121    * source comment, class statement, and left curly.
 122    **/
 123   protected void writeHeading ()
 124   {
 125     Util.writePackage (stream, entry, Util.TypeFile); // REVISIT - same as interface?
 126     Util.writeProlog (stream, stream.name ());
 127     if (entry.comment () != null)
 128       entry.comment ().generate ("", stream);
 129     stream.print ("public class " + factoryClass + " implements ");
 130     if (hasFactoryMethods ())
 131         stream.print (factoryInterface);
 132     else
 133         stream.print ("org.omg.CORBA.portable.ValueFactory");
 134     stream.println (" {");
 135   } // writeHeading
 136 
 137   /**
 138    * Generate the contents of this class
 139    **/
 140   protected void writeBody ()
 141   {
 142     writeFactoryMethods ();
 143     stream.println ();
 144     writeReadValue ();
 145   } // writeBody
 146 
 147   /**
 148    * Generate members of this class.
 149    **/
 150   protected void writeFactoryMethods ()
 151   {
 152     Vector init = ((ValueEntry)entry).initializers ();
 153     if (init != null)
 154     {
 155       for (int i = 0; i < init.size (); i++)
 156       {
 157         MethodEntry element = (MethodEntry) init.elementAt (i);
 158         element.valueMethod (true); //tag value method if not tagged previously
 159         ((MethodGen24) element.generator ()).defaultFactoryMethod (symbolTable, element, stream);
 160       }
 161     }
 162   } // writeFactoryMethods
 163 
 164   /**
 165    * Generate default read_value
 166    **/
 167   protected void writeReadValue ()
 168   {
 169      stream.println ("  public java.io.Serializable read_value (org.omg.CORBA_2_3.portable.InputStream is)");
 170      stream.println ("  {");
 171      stream.println ("    return is.read_value(new " + implType + " ());");
 172      stream.println ("  }");
 173   } // writeReadValue
 174 
 175   /**
 176    * Generate the closing statements.
 177    **/
 178   protected void writeClosing ()
 179   {
 180     stream.println ('}');
 181   } // writeClosing
 182 
 183   /**
 184    * Write the stream to file by closing the print stream.
 185    **/
 186   protected void closeStream ()
 187   {
 188     stream.close ();
 189   } // closeStream
 190 
 191   protected java.util.Hashtable     symbolTable;
 192   protected com.sun.tools.corba.se.idl.SymtabEntry entry;
 193   protected GenFileStream           stream;
 194 
 195   // Unique to this generator
 196   protected String factoryClass;
 197   protected String factoryInterface;
 198   protected String factoryType;
 199   protected String implType;
 200 } // class Holder