src/share/classes/com/sun/tools/doclets/internal/toolkit/util/DirectoryManager.java

Mon, 02 May 2011 02:13:02 -0700

author
bpatel
date
Mon, 02 May 2011 02:13:02 -0700
changeset 995
62bc3775d5bb
parent 798
4868a36f6fd8
child 1362
c46e0c9940d6
permissions
-rw-r--r--

6492694: @deprecated tag doesn't work in package-info files.
Reviewed-by: jjg

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

mercurial