Arguments.java revision 672:2bb058ce572e
11573Srgrimes/*
21573Srgrimes * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
31573Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
41573Srgrimes *
51573Srgrimes * This code is free software; you can redistribute it and/or modify it
61573Srgrimes * under the terms of the GNU General Public License version 2 only, as
71573Srgrimes * published by the Free Software Foundation.  Oracle designates this
81573Srgrimes * particular file as subject to the "Classpath" exception as provided
91573Srgrimes * by Oracle in the LICENSE file that accompanied this code.
101573Srgrimes *
111573Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
121573Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
131573Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
141573Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
151573Srgrimes * accompanied this code).
161573Srgrimes *
171573Srgrimes * You should have received a copy of the GNU General Public License version
181573Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
191573Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
201573Srgrimes *
211573Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
221573Srgrimes * or visit www.oracle.com if you need additional information or have any
231573Srgrimes * questions.
241573Srgrimes */
251573Srgrimes/*
261573Srgrimes * COMPONENT_NAME: idl.toJava
271573Srgrimes *
281573Srgrimes * ORIGINS: 27
2950476Speter *
301573Srgrimes * Licensed Materials - Property of IBM
31139434Skrion * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
321573Srgrimes * RMI-IIOP v1.0
3379531Sru *
341573Srgrimes */
351573Srgrimes
361573Srgrimespackage com.sun.tools.corba.se.idl.toJavaPortable;
371573Srgrimes
381573Srgrimes// NOTES:
391573Srgrimes// -09/23/98 <klr> Ported -td option to change output directory
401573Srgrimes// -09/23/98 <klr> Ported -m option to generate make dependencies
4159460Sphantom// -F46082.51<daz> Transferred -m, -mmin, mall, -mdepend options to com.sun.tools.corba.se.idl.toJava
4259460Sphantom// since these are IBM-specific (see f46838); cleaned-out dead code.
431573Srgrimes// -D57482   <klr> Added method setDefaultEmitter so could be overridden.
4484306Sru// -F60858.1<daz> Set corba level to 2.3.
4584306Sru
461573Srgrimesimport java.util.Enumeration;
471573Srgrimesimport java.util.Hashtable;
481573Srgrimesimport java.util.Properties;
491573Srgrimesimport java.util.Vector;
501573Srgrimesimport java.io.File;
511573Srgrimes
521573Srgrimesimport com.sun.tools.corba.se.idl.InvalidArgument;
531573Srgrimes
541573Srgrimes/**
551573Srgrimes *
561573Srgrimes **/
571573Srgrimespublic class Arguments extends com.sun.tools.corba.se.idl.Arguments
581573Srgrimes{
591573Srgrimes  /**
601573Srgrimes   * Public, zero-argument constructor.
611573Srgrimes   **/
621573Srgrimes  public Arguments ()
631573Srgrimes  {
6457695Ssheldonh    super ();
6557695Ssheldonh    corbaLevel = 2.4f;
661573Srgrimes  } // ctor
671573Srgrimes
681573Srgrimes  /**
691573Srgrimes   *
701573Srgrimes   **/
711573Srgrimes  protected void parseOtherArgs (String[] args,
721573Srgrimes    Properties properties) throws InvalidArgument
7357695Ssheldonh  {
7457695Ssheldonh    String skeletonPattern = null ;
751573Srgrimes    String tiePattern = null ;
761573Srgrimes
771573Srgrimes    // Get package prefixes from user's properties file.
781573Srgrimes    packages.put ("CORBA", "org.omg"); // klr - always needed
791573Srgrimes    packageFromProps (properties);
801573Srgrimes
811573Srgrimes    // Now get package prefixes from command line (along with other args).
821573Srgrimes    // This order has the effect of making command line packages
831573Srgrimes    // supercede any idl.config file packages.
841573Srgrimes    try
8581149Sdd    {
861573Srgrimes      Vector unknownArgs = new Vector ();
871573Srgrimes
881573Srgrimes      // Process command line parameters
891573Srgrimes      for (int i = 0; i < args.length; ++i)
901573Srgrimes      {
911573Srgrimes        String lcArg = args[i].toLowerCase ();
921573Srgrimes
931573Srgrimes        if (lcArg.charAt (0) != '-' && lcArg.charAt (0) != '/')
941573Srgrimes          throw new InvalidArgument (args[i]);
951573Srgrimes        if (lcArg.charAt (0) == '-' ) {
961573Srgrimes            lcArg = lcArg.substring (1);
971573Srgrimes        }
981573Srgrimes
991573Srgrimes        // Proxy options; default is -fclient.
1001573Srgrimes        if (lcArg.startsWith ("f"))
1011573Srgrimes        {
1021573Srgrimes          // If the command line had '-f client', make it '-fclient'
1031573Srgrimes          if (lcArg.equals ("f"))
1041573Srgrimes            lcArg = 'f' + args[++i].toLowerCase ();
1051573Srgrimes
1061573Srgrimes          // Determine whether to emit bindings for client, server or both; and
1071573Srgrimes          // whether to emit delegate-style (TIE) rather than derived-style
1081573Srgrimes          // skeletons, which are the default.
1091573Srgrimes
1101573Srgrimes          if (lcArg.equals ("fclient"))
1111573Srgrimes          {
1121573Srgrimes            emit = ((emit == Server || emit == All) ? All : Client);
1131573Srgrimes          }
1141573Srgrimes          else if (lcArg.equals ("fserver"))
1151573Srgrimes          {
1161573Srgrimes            emit = ((emit == Client || emit == All) ? All : Server);
1171573Srgrimes            TIEServer = false;
1181573Srgrimes          }
1191573Srgrimes          else if (lcArg.equals ("fall"))
1201573Srgrimes          {
1211573Srgrimes            emit = All;
1221573Srgrimes            TIEServer = false;
1231573Srgrimes            //Should be removed and incorporated in the clause below
1241573Srgrimes            //            POAServer = true;
1251573Srgrimes          }
1261573Srgrimes          else if (lcArg.equals ("fservertie"))
1271573Srgrimes          {
1281573Srgrimes            emit = ((emit == Client || emit == All) ? All : Server);
129108087Sru            TIEServer = true;
1301573Srgrimes          }
131108087Sru          else if (lcArg.equals ("falltie"))
1321573Srgrimes          {
1331573Srgrimes            emit = All;
1341573Srgrimes            TIEServer = true;
1351573Srgrimes          }
1361573Srgrimes          else
1371573Srgrimes            i = collectUnknownArg (args, i, unknownArgs);
1381573Srgrimes        }
1391573Srgrimes        else if (lcArg.equals ("pkgtranslate"))
1401573Srgrimes        {
1411573Srgrimes          if (i + 2 >= args.length)
1421573Srgrimes            throw new InvalidArgument( args[i] ) ;
1431573Srgrimes
14414855Smpp          String orig = args[++i] ;
1451573Srgrimes          String trans = args[++i] ;
14671895Sru          checkPackageNameValid( orig ) ;
14767967Sasmodai          checkPackageNameValid( trans ) ;
1481573Srgrimes          if (orig.equals( "org" ) || orig.startsWith( "org.omg" ))
14914855Smpp              throw new InvalidArgument( args[i] ) ;
1501573Srgrimes          orig = orig.replace( '.', '/' ) ;
1511573Srgrimes          trans = trans.replace( '.', '/' ) ;
1521573Srgrimes          packageTranslation.put( orig, trans ) ;
1531573Srgrimes        }
1541573Srgrimes        // Package prefix
1551573Srgrimes        else if (lcArg.equals ("pkgprefix"))
1561573Srgrimes        {
1571573Srgrimes          if (i + 2 >= args.length)
1581573Srgrimes            throw new InvalidArgument (args[i]);
1591573Srgrimes
1601573Srgrimes          String type = args[++i];
1611573Srgrimes          String pkg = args[++i];
1621573Srgrimes          checkPackageNameValid( type ) ;
163108087Sru          checkPackageNameValid( pkg ) ;
1641573Srgrimes          packages.put (type, pkg);
1651573Srgrimes        }
1661573Srgrimes        // Target directory
1671573Srgrimes        else if (lcArg.equals ("td"))  // <f46838.4>
1681573Srgrimes        {
1691573Srgrimes          if (i + 1 >= args.length)
1701573Srgrimes            throw new InvalidArgument (args[i]);
1711573Srgrimes          String trgtDir = args[++i];
1721573Srgrimes          if (trgtDir.charAt (0) == '-')
1731573Srgrimes            throw new InvalidArgument (args[i - 1]);
1741573Srgrimes          else
1751573Srgrimes          {
1761573Srgrimes            targetDir = trgtDir.replace ('/', File.separatorChar);
17774875Scwt            if (targetDir.charAt (targetDir.length () - 1) != File.separatorChar)
17875158Sru              targetDir = targetDir + File.separatorChar;
17975158Sru          }
18075158Sru        }
1811573Srgrimes        // Separator
18215874Smpp        else if (lcArg.equals ("sep"))
18315874Smpp        {
1841573Srgrimes          if (i + 1 >= args.length)
1851573Srgrimes            throw new InvalidArgument (args[i]);
1861573Srgrimes          separator = args[++i];
1871573Srgrimes        }
18815874Smpp        // POA flag ?
18936664Sjkoshy        else if (lcArg.equals ("oldimplbase")){
19036664Sjkoshy            POAServer = false;
19136664Sjkoshy        }
1921573Srgrimes        else if (lcArg.equals("skeletonname")){
1931573Srgrimes          if (i + 1 >= args.length)
1941573Srgrimes            throw new InvalidArgument (args[i]);
1951573Srgrimes          skeletonPattern = args[++i];
1961573Srgrimes        }
1971573Srgrimes        else if (lcArg.equals("tiename")){
1981573Srgrimes          if (i + 1 >= args.length)
1991573Srgrimes            throw new InvalidArgument (args[i]);
2001573Srgrimes          tiePattern = args[++i];
2011573Srgrimes        }
2021573Srgrimes        else if (lcArg.equals("localoptimization")) {
2031573Srgrimes            LocalOptimization = true;
2041573Srgrimes        }
205139434Skrion        else i = collectUnknownArg (args, i, unknownArgs);
206139434Skrion      }
20752474Sobrien
20852474Sobrien      // Encountered unknown arguments?
20969662Sru      if (unknownArgs.size () > 0)
2101573Srgrimes      {
2111573Srgrimes        String [] otherArgs = new String [unknownArgs.size ()];
2121573Srgrimes        unknownArgs.copyInto (otherArgs);
2131573Srgrimes        // Throws InvalidArgument by default
2141573Srgrimes        super.parseOtherArgs (otherArgs, properties);
2151573Srgrimes      }
2161573Srgrimes
2171573Srgrimes      setDefaultEmitter(); // d57482 <klr>
2181573Srgrimes      setNameModifiers( skeletonPattern, tiePattern ) ;
2191573Srgrimes    }
2201573Srgrimes    catch (ArrayIndexOutOfBoundsException e)
2211573Srgrimes    {
2221573Srgrimes      // If there is any array indexing problem, it is probably
2231573Srgrimes      // because the qualifier on the last argument is missing.
22467967Sasmodai      // Report that this last argument is invalid.
2251573Srgrimes      throw new InvalidArgument (args[args.length - 1]);
2261573Srgrimes    }
2271573Srgrimes  } // parseOtherArgs
2281573Srgrimes
2291573Srgrimes  /**
2301573Srgrimes   *
2311573Srgrimes   **/
2321573Srgrimes  protected int collectUnknownArg (String[] args, int i, Vector unknownArgs)
2331573Srgrimes  {
2341573Srgrimes    unknownArgs.addElement (args [i]);
2351573Srgrimes    ++i;
2361573Srgrimes    while (i < args.length && args[i].charAt (0) != '-' && args[i].charAt (0) != '/')
2371573Srgrimes      unknownArgs.addElement (args[i++]);
2381573Srgrimes    return --i;
2391573Srgrimes  } // collectUnknownArg
2401573Srgrimes
2411573Srgrimes  /**
2421573Srgrimes   *
2431573Srgrimes   **/
2441573Srgrimes  // XXX Either generalize this facility or remove it completely.
2451573Srgrimes  protected void packageFromProps (Properties props) throws InvalidArgument
2461573Srgrimes  {
2471573Srgrimes    Enumeration propsEnum = props.propertyNames ();
2481573Srgrimes    while (propsEnum.hasMoreElements ())
2491573Srgrimes    {
2501573Srgrimes      String prop = (String)propsEnum.nextElement ();
2511573Srgrimes      if (prop.startsWith ("PkgPrefix."))
2521573Srgrimes      {
2531573Srgrimes        String type = prop.substring (10);
2541573Srgrimes        String pkg = props.getProperty (prop);
2551573Srgrimes        checkPackageNameValid( pkg ) ;
2561573Srgrimes        checkPackageNameValid( type ) ;
2571573Srgrimes        packages.put (type, pkg);
2581573Srgrimes      }
2591573Srgrimes    }
2601573Srgrimes  } // packageFromProps
2611573Srgrimes
2621573Srgrimes  /**
2631573Srgrimes   * d57482 (klr) method added so default emitter check could be overriden.
2641573Srgrimes   **/
2651573Srgrimes  protected void setDefaultEmitter () {
2661573Srgrimes      // If the flag -fclient was not found, assume it.
26715874Smpp      if (emit == None) emit = Client;
26815874Smpp  }
2691573Srgrimes
27015874Smpp  protected void setNameModifiers( String skeletonPattern,
2711573Srgrimes    String tiePattern ) {
2721573Srgrimes    if (emit>Client) {
2731573Srgrimes        String tp ;
2741573Srgrimes        String sp ;
2751573Srgrimes
2761573Srgrimes        if (skeletonPattern != null)
2771573Srgrimes            sp = skeletonPattern ;
2781573Srgrimes        else if (POAServer)
2791573Srgrimes            sp = "%POA" ;
28067967Sasmodai        else
2811573Srgrimes            sp = "_%ImplBase" ;
28277200Seric
28377200Seric        if (tiePattern != null)
28477200Seric            tp = tiePattern ;
28579754Sdd        else if (POAServer)
28677200Seric            tp = "%POATie" ;
28777200Seric        else
28877200Seric            tp = "%_Tie" ;
28977200Seric
29077200Seric        skeletonNameModifier = new NameModifierImpl( sp ) ;
29177200Seric        tieNameModifier = new NameModifierImpl( tp ) ;
29277200Seric    }
29377200Seric  }
29477200Seric
29577200Seric  /**
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
343