langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/DirectoryManager.java
changeset 14508 eabcd457c4db
parent 14507 066419d1e732
parent 14455 a2cf4d4a4843
child 14509 4358b75583be
equal deleted inserted replaced
14507:066419d1e732 14508:eabcd457c4db
     1 /*
       
     2  * Copyright (c) 1998, 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.doclets.internal.toolkit.util;
       
    27 
       
    28 import com.sun.tools.doclets.internal.toolkit.*;
       
    29 import com.sun.javadoc.*;
       
    30 import java.io.*;
       
    31 
       
    32 
       
    33 /**
       
    34  * Handle the directory creations and the path string generations.
       
    35  * All static - never instaniated.
       
    36  *
       
    37  * This code is not part of an API.
       
    38  * It is implementation that is subject to change.
       
    39  * Do not use it as an API
       
    40  *
       
    41  * @since 1.2
       
    42  * @author Atul M Dambalkar
       
    43  */
       
    44 public class DirectoryManager {
       
    45 
       
    46     /**
       
    47      * The file separator string, "/", used in the formation of the URL path.
       
    48      */
       
    49     public static final String URL_FILE_SEPARATOR = "/";
       
    50 
       
    51     /**
       
    52      * Never instaniated.
       
    53      */
       
    54     private DirectoryManager() {
       
    55     }
       
    56 
       
    57     /**
       
    58      * Given a PackageDoc, return its URL path string.
       
    59      *
       
    60      * @param pd PackageDoc
       
    61      * @see #getPath(String)
       
    62      */
       
    63     public static String createPathString(PackageDoc pd) {
       
    64         if (pd == null) {
       
    65             return "";
       
    66         }
       
    67         return getPath(pd.name());
       
    68     }
       
    69 
       
    70     /**
       
    71      * Given a ClassDoc, return its URL path string.
       
    72      *
       
    73      * @param cd ClassDoc
       
    74      * @see #getPath(String)
       
    75      */
       
    76     public static String createPathString(ClassDoc cd) {
       
    77         if (cd == null) {
       
    78             return "";
       
    79         }
       
    80         PackageDoc pd = cd.containingPackage();
       
    81         return (pd == null)? "": getPath(pd.name());
       
    82     }
       
    83 
       
    84     /**
       
    85      * Given a PackageDoc, return the corresponding directory name
       
    86      * with the platform-dependent file separator between subdirectory names.
       
    87      * For example, if name of the package is "java.lang" , then it
       
    88      * returns "java/lang" on Unix and "java\lang" on Windows.
       
    89      * If name of the package contains no dot, then the value
       
    90      * will be returned unchanged.  Because package names cannot
       
    91      * end in a dot, the return value will never end with a slash.
       
    92      * <p>
       
    93      * Also see getPath for the URL separator version of this method
       
    94      * that takes a string instead of a PackageDoc.
       
    95      *
       
    96      * @param  pd    the PackageDoc
       
    97      * @return       the platform-dependent directory path for the package
       
    98      */
       
    99     public static String getDirectoryPath(PackageDoc pd) {
       
   100         return pd == null || pd.name().length() == 0 ? "" : getDirectoryPath(pd.name());
       
   101     }
       
   102 
       
   103     /**
       
   104      * Given a package name, return the corresponding directory name
       
   105      * with the platform-dependent file separator between subdirectory names.
       
   106      * For example, if name of the package is "java.lang" , then it
       
   107      * returns "java/lang" on Unix and "java\lang" on Windows.
       
   108      * If name of the package contains no dot, then the value
       
   109      * will be returned unchanged.  Because package names cannot
       
   110      * end in a dot, the return value will never end with a slash.
       
   111      * <p>
       
   112      * Also see getPath for the URL separator version of this method
       
   113      * that takes a string instead of a PackageDoc.
       
   114      *
       
   115      * @param  packageName    the name of the package
       
   116      * @return       the platform-dependent directory path for the package
       
   117      */
       
   118     public static String getDirectoryPath(String packageName) {
       
   119         if (packageName == null || packageName.length() == 0) {
       
   120             return "";
       
   121         }
       
   122         StringBuilder pathstr = new StringBuilder();
       
   123         for (int i = 0; i < packageName.length(); i++) {
       
   124             char ch = packageName.charAt(i);
       
   125             if (ch == '.') {
       
   126                 pathstr.append(URL_FILE_SEPARATOR);
       
   127             } else {
       
   128                 pathstr.append(ch);
       
   129             }
       
   130         }
       
   131         if (pathstr.length() > 0 && ! pathstr.toString().endsWith(URL_FILE_SEPARATOR)) {
       
   132             pathstr.append(URL_FILE_SEPARATOR);
       
   133         }
       
   134         return pathstr.toString();
       
   135     }
       
   136 
       
   137     /**
       
   138      * Given a package name (a string), return the path string,
       
   139      * with the URL separator "/" separating the subdirectory names.
       
   140      * If name of the package contains no dot, then the value
       
   141      * will be returned unchanged.  Because package names cannot
       
   142      * end in a dot, the return value will never end with a slash.
       
   143      * <p>
       
   144      * For example if the string is "com.sun.javadoc" then the URL
       
   145      * path string will be "com/sun/javadoc".
       
   146      *
       
   147      * @param name   the package name as a String
       
   148      * @return       the String URL path
       
   149      */
       
   150     public static String getPath(String name) {
       
   151         if (name == null || name.length() == 0) {
       
   152             return "";
       
   153         }
       
   154         StringBuilder pathstr = new StringBuilder();
       
   155         for (int i = 0; i < name.length(); i++) {
       
   156             char ch = name.charAt(i);
       
   157             if (ch == '.') {
       
   158                 pathstr.append(URL_FILE_SEPARATOR);
       
   159             } else {
       
   160                 pathstr.append(ch);
       
   161             }
       
   162         }
       
   163         return pathstr.toString();
       
   164     }
       
   165 
       
   166     /**
       
   167      * Given two package names as strings, return the relative path
       
   168      * from the package directory corresponding to the first string
       
   169      * to the package directory corresponding to the second string,
       
   170      * with the URL file separator "/" separating subdirectory names.
       
   171      * <p>
       
   172      * For example, if the parameter "from" is "java.lang"
       
   173      * and parameter "to" is "java.applet", return string
       
   174      * "../../java/applet".
       
   175      *
       
   176      * @param from   the package name from which path is calculated
       
   177      * @param to     the package name to which path is calculated
       
   178      * @return       relative path between "from" and "to" with URL
       
   179      *               separators
       
   180      * @see          #getRelativePath(String)
       
   181      * @see          #getPath(String)
       
   182      */
       
   183     public static String getRelativePath(String from, String to) {
       
   184         StringBuilder pathstr = new StringBuilder();
       
   185         pathstr.append(getRelativePath(from));
       
   186         pathstr.append(getPath(to));
       
   187         pathstr.append(URL_FILE_SEPARATOR);
       
   188         return pathstr.toString();
       
   189     }
       
   190 
       
   191     /**
       
   192      * Given a package name as a string, return relative path string
       
   193      * from the corresponding package directory to the root of
       
   194      * the documentation, using the URL separator "/" between
       
   195      * subdirectory names.
       
   196      * <p>
       
   197      * For example, if the string "from" is "java.lang",
       
   198      * return "../../"
       
   199      *
       
   200      * @param from    the package
       
   201      * @return        String relative path from "from".
       
   202      * @see           #getRelativePath(String, String)
       
   203      */
       
   204     public static String getRelativePath(PackageDoc from) {
       
   205         return from == null || from.name().length() == 0 ? "" : getRelativePath(from.name());
       
   206     }
       
   207 
       
   208     /**
       
   209      * Given a package name as a string, return relative path string
       
   210      * from the corresponding package directory to the root of
       
   211      * the documentation, using the URL separator "/" between
       
   212      * subdirectory names.
       
   213      * <p>
       
   214      * For example, if the string "from" is "java.lang",
       
   215      * return "../../"
       
   216      *
       
   217      * @param from    the package name
       
   218      * @return        String relative path from "from".
       
   219      * @see           #getRelativePath(String, String)
       
   220      */
       
   221     public static String getRelativePath(String from) {
       
   222         if (from == null || from.length() == 0) {
       
   223             return "";
       
   224         }
       
   225         StringBuilder pathstr = new StringBuilder();
       
   226         for (int i = 0; i < from.length(); i++) {
       
   227             char ch = from.charAt(i);
       
   228             if (ch == '.') {
       
   229                 pathstr.append(".." + URL_FILE_SEPARATOR);
       
   230             }
       
   231         }
       
   232         pathstr.append(".." + URL_FILE_SEPARATOR);
       
   233         return pathstr.toString();
       
   234     }
       
   235 
       
   236     /**
       
   237      * Given a relative or absolute path that might be empty,
       
   238      * convert it to a path that does not end with a
       
   239      * URL separator "/".  Used for converting
       
   240      * HtmlStandardWriter.relativepath when replacing {@docRoot}.
       
   241      *
       
   242      * @param path   the path to convert.  An empty path represents
       
   243      *               the current directory.
       
   244      */
       
   245     public static String getPathNoTrailingSlash(String path) {
       
   246         if ( path.equals("") ) {
       
   247             return ".";
       
   248         }
       
   249         if ( path.equals("/") ) {
       
   250             return "/.";
       
   251         }
       
   252         if ( path.endsWith("/") ) {
       
   253             // Remove trailing slash
       
   254             path = path.substring(0, path.length() -1);
       
   255         }
       
   256         return path;
       
   257     }
       
   258 
       
   259     /**
       
   260      * Given a path string create all the directories in the path. For example,
       
   261      * if the path string is "java/applet", the method will create directory
       
   262      * "java" and then "java/applet" if they don't exist. The file separator
       
   263      * string "/" is platform dependent system property.
       
   264      *
       
   265      * @param path Directory path string.
       
   266      */
       
   267     public static void createDirectory(Configuration configuration,
       
   268                                        String path) {
       
   269         if (path == null || path.length() == 0) {
       
   270             return;
       
   271         }
       
   272         File dir = new File(path);
       
   273         if (dir.exists()) {
       
   274             return;
       
   275         } else {
       
   276             if (dir.mkdirs()) {
       
   277                 return;
       
   278             } else {
       
   279                 configuration.message.error(
       
   280                        "doclet.Unable_to_create_directory_0", path);
       
   281                 throw new DocletAbortException();
       
   282             }
       
   283         }
       
   284     }
       
   285 
       
   286     /**
       
   287      * Given a package name and a file name, return the full path to that file.
       
   288      * For example, if PackageDoc passed is for "java.lang" and the filename
       
   289      * passed is "package-summary.html", then the string returned is
       
   290      * "java/lang/package-summary.html".
       
   291      *
       
   292      * @param pd         PackageDoc.
       
   293      * @param filename   File name to be appended to the path of the package.
       
   294      */
       
   295     public static String getPathToPackage(PackageDoc pd, String filename) {
       
   296         StringBuilder buf = new StringBuilder();
       
   297         String pathstr = createPathString(pd);
       
   298         if (pathstr.length() > 0) {
       
   299             buf.append(pathstr);
       
   300             buf.append(URL_FILE_SEPARATOR);
       
   301         }
       
   302         buf.append(filename);
       
   303         return buf.toString();
       
   304     }
       
   305 
       
   306     /**
       
   307      * Given a class name return the full path to the class file.
       
   308      * For example, if ClassDoc passed is for "java.lang.Object" then the
       
   309      * string returned is "java/lang/Object.html".
       
   310      *
       
   311      * @param cd   ClassDoc.
       
   312      */
       
   313     public static String getPathToClass(ClassDoc cd) {
       
   314         return getPathToPackage(cd.containingPackage(), cd.name() + ".html");
       
   315     }
       
   316 
       
   317 }