duke@1: /* ohair@158: * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@158: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@158: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@158: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@158: * or visit www.oracle.com if you need additional information or have any ohair@158: * questions. duke@1: */ duke@1: /* duke@1: * Licensed Materials - Property of IBM duke@1: * RMI-IIOP v1.0 duke@1: * Copyright IBM Corp. 1998 1999 All Rights Reserved duke@1: * duke@1: */ duke@1: duke@1: package sun.rmi.rmic.iiop; duke@1: duke@1: import java.io.File; duke@1: import java.io.IOException; duke@1: import java.io.OutputStreamWriter; duke@1: import sun.tools.java.CompilerError; duke@1: import sun.tools.java.ClassDefinition; duke@1: import sun.rmi.rmic.IndentingWriter; duke@1: import sun.rmi.rmic.Main; duke@1: duke@1: /** duke@1: * An IDL generator for rmic. duke@1: * duke@1: * @author Bryan Atsatt duke@1: */ duke@1: public class PrintGenerator implements sun.rmi.rmic.Generator, duke@1: sun.rmi.rmic.iiop.Constants { duke@1: duke@1: private static final int JAVA = 0; duke@1: private static final int IDL = 1; duke@1: private static final int BOTH = 2; duke@1: duke@1: private int whatToPrint; // Initialized in parseArgs. duke@1: private boolean global = false; duke@1: private boolean qualified = false; duke@1: private boolean trace = false; duke@1: private boolean valueMethods = false; duke@1: duke@1: private IndentingWriter out; duke@1: duke@1: /** duke@1: * Default constructor for Main to use. duke@1: */ duke@1: public PrintGenerator() { duke@1: OutputStreamWriter writer = new OutputStreamWriter(System.out); duke@1: out = new IndentingWriter (writer); duke@1: } duke@1: duke@1: /** duke@1: * Examine and consume command line arguments. duke@1: * @param argv The command line arguments. Ignore null duke@1: * @param error Report any errors using the main.error() methods. duke@1: * @return true if no errors, false otherwise. duke@1: */ duke@1: public boolean parseArgs(String argv[], Main main) { duke@1: for (int i = 0; i < argv.length; i++) { duke@1: if (argv[i] != null) { duke@1: String arg = argv[i].toLowerCase(); duke@1: if (arg.equals("-xprint")) { duke@1: whatToPrint = JAVA; duke@1: argv[i] = null; duke@1: if (i+1 < argv.length) { duke@1: if (argv[i+1].equalsIgnoreCase("idl")) { duke@1: argv[++i] = null; duke@1: whatToPrint = IDL; duke@1: } else if (argv[i+1].equalsIgnoreCase("both")) { duke@1: argv[++i] = null; duke@1: whatToPrint = BOTH; duke@1: } duke@1: } duke@1: } else if (arg.equals("-xglobal")) { duke@1: global = true; duke@1: argv[i] = null; duke@1: } else if (arg.equals("-xqualified")) { duke@1: qualified = true; duke@1: argv[i] = null; duke@1: } else if (arg.equals("-xtrace")) { duke@1: trace = true; duke@1: argv[i] = null; duke@1: } else if (arg.equals("-xvaluemethods")) { duke@1: valueMethods = true; duke@1: argv[i] = null; duke@1: } duke@1: } duke@1: } duke@1: return true; duke@1: } duke@1: duke@1: /** duke@1: * Generate output. Any source files created which need compilation should duke@1: * be added to the compiler environment using the addGeneratedFile(File) duke@1: * method. duke@1: * duke@1: * @param env The compiler environment duke@1: * @param cdef The definition for the implementation class or interface from duke@1: * which to generate output duke@1: * @param destDir The directory for the root of the package hierarchy duke@1: * for generated files. May be null. duke@1: */ duke@1: public void generate(sun.rmi.rmic.BatchEnvironment env, ClassDefinition cdef, File destDir) { duke@1: duke@1: BatchEnvironment ourEnv = (BatchEnvironment) env; duke@1: ContextStack stack = new ContextStack(ourEnv); duke@1: stack.setTrace(trace); duke@1: duke@1: if (valueMethods) { duke@1: ourEnv.setParseNonConforming(true); duke@1: } duke@1: duke@1: // Get our top level type... duke@1: duke@1: CompoundType topType = CompoundType.forCompound(cdef,stack); duke@1: duke@1: if (topType != null) { duke@1: duke@1: try { duke@1: duke@1: // Collect up all the compound types... duke@1: duke@1: Type[] theTypes = topType.collectMatching(TM_COMPOUND); duke@1: duke@1: for (int i = 0; i < theTypes.length; i++) { duke@1: duke@1: out.pln("\n-----------------------------------------------------------\n"); duke@1: duke@1: Type theType = theTypes[i]; duke@1: duke@1: switch (whatToPrint) { duke@1: case JAVA: theType.println(out,qualified,false,false); duke@1: break; duke@1: duke@1: case IDL: theType.println(out,qualified,true,global); duke@1: break; duke@1: duke@1: case BOTH: theType.println(out,qualified,false,false); duke@1: theType.println(out,qualified,true,global); duke@1: break; duke@1: duke@1: default: throw new CompilerError("Unknown type!"); duke@1: } duke@1: } duke@1: duke@1: out.flush(); duke@1: duke@1: } catch (IOException e) { duke@1: throw new CompilerError("PrintGenerator caught " + e); duke@1: } duke@1: } duke@1: } duke@1: }