aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.internal.ws.processor.util; aoqi@0: aoqi@0: import com.sun.tools.internal.ws.processor.generator.GeneratorException; aoqi@0: import com.sun.tools.internal.ws.util.ClassNameInfo; aoqi@0: aoqi@0: import java.io.File; aoqi@0: import java.io.IOException; aoqi@0: aoqi@0: /** aoqi@0: * Util provides static utility methods used by other wscompile classes. aoqi@0: * aoqi@0: * @author WS Development Team aoqi@0: */ aoqi@0: public class DirectoryUtil { aoqi@0: aoqi@0: public static File getOutputDirectoryFor(String theClass, File rootDir) throws GeneratorException { aoqi@0: aoqi@0: File outputDir = null; aoqi@0: String qualifiedClassName = theClass; aoqi@0: String packagePath = null; aoqi@0: String packageName = ClassNameInfo.getQualifier(qualifiedClassName); aoqi@0: if (packageName != null && packageName.length() > 0) { aoqi@0: packagePath = packageName.replace('.', File.separatorChar); aoqi@0: } aoqi@0: aoqi@0: // Do we have a root directory? aoqi@0: if (rootDir != null) { aoqi@0: aoqi@0: // Yes, do we have a package name? aoqi@0: if (packagePath != null) { aoqi@0: aoqi@0: // Yes, so use it as the root. Open the directory... aoqi@0: outputDir = new File(rootDir, packagePath); aoqi@0: aoqi@0: // Make sure the directory exists... aoqi@0: ensureDirectory(outputDir); aoqi@0: } else { aoqi@0: aoqi@0: // Default package, so use root as output dir... aoqi@0: outputDir = rootDir; aoqi@0: } aoqi@0: } else { aoqi@0: aoqi@0: // No root directory. Get the current working directory... aoqi@0: String workingDirPath = System.getProperty("user.dir"); aoqi@0: File workingDir = new File(workingDirPath); aoqi@0: aoqi@0: // Do we have a package name? aoqi@0: if (packagePath == null) { aoqi@0: aoqi@0: // No, so use working directory... aoqi@0: outputDir = workingDir; aoqi@0: } else { aoqi@0: aoqi@0: // Yes, so use working directory as the root... aoqi@0: outputDir = new File(workingDir, packagePath); aoqi@0: aoqi@0: // Make sure the directory exists... aoqi@0: ensureDirectory(outputDir); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Finally, return the directory... aoqi@0: return outputDir; aoqi@0: } aoqi@0: aoqi@0: public static String getRelativePathfromCommonBase(File file, File base) throws IOException { aoqi@0: String basePath = base.getCanonicalPath(); aoqi@0: String filePath = file.getCanonicalPath(); aoqi@0: return filePath.substring(basePath.length()); aoqi@0: aoqi@0: } aoqi@0: aoqi@0: private static void ensureDirectory(File dir) throws GeneratorException { aoqi@0: if (!dir.exists()) { aoqi@0: boolean created = dir.mkdirs(); aoqi@0: if (!created || !dir.exists()) { aoqi@0: throw new GeneratorException("generator.cannot.create.dir", aoqi@0: dir.getAbsolutePath()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: }