1 /*
2  * Copyright (c) 1997, 2012, 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 package com.sun.tools.internal.ws.processor.util;
27 
28 import com.sun.tools.internal.ws.processor.generator.GeneratorException;
29 import com.sun.tools.internal.ws.util.ClassNameInfo;
30 
31 import java.io.File;
32 import java.io.IOException;
33 
34 /**
35  * Util provides static utility methods used by other wscompile classes.
36  *
37  * @author WS Development Team
38  */
39 public class DirectoryUtil  {
40 
getOutputDirectoryFor(String theClass, File rootDir)41     public static File getOutputDirectoryFor(String theClass, File rootDir) throws GeneratorException {
42 
43         File outputDir = null;
44         String qualifiedClassName = theClass;
45         String packagePath = null;
46         String packageName = ClassNameInfo.getQualifier(qualifiedClassName);
47         if (packageName != null && packageName.length() > 0) {
48             packagePath = packageName.replace('.', File.separatorChar);
49         }
50 
51         // Do we have a root directory?
52         if (rootDir != null) {
53 
54             // Yes, do we have a package name?
55             if (packagePath != null) {
56 
57                 // Yes, so use it as the root. Open the directory...
58                 outputDir = new File(rootDir, packagePath);
59 
60                 // Make sure the directory exists...
61                 ensureDirectory(outputDir);
62             } else {
63 
64                 // Default package, so use root as output dir...
65                 outputDir = rootDir;
66             }
67         } else {
68 
69             // No root directory. Get the current working directory...
70             String workingDirPath = System.getProperty("user.dir");
71             File workingDir = new File(workingDirPath);
72 
73             // Do we have a package name?
74             if (packagePath == null) {
75 
76                 // No, so use working directory...
77                 outputDir = workingDir;
78             } else {
79 
80                 // Yes, so use working directory as the root...
81                 outputDir = new File(workingDir, packagePath);
82 
83                 // Make sure the directory exists...
84                 ensureDirectory(outputDir);
85             }
86         }
87 
88         // Finally, return the directory...
89         return outputDir;
90     }
91 
getRelativePathfromCommonBase(File file, File base)92     public static String getRelativePathfromCommonBase(File file, File base) throws IOException {
93         String basePath = base.getCanonicalPath();
94         String filePath = file.getCanonicalPath();
95         return filePath.substring(basePath.length());
96 
97     }
98 
ensureDirectory(File dir)99     private static void ensureDirectory(File dir) throws GeneratorException {
100         if (!dir.exists()) {
101             boolean created = dir.mkdirs();
102             if (!created || !dir.exists()) {
103                 throw new GeneratorException("generator.cannot.create.dir",
104                     dir.getAbsolutePath());
105             }
106         }
107     }
108 }
109