1/*
2 * Copyright (c) 1999, 2013, 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
33package sun.rmi.rmic;
34
35import java.io.File;
36import sun.tools.java.Identifier;
37
38/**
39 * Util provides static utility methods used by other rmic classes.
40 *
41 * WARNING: The contents of this source file are not part of any
42 * supported API.  Code that depends on them does so at its own risk:
43 * they are subject to change or removal without notice.
44 *
45 * @author Bryan Atsatt
46 */
47
48public class Util implements sun.rmi.rmic.Constants {
49
50    /**
51     * Return the directory that should be used for output for a given
52     * class.
53     * @param theClass The fully qualified name of the class.
54     * @param rootDir The directory to use as the root of the
55     * package hierarchy.  May be null, in which case the current
56     * working directory is used as the root.
57     */
58    public static File getOutputDirectoryFor(Identifier theClass,
59                                             File rootDir,
60                                             BatchEnvironment env) {
61
62        File outputDir = null;
63        String className = theClass.getFlatName().toString().replace('.', SIGC_INNERCLASS);
64        String qualifiedClassName = className;
65        String packagePath = null;
66        String packageName = theClass.getQualifier().toString();
67
68        if (packageName.length() > 0) {
69            qualifiedClassName = packageName + "." + className;
70            packagePath = packageName.replace('.', File.separatorChar);
71        }
72
73        // Do we have a root directory?
74
75        if (rootDir != null) {
76
77            // Yes, do we have a package name?
78
79            if (packagePath != null) {
80
81                // Yes, so use it as the root. Open the directory...
82
83                outputDir = new File(rootDir, packagePath);
84
85                // Make sure the directory exists...
86
87                ensureDirectory(outputDir,env);
88
89            } else {
90
91                // Default package, so use root as output dir...
92
93                outputDir = rootDir;
94            }
95        } else {
96
97            // No root directory. Get the current working directory...
98
99            String workingDirPath = System.getProperty("user.dir");
100            File workingDir = new File(workingDirPath);
101
102            // Do we have a package name?
103
104            if (packagePath == null) {
105
106                // No, so use working directory...
107
108                outputDir = workingDir;
109
110            } else {
111
112                // Yes, so use working directory as the root...
113
114                outputDir = new File(workingDir, packagePath);
115
116                // Make sure the directory exists...
117
118                ensureDirectory(outputDir,env);
119            }
120        }
121
122        // Finally, return the directory...
123
124        return outputDir;
125    }
126
127    private static void ensureDirectory (File dir, BatchEnvironment env) {
128        if (!dir.exists()) {
129            dir.mkdirs();
130            if (!dir.exists()) {
131                env.error(0,"rmic.cannot.create.dir",dir.getAbsolutePath());
132                throw new InternalError();
133            }
134        }
135    }
136}
137