001: /*
002: * Copyright 1998-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.doclets.internal.toolkit.util;
027:
028: import com.sun.tools.doclets.internal.toolkit.*;
029: import com.sun.javadoc.*;
030: import java.io.*;
031:
032: /**
033: * Handle the directory creations and the path string generations.
034: * All static - never instaniated.
035: *
036: * This code is not part of an API.
037: * It is implementation that is subject to change.
038: * Do not use it as an API
039: *
040: * @since 1.2
041: * @author Atul M Dambalkar
042: */
043: public class DirectoryManager {
044:
045: /**
046: * The file separator string, "/", used in the formation of the URL path.
047: */
048: public static final String URL_FILE_SEPERATOR = "/";
049:
050: /**
051: * Never instaniated.
052: */
053: private DirectoryManager() {
054: }
055:
056: /**
057: * Given a PackageDoc, return its URL path string.
058: *
059: * @param pd PackageDoc
060: * @see #getPath(String)
061: */
062: public static String createPathString(PackageDoc pd) {
063: if (pd == null) {
064: return "";
065: }
066: return getPath(pd.name());
067: }
068:
069: /**
070: * Given a ClassDoc, return its URL path string.
071: *
072: * @param cd ClassDoc
073: * @see #getPath(String)
074: */
075: public static String createPathString(ClassDoc cd) {
076: if (cd == null) {
077: return "";
078: }
079: PackageDoc pd = cd.containingPackage();
080: return (pd == null) ? "" : getPath(pd.name());
081: }
082:
083: /**
084: * Given a PackageDoc, return the corresponding directory name
085: * with the platform-dependent file separator between subdirectory names.
086: * For example, if name of the package is "java.lang" , then it
087: * returns "java/lang" on Unix and "java\lang" on Windows.
088: * If name of the package contains no dot, then the value
089: * will be returned unchanged. Because package names cannot
090: * end in a dot, the return value will never end with a slash.
091: * <p>
092: * Also see getPath for the URL separator version of this method
093: * that takes a string instead of a PackageDoc.
094: *
095: * @param pd the PackageDoc
096: * @return the platform-dependent directory path for the package
097: */
098: public static String getDirectoryPath(PackageDoc pd) {
099: return pd == null || pd.name().length() == 0 ? ""
100: : 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: StringBuffer pathstr = new StringBuffer();
123: for (int i = 0; i < packageName.length(); i++) {
124: char ch = packageName.charAt(i);
125: if (ch == '.') {
126: pathstr.append(URL_FILE_SEPERATOR);
127: } else {
128: pathstr.append(ch);
129: }
130: }
131: if (pathstr.length() > 0
132: && !pathstr.toString().endsWith(URL_FILE_SEPERATOR)) {
133: pathstr.append(URL_FILE_SEPERATOR);
134: }
135: return pathstr.toString();
136: }
137:
138: /**
139: * Given a package name (a string), return the path string,
140: * with the URL separator "/" separating the subdirectory names.
141: * If name of the package contains no dot, then the value
142: * will be returned unchanged. Because package names cannot
143: * end in a dot, the return value will never end with a slash.
144: * <p>
145: * For example if the string is "com.sun.javadoc" then the URL
146: * path string will be "com/sun/javadoc".
147: *
148: * @param name the package name as a String
149: * @return the String URL path
150: */
151: public static String getPath(String name) {
152: if (name == null || name.length() == 0) {
153: return "";
154: }
155: StringBuffer pathstr = new StringBuffer();
156: for (int i = 0; i < name.length(); i++) {
157: char ch = name.charAt(i);
158: if (ch == '.') {
159: pathstr.append(URL_FILE_SEPERATOR);
160: } else {
161: pathstr.append(ch);
162: }
163: }
164: return pathstr.toString();
165: }
166:
167: /**
168: * Given two package names as strings, return the relative path
169: * from the package directory corresponding to the first string
170: * to the package directory corresponding to the second string,
171: * with the URL file separator "/" separating subdirectory names.
172: * <p>
173: * For example, if the parameter "from" is "java.lang"
174: * and parameter "to" is "java.applet", return string
175: * "../../java/applet".
176: *
177: * @param from the package name from which path is calculated
178: * @param to the package name to which path is calculated
179: * @return relative path between "from" and "to" with URL
180: * separators
181: * @see #getRelativePath(String)
182: * @see #getPath(String)
183: */
184: public static String getRelativePath(String from, String to) {
185: StringBuffer pathstr = new StringBuffer();
186: pathstr.append(getRelativePath(from));
187: pathstr.append(getPath(to));
188: pathstr.append(URL_FILE_SEPERATOR);
189: return pathstr.toString();
190: }
191:
192: /**
193: * Given a package name as a string, return relative path string
194: * from the corresponding package directory to the root of
195: * the documentation, using the URL separator "/" between
196: * subdirectory names.
197: * <p>
198: * For example, if the string "from" is "java.lang",
199: * return "../../"
200: *
201: * @param from the package
202: * @return String relative path from "from".
203: * @see #getRelativePath(String, String)
204: */
205: public static String getRelativePath(PackageDoc from) {
206: return from == null || from.name().length() == 0 ? ""
207: : getRelativePath(from.name());
208: }
209:
210: /**
211: * Given a package name as a string, return relative path string
212: * from the corresponding package directory to the root of
213: * the documentation, using the URL separator "/" between
214: * subdirectory names.
215: * <p>
216: * For example, if the string "from" is "java.lang",
217: * return "../../"
218: *
219: * @param from the package name
220: * @return String relative path from "from".
221: * @see #getRelativePath(String, String)
222: */
223: public static String getRelativePath(String from) {
224: if (from == null || from.length() == 0) {
225: return "";
226: }
227: StringBuffer pathstr = new StringBuffer();
228: for (int i = 0; i < from.length(); i++) {
229: char ch = from.charAt(i);
230: if (ch == '.') {
231: pathstr.append(".." + URL_FILE_SEPERATOR);
232: }
233: }
234: pathstr.append(".." + URL_FILE_SEPERATOR);
235: return pathstr.toString();
236: }
237:
238: /**
239: * Given a relative or absolute path that might be empty,
240: * convert it to a path that does not end with a
241: * URL separator "/". Used for converting
242: * HtmlStandardWriter.relativepath when replacing {@docRoot}.
243: *
244: * @param path the path to convert. An empty path represents
245: * the current directory.
246: */
247: public static String getPathNoTrailingSlash(String path) {
248: if (path.equals("")) {
249: return ".";
250: }
251: if (path.equals("/")) {
252: return "/.";
253: }
254: if (path.endsWith("/")) {
255: // Remove trailing slash
256: path = path.substring(0, path.length() - 1);
257: }
258: return path;
259: }
260:
261: /**
262: * Given a path string create all the directories in the path. For example,
263: * if the path string is "java/applet", the method will create directory
264: * "java" and then "java/applet" if they don't exist. The file separator
265: * string "/" is platform dependent system property.
266: *
267: * @param path Directory path string.
268: */
269: public static void createDirectory(Configuration configuration,
270: String path) {
271: if (path == null || path.length() == 0) {
272: return;
273: }
274: File dir = new File(path);
275: if (dir.exists()) {
276: return;
277: } else {
278: if (dir.mkdirs()) {
279: return;
280: } else {
281: configuration.message.error(
282: "doclet.Unable_to_create_directory_0", path);
283: throw new DocletAbortException();
284: }
285: }
286: }
287:
288: /**
289: * Given a package name and a file name, return the full path to that file.
290: * For example, if PackageDoc passed is for "java.lang" and the filename
291: * passed is "package-summary.html", then the string returned is
292: * "java/lang/package-summary.html".
293: *
294: * @param pd PackageDoc.
295: * @param filename File name to be appended to the path of the package.
296: */
297: public static String getPathToPackage(PackageDoc pd, String filename) {
298: StringBuffer buf = new StringBuffer();
299: String pathstr = createPathString(pd);
300: if (pathstr.length() > 0) {
301: buf.append(pathstr);
302: buf.append(URL_FILE_SEPERATOR);
303: }
304: buf.append(filename);
305: return buf.toString();
306: }
307:
308: /**
309: * Given a class name return the full path to the class file.
310: * For example, if ClassDoc passed is for "java.lang.Object" then the
311: * string returned is "java/lang/Object.html".
312: *
313: * @param cd ClassDoc.
314: */
315: public static String getPathToClass(ClassDoc cd) {
316: return getPathToPackage(cd.containingPackage(), cd.name()
317: + ".html");
318: }
319:
320: }
|