langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/DirectoryManager.java
author ohair
Tue, 25 May 2010 15:54:51 -0700
changeset 5520 86e4b9a9da40
parent 10 06bc494ca11e
child 7614 cfadc977ca75
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     2
 * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.doclets.internal.toolkit.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.doclets.internal.toolkit.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 * Handle the directory creations and the path string generations.
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 * All static - never instaniated.
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * This code is not part of an API.
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * It is implementation that is subject to change.
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * Do not use it as an API
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * @since 1.2
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * @author Atul M Dambalkar
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
public class DirectoryManager {
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
     * The file separator string, "/", used in the formation of the URL path.
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    public static final String URL_FILE_SEPERATOR = "/";
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
     * Never instaniated.
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    private DirectoryManager() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
     * Given a PackageDoc, return its URL path string.
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
     * @param pd PackageDoc
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     * @see #getPath(String)
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    public static String createPathString(PackageDoc pd) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        if (pd == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
            return "";
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        return getPath(pd.name());
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     * Given a ClassDoc, return its URL path string.
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     * @param cd ClassDoc
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     * @see #getPath(String)
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    public static String createPathString(ClassDoc cd) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        if (cd == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
            return "";
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        PackageDoc pd = cd.containingPackage();
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        return (pd == null)? "": getPath(pd.name());
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
     * Given a PackageDoc, return the corresponding directory name
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     * with the platform-dependent file separator between subdirectory names.
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     * For example, if name of the package is "java.lang" , then it
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     * returns "java/lang" on Unix and "java\lang" on Windows.
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     * If name of the package contains no dot, then the value
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     * will be returned unchanged.  Because package names cannot
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     * end in a dot, the return value will never end with a slash.
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     * <p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     * Also see getPath for the URL separator version of this method
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     * that takes a string instead of a PackageDoc.
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
     * @param  pd    the PackageDoc
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
     * @return       the platform-dependent directory path for the package
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    public static String getDirectoryPath(PackageDoc pd) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        return pd == null || pd.name().length() == 0 ? "" : getDirectoryPath(pd.name());
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     * Given a package name, return the corresponding directory name
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
     * with the platform-dependent file separator between subdirectory names.
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
     * For example, if name of the package is "java.lang" , then it
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     * returns "java/lang" on Unix and "java\lang" on Windows.
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     * If name of the package contains no dot, then the value
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     * will be returned unchanged.  Because package names cannot
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     * end in a dot, the return value will never end with a slash.
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     * <p>
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     * Also see getPath for the URL separator version of this method
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
     * that takes a string instead of a PackageDoc.
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     * @param  packageName    the name of the package
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
     * @return       the platform-dependent directory path for the package
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    public static String getDirectoryPath(String packageName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        if (packageName == null || packageName.length() == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            return "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        StringBuffer pathstr = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        for (int i = 0; i < packageName.length(); i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
            char ch = packageName.charAt(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            if (ch == '.') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                pathstr.append(URL_FILE_SEPERATOR);
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                pathstr.append(ch);
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        if (pathstr.length() > 0 && ! pathstr.toString().endsWith(URL_FILE_SEPERATOR)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
            pathstr.append(URL_FILE_SEPERATOR);
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        return pathstr.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     * Given a package name (a string), return the path string,
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     * with the URL separator "/" separating the subdirectory names.
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
     * If name of the package contains no dot, then the value
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     * will be returned unchanged.  Because package names cannot
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
     * end in a dot, the return value will never end with a slash.
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
     * <p>
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
     * For example if the string is "com.sun.javadoc" then the URL
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
     * path string will be "com/sun/javadoc".
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
     * @param name   the package name as a String
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
     * @return       the String URL path
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    public static String getPath(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        if (name == null || name.length() == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            return "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        StringBuffer pathstr = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        for (int i = 0; i < name.length(); i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            char ch = name.charAt(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
            if (ch == '.') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
                pathstr.append(URL_FILE_SEPERATOR);
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
                pathstr.append(ch);
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        return pathstr.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
     * Given two package names as strings, return the relative path
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
     * from the package directory corresponding to the first string
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
     * to the package directory corresponding to the second string,
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
     * with the URL file separator "/" separating subdirectory names.
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
     * <p>
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
     * For example, if the parameter "from" is "java.lang"
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
     * and parameter "to" is "java.applet", return string
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
     * "../../java/applet".
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
     * @param from   the package name from which path is calculated
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
     * @param to     the package name to which path is calculated
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
     * @return       relative path between "from" and "to" with URL
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
     *               separators
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
     * @see          #getRelativePath(String)
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
     * @see          #getPath(String)
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
    public static String getRelativePath(String from, String to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
        StringBuffer pathstr = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        pathstr.append(getRelativePath(from));
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        pathstr.append(getPath(to));
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        pathstr.append(URL_FILE_SEPERATOR);
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        return pathstr.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
     * Given a package name as a string, return relative path string
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
     * from the corresponding package directory to the root of
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
     * the documentation, using the URL separator "/" between
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
     * subdirectory names.
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
     * <p>
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
     * For example, if the string "from" is "java.lang",
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
     * return "../../"
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
     * @param from    the package
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
     * @return        String relative path from "from".
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
     * @see           #getRelativePath(String, String)
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    public static String getRelativePath(PackageDoc from) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        return from == null || from.name().length() == 0 ? "" : getRelativePath(from.name());
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
     * Given a package name as a string, return relative path string
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
     * from the corresponding package directory to the root of
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
     * the documentation, using the URL separator "/" between
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
     * subdirectory names.
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
     * <p>
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
     * For example, if the string "from" is "java.lang",
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
     * return "../../"
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
     * @param from    the package name
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
     * @return        String relative path from "from".
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
     * @see           #getRelativePath(String, String)
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
    public static String getRelativePath(String from) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        if (from == null || from.length() == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
            return "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
        StringBuffer pathstr = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        for (int i = 0; i < from.length(); i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
            char ch = from.charAt(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
            if (ch == '.') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
                pathstr.append(".." + URL_FILE_SEPERATOR);
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
        pathstr.append(".." + URL_FILE_SEPERATOR);
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        return pathstr.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
     * Given a relative or absolute path that might be empty,
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
     * convert it to a path that does not end with a
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
     * URL separator "/".  Used for converting
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
     * HtmlStandardWriter.relativepath when replacing {@docRoot}.
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
     * @param path   the path to convert.  An empty path represents
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
     *               the current directory.
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
    public static String getPathNoTrailingSlash(String path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
        if ( path.equals("") ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            return ".";
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
        if ( path.equals("/") ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
            return "/.";
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        if ( path.endsWith("/") ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
            // Remove trailing slash
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
            path = path.substring(0, path.length() -1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        return path;
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
     * Given a path string create all the directories in the path. For example,
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
     * if the path string is "java/applet", the method will create directory
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
     * "java" and then "java/applet" if they don't exist. The file separator
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
     * string "/" is platform dependent system property.
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
     * @param path Directory path string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
    public static void createDirectory(Configuration configuration,
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                                       String path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
        if (path == null || path.length() == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
        File dir = new File(path);
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
        if (dir.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
            if (dir.mkdirs()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
                configuration.message.error(
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                       "doclet.Unable_to_create_directory_0", path);
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
                throw new DocletAbortException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
     * Given a package name and a file name, return the full path to that file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
     * For example, if PackageDoc passed is for "java.lang" and the filename
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
     * passed is "package-summary.html", then the string returned is
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
     * "java/lang/package-summary.html".
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
     * @param pd         PackageDoc.
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
     * @param filename   File name to be appended to the path of the package.
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
    public static String getPathToPackage(PackageDoc pd, String filename) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        StringBuffer buf = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
        String pathstr = createPathString(pd);
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
        if (pathstr.length() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
            buf.append(pathstr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
            buf.append(URL_FILE_SEPERATOR);
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
        buf.append(filename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
        return buf.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
     * Given a class name return the full path to the class file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
     * For example, if ClassDoc passed is for "java.lang.Object" then the
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
     * string returned is "java/lang/Object.html".
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
     * @param cd   ClassDoc.
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
    public static String getPathToClass(ClassDoc cd) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
        return getPathToPackage(cd.containingPackage(), cd.name() + ".html");
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
}