1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/ws/processor/util/DirectoryUtil.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,110 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.internal.ws.processor.util; 1.30 + 1.31 +import com.sun.tools.internal.ws.processor.generator.GeneratorException; 1.32 +import com.sun.tools.internal.ws.util.ClassNameInfo; 1.33 + 1.34 +import java.io.File; 1.35 +import java.io.IOException; 1.36 + 1.37 +/** 1.38 + * Util provides static utility methods used by other wscompile classes. 1.39 + * 1.40 + * @author WS Development Team 1.41 + */ 1.42 +public class DirectoryUtil { 1.43 + 1.44 + public static File getOutputDirectoryFor(String theClass, File rootDir) throws GeneratorException { 1.45 + 1.46 + File outputDir = null; 1.47 + String qualifiedClassName = theClass; 1.48 + String packagePath = null; 1.49 + String packageName = ClassNameInfo.getQualifier(qualifiedClassName); 1.50 + if (packageName != null && packageName.length() > 0) { 1.51 + packagePath = packageName.replace('.', File.separatorChar); 1.52 + } 1.53 + 1.54 + // Do we have a root directory? 1.55 + if (rootDir != null) { 1.56 + 1.57 + // Yes, do we have a package name? 1.58 + if (packagePath != null) { 1.59 + 1.60 + // Yes, so use it as the root. Open the directory... 1.61 + outputDir = new File(rootDir, packagePath); 1.62 + 1.63 + // Make sure the directory exists... 1.64 + ensureDirectory(outputDir); 1.65 + } else { 1.66 + 1.67 + // Default package, so use root as output dir... 1.68 + outputDir = rootDir; 1.69 + } 1.70 + } else { 1.71 + 1.72 + // No root directory. Get the current working directory... 1.73 + String workingDirPath = System.getProperty("user.dir"); 1.74 + File workingDir = new File(workingDirPath); 1.75 + 1.76 + // Do we have a package name? 1.77 + if (packagePath == null) { 1.78 + 1.79 + // No, so use working directory... 1.80 + outputDir = workingDir; 1.81 + } else { 1.82 + 1.83 + // Yes, so use working directory as the root... 1.84 + outputDir = new File(workingDir, packagePath); 1.85 + 1.86 + // Make sure the directory exists... 1.87 + ensureDirectory(outputDir); 1.88 + } 1.89 + } 1.90 + 1.91 + // Finally, return the directory... 1.92 + return outputDir; 1.93 + } 1.94 + 1.95 + public static String getRelativePathfromCommonBase(File file, File base) throws IOException { 1.96 + String basePath = base.getCanonicalPath(); 1.97 + String filePath = file.getCanonicalPath(); 1.98 + return filePath.substring(basePath.length()); 1.99 + 1.100 + } 1.101 + 1.102 + private static void ensureDirectory(File dir) 1.103 + throws GeneratorException { 1.104 + 1.105 + if (!dir.exists()) { 1.106 + dir.mkdirs(); 1.107 + if (!dir.exists()) { 1.108 + throw new GeneratorException("generator.cannot.create.dir", 1.109 + dir.getAbsolutePath()); 1.110 + } 1.111 + } 1.112 + } 1.113 +}