1 /*
   2  * Copyright (c) 1999, 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  * 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 // -09/23/98 <klr> Ported -td option to change output directory
  40 // -09/23/98 <klr> Ported -m option to generate make dependencies
  41 // -F46082.51<daz> Transferred -m, -mmin, mall, -mdepend options to com.sun.tools.corba.se.idl.toJava
  42 // since these are IBM-specific (see f46838); cleaned-out dead code.
  43 // -D57482   <klr> Added method setDefaultEmitter so could be overridden.
  44 // -F60858.1<daz> Set corba level to 2.3.
  45 
  46 import java.util.Enumeration;
  47 import java.util.Hashtable;
  48 import java.util.Properties;
  49 import java.util.Vector;
  50 import java.io.File;
  51 
  52 import com.sun.tools.corba.se.idl.InvalidArgument;
  53 
  54 /**
  55  *
  56  **/
  57 public class Arguments extends com.sun.tools.corba.se.idl.Arguments
  58 {
  59   /**
  60    * Public, zero-argument constructor.
  61    **/
  62   public Arguments ()
  63   {
  64     super ();
  65     corbaLevel = 2.4f;
  66   } // ctor
  67 
  68   /**
  69    *
  70    **/
  71   protected void parseOtherArgs (String[] args,
  72     Properties properties) throws InvalidArgument
  73   {
  74     String skeletonPattern = null ;
  75     String tiePattern = null ;
  76 
  77     // Get package prefixes from user's properties file.
  78     packages.put ("CORBA", "org.omg"); // klr - always needed
  79     packageFromProps (properties);
  80 
  81     // Now get package prefixes from command line (along with other args).
  82     // This order has the effect of making command line packages
  83     // supercede any idl.config file packages.
  84     try
  85     {
  86       Vector unknownArgs = new Vector ();
  87 
  88       // Process command line parameters
  89       for (int i = 0; i < args.length; ++i)
  90       {
  91         String lcArg = args[i].toLowerCase ();
  92 
  93         if (lcArg.charAt (0) != '-' && lcArg.charAt (0) != '/')
  94           throw new InvalidArgument (args[i]);
  95         if (lcArg.charAt (0) == '-' ) {
  96             lcArg = lcArg.substring (1);
  97         }
  98 
  99         // Proxy options; default is -fclient.
 100         if (lcArg.startsWith ("f"))
 101         {
 102           // If the command line had '-f client', make it '-fclient'
 103           if (lcArg.equals ("f"))
 104             lcArg = 'f' + args[++i].toLowerCase ();
 105 
 106           // Determine whether to emit bindings for client, server or both; and
 107           // whether to emit delegate-style (TIE) rather than derived-style
 108           // skeletons, which are the default.
 109 
 110           if (lcArg.equals ("fclient"))
 111           {
 112             emit = ((emit == Server || emit == All) ? All : Client);
 113           }
 114           else if (lcArg.equals ("fserver"))
 115           {
 116             emit = ((emit == Client || emit == All) ? All : Server);
 117             TIEServer = false;
 118           }
 119           else if (lcArg.equals ("fall"))
 120           {
 121             emit = All;
 122             TIEServer = false;
 123             //Should be removed and incorporated in the clause below
 124             //            POAServer = true;
 125           }
 126           else if (lcArg.equals ("fservertie"))
 127           {
 128             emit = ((emit == Client || emit == All) ? All : Server);
 129             TIEServer = true;
 130           }
 131           else if (lcArg.equals ("falltie"))
 132           {
 133             emit = All;
 134             TIEServer = true;
 135           }
 136           else
 137             i = collectUnknownArg (args, i, unknownArgs);
 138         }
 139         else if (lcArg.equals ("pkgtranslate"))
 140         {
 141           if (i + 2 >= args.length)
 142             throw new InvalidArgument( args[i] ) ;
 143 
 144           String orig = args[++i] ;
 145           String trans = args[++i] ;
 146           checkPackageNameValid( orig ) ;
 147           checkPackageNameValid( trans ) ;
 148           if (orig.equals( "org" ) || orig.startsWith( "org.omg" ))
 149               throw new InvalidArgument( args[i] ) ;
 150           orig = orig.replace( '.', '/' ) ;
 151           trans = trans.replace( '.', '/' ) ;
 152           packageTranslation.put( orig, trans ) ;
 153         }
 154         // Package prefix
 155         else if (lcArg.equals ("pkgprefix"))
 156         {
 157           if (i + 2 >= args.length)
 158             throw new InvalidArgument (args[i]);
 159 
 160           String type = args[++i];
 161           String pkg = args[++i];
 162           checkPackageNameValid( type ) ;
 163           checkPackageNameValid( pkg ) ;
 164           packages.put (type, pkg);
 165         }
 166         // Target directory
 167         else if (lcArg.equals ("td"))  // <f46838.4>
 168         {
 169           if (i + 1 >= args.length)
 170             throw new InvalidArgument (args[i]);
 171           String trgtDir = args[++i];
 172           if (trgtDir.charAt (0) == '-')
 173             throw new InvalidArgument (args[i - 1]);
 174           else
 175           {
 176             targetDir = trgtDir.replace ('/', File.separatorChar);
 177             if (targetDir.charAt (targetDir.length () - 1) != File.separatorChar)
 178               targetDir = targetDir + File.separatorChar;
 179           }
 180         }
 181         // Separator
 182         else if (lcArg.equals ("sep"))
 183         {
 184           if (i + 1 >= args.length)
 185             throw new InvalidArgument (args[i]);
 186           separator = args[++i];
 187         }
 188         // POA flag ?
 189         else if (lcArg.equals ("oldimplbase")){
 190             POAServer = false;
 191         }
 192         else if (lcArg.equals("skeletonname")){
 193           if (i + 1 >= args.length)
 194             throw new InvalidArgument (args[i]);
 195           skeletonPattern = args[++i];
 196         }
 197         else if (lcArg.equals("tiename")){
 198           if (i + 1 >= args.length)
 199             throw new InvalidArgument (args[i]);
 200           tiePattern = args[++i];
 201         }
 202         else if (lcArg.equals("localoptimization")) {
 203             LocalOptimization = true;
 204         }
 205         else i = collectUnknownArg (args, i, unknownArgs);
 206       }
 207 
 208       // Encountered unknown arguments?
 209       if (unknownArgs.size () > 0)
 210       {
 211         String [] otherArgs = new String [unknownArgs.size ()];
 212         unknownArgs.copyInto (otherArgs);
 213         // Throws InvalidArgument by default
 214         super.parseOtherArgs (otherArgs, properties);
 215       }
 216 
 217       setDefaultEmitter(); // d57482 <klr>
 218       setNameModifiers( skeletonPattern, tiePattern ) ;
 219     }
 220     catch (ArrayIndexOutOfBoundsException e)
 221     {
 222       // If there is any array indexing problem, it is probably
 223       // because the qualifier on the last argument is missing.
 224       // Report that this last argument is invalid.
 225       throw new InvalidArgument (args[args.length - 1]);
 226     }
 227   } // parseOtherArgs
 228 
 229   /**
 230    *
 231    **/
 232   protected int collectUnknownArg (String[] args, int i, Vector unknownArgs)
 233   {
 234     unknownArgs.addElement (args [i]);
 235     ++i;
 236     while (i < args.length && args[i].charAt (0) != '-' && args[i].charAt (0) != '/')
 237       unknownArgs.addElement (args[i++]);
 238     return --i;
 239   } // collectUnknownArg
 240 
 241   /**
 242    *
 243    **/
 244   // XXX Either generalize this facility or remove it completely.
 245   protected void packageFromProps (Properties props) throws InvalidArgument
 246   {
 247     Enumeration propsEnum = props.propertyNames ();
 248     while (propsEnum.hasMoreElements ())
 249     {
 250       String prop = (String)propsEnum.nextElement ();
 251       if (prop.startsWith ("PkgPrefix."))
 252       {
 253         String type = prop.substring (10);
 254         String pkg = props.getProperty (prop);
 255         checkPackageNameValid( pkg ) ;
 256         checkPackageNameValid( type ) ;
 257         packages.put (type, pkg);
 258       }
 259     }
 260   } // packageFromProps
 261 
 262   /**
 263    * d57482 (klr) method added so default emitter check could be overriden.
 264    **/
 265   protected void setDefaultEmitter () {
 266       // If the flag -fclient was not found, assume it.
 267       if (emit == None) emit = Client;
 268   }
 269 
 270   protected void setNameModifiers( String skeletonPattern,
 271     String tiePattern ) {
 272     if (emit>Client) {
 273         String tp ;
 274         String sp ;
 275 
 276         if (skeletonPattern != null)
 277             sp = skeletonPattern ;
 278         else if (POAServer)
 279             sp = "%POA" ;
 280         else
 281             sp = "_%ImplBase" ;
 282 
 283         if (tiePattern != null)
 284             tp = tiePattern ;
 285         else if (POAServer)
 286             tp = "%POATie" ;
 287         else
 288             tp = "%_Tie" ;
 289 
 290         skeletonNameModifier = new NameModifierImpl( sp ) ;
 291         tieNameModifier = new NameModifierImpl( tp ) ;
 292     }
 293   }
 294 
 295   /**
 296    *
 297    **/
 298   private void checkPackageNameValid (String name) throws InvalidArgument
 299   {
 300     if (name.charAt (0) == '.')
 301       throw new InvalidArgument (name);
 302     for (int i = 0; i < name.length ();++i)
 303       if (name.charAt (i) == '.')
 304       {
 305         if (i == name.length () - 1 || !Character.isJavaIdentifierStart (name.charAt (++i)))
 306           throw new InvalidArgument (name);
 307       }
 308       else if (!Character.isJavaIdentifierPart (name.charAt (i)))
 309         throw new InvalidArgument (name);
 310   } // validatePackageName
 311 
 312   // <46082.03><46838> Modified access restrictions from protected to public.
 313 
 314   // This is a hash table whose keys are top-level typenames and
 315   // whose values are the package prefixes to those types.
 316   // For instance, <"CORBA", "org.omg"> is a possible entry.
 317   public Hashtable packages         = new Hashtable ();
 318 
 319   public    String separator        = null;
 320 
 321   public static final int
 322     None   = 0,
 323     Client = 1,
 324     Server = 2,
 325     All    = 3;
 326   public int       emit              = None;
 327   public boolean   TIEServer         = false;
 328   public boolean   POAServer         = true;
 329   // By default we do not generate Locally Optimized stub because of an
 330   // unresolved PI problem. We will generate only if -localOptimization flag
 331   // is passed
 332   public boolean   LocalOptimization = false;
 333   public NameModifier skeletonNameModifier   = null ;
 334   public NameModifier tieNameModifier   = null ;
 335 
 336   // Key is original package name; value is translated package name.
 337   // Note that this translation happens AFTER prefixes are added in the
 338   // packages table.
 339   public Hashtable packageTranslation = new Hashtable() ;
 340 
 341   public String    targetDir        = "";     // <f46838.4>
 342 } // class Arguments