001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.ws.processor.util;
027:
028: import java.io.File;
029:
030: import com.sun.tools.internal.ws.processor.generator.GeneratorException;
031: import com.sun.tools.internal.ws.util.ClassNameInfo;
032:
033: /**
034: * Util provides static utility methods used by other wscompile classes.
035: *
036: * @author WS Development Team
037: */
038: public class DirectoryUtil {
039:
040: public static File getOutputDirectoryFor(String theClass,
041: File rootDir, ProcessorEnvironment env)
042: throws GeneratorException {
043:
044: File outputDir = null;
045: String qualifiedClassName = theClass;
046: String packagePath = null;
047: String packageName = ClassNameInfo
048: .getQualifier(qualifiedClassName);
049: if (packageName != null && packageName.length() > 0) {
050: packagePath = packageName.replace('.', File.separatorChar);
051: }
052:
053: // Do we have a root directory?
054: if (rootDir != null) {
055:
056: // Yes, do we have a package name?
057: if (packagePath != null) {
058:
059: // Yes, so use it as the root. Open the directory...
060: outputDir = new File(rootDir, packagePath);
061:
062: // Make sure the directory exists...
063: ensureDirectory(outputDir, env);
064: } else {
065:
066: // Default package, so use root as output dir...
067: outputDir = rootDir;
068: }
069: } else {
070:
071: // No root directory. Get the current working directory...
072: String workingDirPath = System.getProperty("user.dir");
073: File workingDir = new File(workingDirPath);
074:
075: // Do we have a package name?
076: if (packagePath == null) {
077:
078: // No, so use working directory...
079: outputDir = workingDir;
080: } else {
081:
082: // Yes, so use working directory as the root...
083: outputDir = new File(workingDir, packagePath);
084:
085: // Make sure the directory exists...
086: ensureDirectory(outputDir, env);
087: }
088: }
089:
090: // Finally, return the directory...
091: return outputDir;
092: }
093:
094: private static void ensureDirectory(File dir,
095: ProcessorEnvironment env) throws GeneratorException {
096:
097: if (!dir.exists()) {
098: dir.mkdirs();
099: if (!dir.exists()) {
100: throw new GeneratorException(
101: "generator.cannot.create.dir", dir
102: .getAbsolutePath());
103: }
104: }
105: }
106: }
|