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 /*
  27  * Licensed Materials - Property of IBM
  28  * RMI-IIOP v1.0
  29  * Copyright IBM Corp. 1998 1999  All Rights Reserved
  30  *
  31  */
  32 
  33 package sun.rmi.rmic.iiop;
  34 
  35 import java.io.File;
  36 import sun.tools.java.Identifier;
  37 
  38 import com.sun.corba.se.impl.util.PackagePrefixChecker;
  39 
  40 /**
  41  * Util provides static utility methods used by other rmic classes.
  42  * @author Bryan Atsatt
  43  */
  44 
  45 public final class Util implements sun.rmi.rmic.Constants {
  46 
  47 
  48     public static String packagePrefix(){ return PackagePrefixChecker.packagePrefix();}
  49 
  50 
  51     /**
  52      * Return the directory that should be used for output for a given
  53      * class.
  54      * @param theClass The fully qualified name of the class.
  55      * @param rootDir The directory to use as the root of the
  56      * package heirarchy.  May be null, in which case the current
  57      * working directory is used as the root.
  58      */
  59     private static File getOutputDirectoryFor(Identifier theClass,
  60                                              File rootDir,
  61                                              BatchEnvironment env,
  62                                              boolean idl ) {
  63         File outputDir = null;
  64         String className = theClass.getFlatName().toString().replace('.', SIGC_INNERCLASS);
  65         String qualifiedClassName = className;
  66         String packagePath = null;
  67         String packageName = theClass.getQualifier().toString();
  68         //Shift package names for stubs generated for interfaces.
  69         /*if(type.isInterface())*/
  70         packageName = correctPackageName(packageName, idl, env.getStandardPackage());
  71         //Done.
  72         if (packageName.length() > 0) {
  73             qualifiedClassName = packageName + "." + className;
  74             packagePath = packageName.replace('.', File.separatorChar);
  75         }
  76 
  77         // Do we have a root directory?
  78 
  79         if (rootDir != null) {
  80 
  81             // Yes, do we have a package name?
  82 
  83             if (packagePath != null) {
  84 
  85                 // Yes, so use it as the root. Open the directory...
  86 
  87                 outputDir = new File(rootDir, packagePath);
  88 
  89                 // Make sure the directory exists...
  90 
  91                 ensureDirectory(outputDir,env);
  92 
  93             } else {
  94 
  95                 // Default package, so use root as output dir...
  96 
  97                 outputDir = rootDir;
  98             }
  99         } else {
 100 
 101             // No root directory. Get the current working directory...
 102 
 103             String workingDirPath = System.getProperty("user.dir");
 104             File workingDir = new File(workingDirPath);
 105 
 106             // Do we have a package name?
 107 
 108             if (packagePath == null) {
 109 
 110                 // No, so use working directory...
 111 
 112                 outputDir = workingDir;
 113 
 114             } else {
 115 
 116                 // Yes, so use working directory as the root...
 117 
 118                 outputDir = new File(workingDir, packagePath);
 119 
 120                 // Make sure the directory exists...
 121 
 122                 ensureDirectory(outputDir,env);
 123             }
 124         }
 125 
 126         // Finally, return the directory...
 127 
 128         return outputDir;
 129     }
 130 
 131     public static File getOutputDirectoryForIDL(Identifier theClass,
 132                                              File rootDir,
 133                                              BatchEnvironment env) {
 134         return getOutputDirectoryFor(theClass, rootDir, env, true);
 135     }
 136 
 137     public static File getOutputDirectoryForStub(Identifier theClass,
 138                                              File rootDir,
 139                                              BatchEnvironment env) {
 140         return getOutputDirectoryFor(theClass, rootDir, env, false);
 141     }
 142 
 143     private static void ensureDirectory (File dir, BatchEnvironment env) {
 144         if (!dir.exists()) {
 145             dir.mkdirs();
 146             if (!dir.exists()) {
 147                 env.error(0,"rmic.cannot.create.dir",dir.getAbsolutePath());
 148                 throw new InternalError();
 149             }
 150         }
 151     }
 152 
 153     public static String correctPackageName(
 154             String p, boolean idl, boolean standardPackage){
 155         if (idl){
 156             return p;
 157         } else {
 158             if (standardPackage) {
 159                 return p;
 160             } else {
 161               return PackagePrefixChecker.correctPackageName(p);
 162             }
 163         }
 164     }
 165 
 166     public static boolean isOffendingPackage(String p){
 167         return PackagePrefixChecker.isOffendingPackage(p);
 168     }
 169 
 170     public static boolean hasOffendingPrefix(String p){
 171         return PackagePrefixChecker.hasOffendingPrefix(p);
 172     }
 173 
 174 }