1 /*
   2  * Copyright (c) 1998, 2007, 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  * Licensed Materials - Property of IBM
  27  * RMI-IIOP v1.0
  28  * Copyright IBM Corp. 1998 1999  All Rights Reserved
  29  *
  30  */
  31 
  32 package sun.rmi.rmic.iiop;
  33 
  34 import java.io.File;
  35 import java.io.IOException;
  36 import java.io.OutputStreamWriter;
  37 import sun.tools.java.CompilerError;
  38 import sun.tools.java.ClassDefinition;
  39 import sun.rmi.rmic.IndentingWriter;
  40 import sun.rmi.rmic.Main;
  41 
  42 /**
  43  * An IDL generator for rmic.
  44  *
  45  * @author      Bryan Atsatt
  46  */
  47 public class PrintGenerator implements sun.rmi.rmic.Generator,
  48                                        sun.rmi.rmic.iiop.Constants {
  49 
  50     private static final int JAVA = 0;
  51     private static final int IDL = 1;
  52     private static final int BOTH = 2;
  53 
  54     private int whatToPrint; // Initialized in parseArgs.
  55     private boolean global = false;
  56     private boolean qualified = false;
  57     private boolean trace = false;
  58     private boolean valueMethods = false;
  59 
  60     private IndentingWriter out;
  61 
  62     /**
  63      * Default constructor for Main to use.
  64      */
  65     public PrintGenerator() {
  66         OutputStreamWriter writer = new OutputStreamWriter(System.out);
  67         out = new IndentingWriter (writer);
  68     }
  69 
  70     /**
  71      * Examine and consume command line arguments.
  72      * @param argv The command line arguments. Ignore null
  73      * @param error Report any errors using the main.error() methods.
  74      * @return true if no errors, false otherwise.
  75      */
  76     public boolean parseArgs(String argv[], Main main) {
  77         for (int i = 0; i < argv.length; i++) {
  78             if (argv[i] != null) {
  79                 String arg = argv[i].toLowerCase();
  80                 if (arg.equals("-xprint")) {
  81                     whatToPrint = JAVA;
  82                     argv[i] = null;
  83                     if (i+1 < argv.length) {
  84                         if (argv[i+1].equalsIgnoreCase("idl")) {
  85                             argv[++i] = null;
  86                             whatToPrint = IDL;
  87                         } else if (argv[i+1].equalsIgnoreCase("both")) {
  88                             argv[++i] = null;
  89                             whatToPrint = BOTH;
  90                         }
  91                     }
  92                 } else if (arg.equals("-xglobal")) {
  93                     global = true;
  94                     argv[i] = null;
  95                 } else if (arg.equals("-xqualified")) {
  96                     qualified = true;
  97                     argv[i] = null;
  98                 } else if (arg.equals("-xtrace")) {
  99                     trace = true;
 100                     argv[i] = null;
 101                 } else if (arg.equals("-xvaluemethods")) {
 102                     valueMethods = true;
 103                     argv[i] = null;
 104                 }
 105             }
 106         }
 107         return true;
 108     }
 109 
 110     /**
 111      * Generate output. Any source files created which need compilation should
 112      * be added to the compiler environment using the addGeneratedFile(File)
 113      * method.
 114      *
 115      * @param env       The compiler environment
 116      * @param cdef      The definition for the implementation class or interface from
 117      *              which to generate output
 118      * @param destDir   The directory for the root of the package hierarchy
 119      *                          for generated files. May be null.
 120      */
 121     public void generate(sun.rmi.rmic.BatchEnvironment env, ClassDefinition cdef, File destDir) {
 122 
 123         BatchEnvironment ourEnv = (BatchEnvironment) env;
 124         ContextStack stack = new ContextStack(ourEnv);
 125         stack.setTrace(trace);
 126 
 127         if (valueMethods) {
 128             ourEnv.setParseNonConforming(true);
 129         }
 130 
 131         // Get our top level type...
 132 
 133         CompoundType topType = CompoundType.forCompound(cdef,stack);
 134 
 135         if (topType != null) {
 136 
 137             try {
 138 
 139                                 // Collect up all the compound types...
 140 
 141                 Type[] theTypes = topType.collectMatching(TM_COMPOUND);
 142 
 143                 for (int i = 0; i < theTypes.length; i++) {
 144 
 145                     out.pln("\n-----------------------------------------------------------\n");
 146 
 147                     Type theType = theTypes[i];
 148 
 149                     switch (whatToPrint) {
 150                     case JAVA:  theType.println(out,qualified,false,false);
 151                         break;
 152 
 153                     case IDL:   theType.println(out,qualified,true,global);
 154                         break;
 155 
 156                     case BOTH:  theType.println(out,qualified,false,false);
 157                         theType.println(out,qualified,true,global);
 158                         break;
 159 
 160                     default:    throw new CompilerError("Unknown type!");
 161                     }
 162                 }
 163 
 164                 out.flush();
 165 
 166             } catch (IOException e) {
 167                 throw new CompilerError("PrintGenerator caught " + e);
 168             }
 169         }
 170     }
 171 }